显着性指标未出现在 R 的图表中

问题描述 投票:0回答:1

我对 R 比较陌生,并且在显示显着差异的星号时遇到问题,尽管方差分析和 Tukey 事后显示 p <0.05. I also cannot seem to get the tukey comparisons to only show comparisons to Concentration_0. Any help would be appreciated

代码:

# Step 0: Packages

library(tidyverse)
library(ggsignif)

# Step 1: Create the data frame (Your provided data)
data <- data.frame(
    Concentration_100 = c(37064, 43632, 41114, 30876),
    Concentration_10  = c(45548, 47482, 41894, 44574),
    Concentration_1   = c(45587, 48042, 47934, 40098),
    Concentration_0.1 = c(32750, 41024, 39483, 34704),
    Concentration_0.01 = c(45407, 53434, 48684, 42951),
    Concentration_0 = c(101895, 132464, 103751, 108483)
)

# Step 2: Reshape the data to long format using tidyr
long_data <- data %>%
    pivot_longer(cols = everything(), 
                 names_to = "Concentration", 
                 values_to = "Value")

# Step 3: Calculate the averages and standard errors for each concentration
average_data <- long_data %>%
    group_by(Concentration) %>%
    summarize(Average = mean(Value), .groups = "drop")

# Calculate the standard error
se_data <- long_data %>%
    group_by(Concentration) %>%
    summarize(SE = sd(Value) / sqrt(n()), .groups = "drop")

# Merge averages and standard errors into a single data frame
average_data <- merge(average_data, se_data, by = "Concentration")

# Step 4: Convert Concentration to a factor for proper ordering
long_data$Concentration <- factor(long_data$Concentration, 
                                   levels = c("Concentration_100", 
                                              "Concentration_10", 
                                              "Concentration_1", 
                                              "Concentration_0.1", 
                                              "Concentration_0.01", 
                                              "Concentration_0"))

# Step 5: Perform the one-way ANOVA
anova_result <- aov(Value ~ Concentration, data = long_data)

# Step 6: Run Tukey's HSD (Post-Hoc Test)
tukey_result <- TukeyHSD(anova_result)

# Step 7: Extract only comparisons with Concentration_0
tukey_comparisons <- tukey_result$Concentration

# Filter to show comparisons with Concentration_0
comparisons_with_control <- tukey_result$`Concentration`[
    grep("Concentration_0", rownames(tukey_result$`Concentration`), 
         ignore.case = TRUE), ]

# Print the filtered comparisons
print(comparisons_with_control)

# Step 8: Define a custom color palette for the columns (from light to dark blue)
blue_colors <- c(
    "Concentration_100" = "#A3C9F1",  # light blue
    "Concentration_10"  = "#6FA9D2",  # medium-light blue
    "Concentration_1"   = "#3989B1",  # medium blue
    "Concentration_0.1" = "#1F6C96",  # darker blue
    "Concentration_0.01" = "#115F82", # dark blue
    "Concentration_0"   = "#0C4E6D"   # darkest blue
)

# Find the maximum value in the data to ensure it's not clipped
max_value <- max(long_data$Value, na.rm = TRUE)  # Get the maximum value from the raw data points

# Set the upper limit for the y-axis to be a bit higher than the maximum value
y_axis_limit <- max_value * 1.1  # Increase by 10% to add some space

# Step 9: Create the plot with significance indicators and custom colors
ggplot() +
    # Add the average columns (bar heights) with custom colors
    geom_col(data = average_data, aes(x = Concentration, 
                                       y = Average, fill = Concentration), 
             width = 0.6, color = "black") +  
    
    # Add individual data points in front of the columns
    geom_jitter(data = long_data, aes(x = Concentration, y = Value), 
                color = "black", size = 3, width = 0.2, 
                height = 0, alpha = 0.8, shape = 21, fill = "gray") +
  
    # Add standard error bars
    geom_errorbar(data = average_data, aes(x = Concentration, 
                                           ymin = Average - SE, ymax = Average + SE), 
                  width = 0.2, color = "black") +
  
    # Customize axis labels and title
    labs(x = "Bortezomib (µM)",  # x-axis title
         y = "Relative Fluorescence Intensity (544/590 nm)") +  # y-axis title
  
    # Adjust x-axis labels to show just the concentration values
    scale_x_discrete(labels = c("Concentration_100" = "100",
                                "Concentration_10" = "10", 
                                "Concentration_1" = "1",
                                "Concentration_0.1" = "0.1", 
                                "Concentration_0.01" = "0.01", 
                                "Concentration_0" = "0")) +
  
    # Adjust the y-axis to reduce padding (set expand to c(0, 0) to remove space above the columns)
    scale_y_continuous(expand = c(0, 0), limits = c(0, y_axis_limit)) +  
  
    # Apply the custom color palette
    scale_fill_manual(values = blue_colors) +
  
    theme_minimal() +
    theme(axis.text.x = element_text(angle = 0, hjust = 0.5, 
                                     colour = "black"),
          axis.text.y = element_text(colour = "black"),
          axis.line = element_line(color = "black", size = 0.5),  # Add axis lines
          panel.background = element_blank(),  # Remove panel background
          panel.grid = element_blank(),  # Remove grid lines
          legend.position = "none",  # Remove the legend
          axis.ticks.x = element_line(color = "black")) +  
  
    # Add significance indicators using ggsignif for only comparisons with Concentration_0
    geom_signif(
        comparisons = list(
            c("Concentration_0", "Concentration_100"),
            c("Concentration_0", "Concentration_10"),
            c("Concentration_0", "Concentration_1"),
            c("Concentration_0", "Concentration_0.1"),
            c("Concentration_0", "Concentration_0.01")
        ),
        map_signif_level = TRUE, textsize = 3, 
        y_position = y_axis_limit * 1.05
    )

Chatgpt 无法帮助我解决这个问题

r ggplot2 presentation significance
1个回答
0
投票

您没有将任何数据或美学映射传递给 geom_signif()。将其添加到 ggplot 函数或 geom_signif 中。此外,y_position 超出范围,因此您需要扩大范围或降低范围。

例如:

  geom_signif(
    data = average_data, 
    aes(x = Concentration, 
        y = Average),
    comparisons = list(
      c("Concentration_0", "Concentration_100"),
      c("Concentration_0", "Concentration_10"),
      c("Concentration_0", "Concentration_1"),
      c("Concentration_0", "Concentration_0.1"),
      c("Concentration_0", "Concentration_0.01")
    ),
    map_signif_level = TRUE, textsize = 3, 
    y_position = y_axis_limit * 0.9
  )
© www.soinside.com 2019 - 2024. All rights reserved.