在R中使用scale_y_break后更改y轴标签

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

我有两组相对于基线的百分比变化数据。为了可视化数据,我创建了一个箱线图,并使用 stat_summary 在底部添加了观察数。此外,我还进行了一项测试来评估每组相对于基线的百分比变化是否与零显着不同。

我尝试将y轴分成两部分,但是在尝试使用scale_y_continuous修改y轴的标签后,遇到了一个问题:y轴显示了两次,在右侧和左侧。如何修改 y 轴的标签而不显示两次?

这是我的数据和我在 R 中的尝试:

set.seed(200)
# data
df <- data.frame(
  G1 = c(rep("A", 8), rep("B", 30)),
  G2 = c(rnorm(8, mean = 2, sd = 10), rnorm(29, mean = 50, sd = 20), 1000)
)

n_fun <- function(x){
  return(data.frame(y = min(df$G2) * 5, # adjusted line
                    label = paste("N=", length(x), "\n")))
  
}

library(dplyr)
# calculate the mean and SD by group
mean_sd <- df %>% 
  group_by(G1) %>% 
  summarize(
    rmean = mean(G2),
    rsd = sd(G2)
  )
# ____ Boxplots: 
theme_set(theme_minimal())

# Test whether each group differs from 0
WC_tests = df %>%
  group_by(G1) %>%
  summarise(P = wilcox.test(G2, mu = 0)$p.value,
            Sig = if (P <= 0.0001) {
              "****"} else if (P <= 0.001) {
              "***" } else if (P <= 0.01) {
              "**"  } else if (P <= 0.05) {
              "*"   } else {
              "ns"  },
            MaxWidth = max(G2))

# gplot
ggboxplot(df, x = "G1", y = "G2", color = "G1", add = "jitter", palette = "jco") +
  # Axis and legende
  xlab("") + 
  ylab("Percent Change") +
  theme(legend.key.size = unit(2.5, "lines")) +
  scale_y_break(c(100, 980)) + # BREAK
  scale_y_continuous(breaks = c(0,50,100, 980, 1000), labels = c(0,50,100, 980, 1000)) + 
  theme(axis.text.x = element_text(angle = 0, size = 9),
        axis.text.y = element_text(size = 10),
        # Remove labels on the right side
        axis.text.y.right = element_blank()) +
  
  # Sample size
  stat_summary(fun.data = n_fun, geom = "text",
               aes(group = G1), hjust = 0.8,
               position = position_dodge(0.9), size = 3) +
  
  # # Wilcoxon test mean comparaison
  # Use the prepared table of test results as data for the geom
  geom_text(aes(label = Sig, y = max(df$G2) + 3), size = 3,
            data = WC_tests)+
  #  y = 0
  geom_hline(yintercept = 0, linetype = "dashed", color = "gray") +
  
  # Mean and sd
  geom_text(data = mean_sd, aes(x = G1, y = 990,
                                label = paste("Mean(SD):",
                                              round(rmean, 2),
                                              "(",
                                              round(rsd, 2),
                                              ")")),
            color = "black")

谢谢! 夏玛

r ggplot2 axis-labels yaxis
2个回答
0
投票

您可以使用

ticklabels
中的
scale_y_break
参数。那么你就不需要
scale_y_continuous
函数来防止像这样的两个 y 轴:

library(ggpubr)
library(ggbreak)
ggboxplot(df, x = "G1", y = "G2", color = "G1", add = "jitter", palette = "jco") +
  # Axis and legende
  xlab("") + 
  ylab("Percent Change") +
  theme(legend.key.size = unit(2.5, "lines")) +
  scale_y_break(c(100, 980), ticklabels = c(0,50,100, 980, 1000)) + # BREAK
  #scale_y_continuous(breaks = c(0,50,100, 980, 1000), labels = c(0,50,100, 980, 1000)) + 
  theme(axis.text.x = element_text(angle = 0, size = 9),
        axis.text.y = element_text(size = 10),
        # Remove labels on the right side
        axis.text.y.right = element_blank()) +
  
  # Sample size
  stat_summary(fun.data = n_fun, geom = "text",
               aes(group = G1), hjust = 0.8,
               position = position_dodge(0.9), size = 3) +
  
  # # Wilcoxon test mean comparaison
  # Use the prepared table of test results as data for the geom
  geom_text(aes(label = Sig, y = max(df$G2) + 3), size = 3,
            data = WC_tests)+
  #  y = 0
  geom_hline(yintercept = 0, linetype = "dashed", color = "gray") +
  
  # Mean and sd
  geom_text(data = mean_sd, aes(x = G1, y = 990,
                                label = paste("Mean(SD):",
                                              round(rmean, 2),
                                              "(",
                                              round(rsd, 2),
                                              ")")),
            color = "black")

创建于 2024-03-18,使用 reprex v2.0.2


0
投票

您只需添加

axis.ticks.y.right = element_blank(), axis.line.y.right = element_blank()
theme()
通话中:

© www.soinside.com 2019 - 2024. All rights reserved.