在r r中使用GGPLOT打印样本大小在Boxplot边界外

发布于 2025-02-07 15:02:22 字数 994 浏览 1 评论 0原文

我想在我的盒装上显示每个物种的样本大小,但是情节杂乱无章。有没有办法在标题(我突出显示的红色区域中使用样本尺寸)时显示图块“外部”的样本大小?

df <- data.frame(year = rep(c(2019, 2020), each = 10),
                 month = rep(c("March", "October"), each = 1), 
                 site = rep(c("1", "2", "3", "4", "5"), each = 2),
                 type = rep(c("baitfish", "shark"), each = 1),
                 salinity = sample(x = 20:35, size  = 20, replace = TRUE),
                 num = sample(x = 0:10, size  = 20, replace = TRUE))

count_by_row <- data.frame(df[rep(row.names(df), df$num), 1:5])
    
    
    ggplot(data=subset(count_by_row, !is.na(salinity)), aes(x = reorder(type, -salinity, FUN = median), y = salinity)) +
      geom_boxplot(outlier.shape = 1, outlier.size = 2) +
      coord_flip() +
      xlab("") +
      ylab("salinity (PSU)")

I'd like to display the sample size of each species on my boxplot, but the plot is fairly cluttered. Is there a way to display the sample size on the "outside" of the plot with "N" as the header (sample size should go in the red area I highlighted)?

enter image description here

df <- data.frame(year = rep(c(2019, 2020), each = 10),
                 month = rep(c("March", "October"), each = 1), 
                 site = rep(c("1", "2", "3", "4", "5"), each = 2),
                 type = rep(c("baitfish", "shark"), each = 1),
                 salinity = sample(x = 20:35, size  = 20, replace = TRUE),
                 num = sample(x = 0:10, size  = 20, replace = TRUE))

count_by_row <- data.frame(df[rep(row.names(df), df$num), 1:5])
    
    
    ggplot(data=subset(count_by_row, !is.na(salinity)), aes(x = reorder(type, -salinity, FUN = median), y = salinity)) +
      geom_boxplot(outlier.shape = 1, outlier.size = 2) +
      coord_flip() +
      xlab("") +
      ylab("salinity (PSU)")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林很绿却致人迷途 2025-02-14 15:02:22

您可以在增加正确的保证金后添加自定义注释:

ggplot(data=subset(count_by_row, !is.na(salinity)), 
       aes(y = reorder(type, -salinity, FUN = median), x = salinity)) +
  geom_boxplot(outlier.shape = 1, outlier.size = 2, orientation = "y") +
  coord_cartesian(clip = "off") +
  annotation_custom(grid::textGrob(c("N", table(count_by_row$type)),
                                   x = 1.1, y = c(0.9, 0.28, 0.72),
                                   gp = grid::gpar(cex = 1.5))) +
  ylab("") +
  xlab("salinity (PSU)") +
  theme_bw(base_size = 20) +
  theme(plot.margin = margin(20, 100, 20, 20))

”在此处输入图像说明”

You could add a custom annotation after increasing the right margin:

ggplot(data=subset(count_by_row, !is.na(salinity)), 
       aes(y = reorder(type, -salinity, FUN = median), x = salinity)) +
  geom_boxplot(outlier.shape = 1, outlier.size = 2, orientation = "y") +
  coord_cartesian(clip = "off") +
  annotation_custom(grid::textGrob(c("N", table(count_by_row$type)),
                                   x = 1.1, y = c(0.9, 0.28, 0.72),
                                   gp = grid::gpar(cex = 1.5))) +
  ylab("") +
  xlab("salinity (PSU)") +
  theme_bw(base_size = 20) +
  theme(plot.margin = margin(20, 100, 20, 20))

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文