R语言 使用ggboxplot()添加自定义刻度和具有自定义颜色的标签y轴

whitzsjs  于 2023-04-03  发布在  其他
关注(0)|答案(2)|浏览(450)

我有一个箱线图,我已经使用ggboxplot创建。在它上面,我有一个水平线在标记y=50。我希望能够添加一个标签在y=50,x=0说“50”在绿色色匹配线。目前,标签重叠的轴线和箱线图已经向右移动。有什么建议吗?

问题

当前编码
symnum.args <- list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1), symbols = c("", "", "", "", "ns"))

my_comparisons<- list(c('BVR3', 'BVR4'), c('BVR6', 'BVR1'), c('BVR6', 'BVR2'), c('BVR6', 'BVR4'), c('BVR6', 'BVR5'))
plot <- ggboxplot(TPdat, x = "loc", y = "TP", 
          ylab = "Median Annual Total Phosphorus (\U00B5g/L)", xlab = "Sampling Site", 
          outlier.shape=NA, ylim=c(0, 925)) +
          scale_x_discrete(labels=c('1','2','3','4','5','6')) +
  theme(axis.text=element_text(size=16, family='Calibri'),
        axis.title=element_text(size=16, family='Calibri')) +
  geom_hline(yintercept=50, col='dark green', linetype=2) +
  coord_cartesian(clip='off', ylim=c(0, NA)) +
  annotate("text", x=0, y=50, label="50", colour='dark green', size=5) +
  stat_compare_means(comparisons=my_comparisons, label='p.signif', 
                     symnum.args = symnum.args, 
                     tip.length = c(0.01,0.01,0.01,0.01,0.01), 
                     label.y=c(800, 825, 850, 875, 900)) +
  stat_compare_means(label.y=930, family='Calibri', size=5)
Dataframe 头
structure(list(loc = structure(c(1L, 1L, 1L, 1L, 1L, 1L), levels = c("BVR1", 
"BVR2", "BVR3", "BVR4", "BVR5", "BVR6"), class = "factor"), TP = c(76L, 
71L, 86L, 48L, 58L, 73L)), row.names = c(NA, 6L), class = "data.frame")
fykwrbwg

fykwrbwg1#

基于这里建议的alistaire's answerChange color of specific tick in ggplot2,我们可以做:

library(ggplot2)

    ggplot(iris, aes(x=Species, y=Sepal.Length)) + 
      geom_boxplot() +
      scale_y_continuous(breaks=c(5,6,7,7.5,8), 
                         labels=expression("5", "6", "7", "7.5", "8")) +
      geom_hline(yintercept=7.5, col='dark green', linetype=2) +
      theme(axis.text.y = element_text(colour = c('black', 'black','black', 'dark green', 'black')))

我使用虹膜数据集来演示如何将一个特定的刻度标记标记为绿色。希望它能有所帮助
带有一个绿色的箱形图:

这个方法也适用于 ggpubr

ggboxplot(iris, x = "Species", y = "Sepal.Length") +
  scale_y_continuous(breaks=c(5,6,7,7.5,8), 
                     labels=expression("5", "6", "7", "7.5", "8")) +
  geom_hline(yintercept=7.5, col='dark green', linetype=2) +
  theme(axis.text.y = element_text(colour = c('black', 'black','black', 'dark green', 'black')))
dphi5xsq

dphi5xsq2#

正如@juan_bzt的回答一样,添加注解的最简单方法是将其添加为(可能是额外的)轴中断。这样做可以避免与轴线重叠,并且不会影响比例范围。
然而,除了将颜色向量传递给element_textcolor参数的选项之外,第二个选项是使用ggtext::element_markdown,它也通过一些HTML进行样式化。CSS和/或markdown。在下面的代码中,我将ifelse传递给scale_y_continuouslabels参数,该参数将y=50处的断点 Package 在绿色色的span标记中:

library(ggpubr)
library(ggtext)

ggboxplot(TPdat,
  x = "loc", y = "TP",
  ylab = "Median Annual Total Phosphorus (\U00B5g/L)", xlab = "Sampling Site",
  outlier.shape = NA, ylim = c(0, 925)
) +
  scale_x_discrete(labels = c("1", "2", "3", "4", "5", "6")) +
  theme(
    axis.text = element_text(size = 16, family = "Calibri"),
    axis.title = element_text(size = 16, family = "Calibri")
  ) +
  geom_hline(yintercept = 50, col = "dark green", linetype = 2) +
  coord_cartesian(clip = "off", ylim = c(0, NA)) +
  scale_y_continuous(
    labels = ~ ifelse(
      .x == 50,
      paste0("<span style='color: \"dark green\"'>", .x, "</span>"),
      as.character(.x)
    )
  ) +
  stat_compare_means(
    comparisons = my_comparisons, label = "p.signif",
    symnum.args = symnum.args,
    tip.length = c(0.01, 0.01, 0.01, 0.01, 0.01),
    label.y = c(800, 825, 850, 875, 900)
  ) +
  stat_compare_means(label.y = 930, family = "Calibri", size = 5) +
  theme(axis.text.y = ggtext::element_markdown())

相关问题