首页 > 解决方案 > 用 geom_bar 打破 ggplot2 中的 y 轴

问题描述

我很难处理这个情节。ANI> 96 中的值的高度使得难以阅读红色和蓝色百分比文本。通过查看 StackOverflow 中其他帖子的答案,我未能打破 y 轴。

有什么建议么?

谢谢。

library(data.table)
library(ggplot2)

dt <- data.table("ANI"= sort(c(seq(79,99),seq(79,99))), "n_pairs" = c(5, 55, 13, 4366, 6692, 59568, 382873, 397996, 1104955, 282915,
                 759579, 261170, 312989, 48423, 120574, 187685, 353819, 79468, 218039, 66314, 41826, 57668, 112960, 81652, 28613,
                 64656, 21939, 113656, 170578, 238967, 610234, 231853, 1412303, 5567, 4607268, 5, 14631942, 0, 17054678, 0, 3503846, 0),
                 "same/diff" = rep(c("yes","no"), 21))

for (i in 1:nrow(dt)) {
  if (i%%2==0) {
    next
  }
  total <- dt$n_pairs[i] + dt$n_pairs[i+1]
  dt$total[i] <- total
  dt$percent[i] <- paste0(round(dt$n_pairs[i]/total *100,2), "%")
  dt$total[i+1] <- total
  dt$percent[i+1] <- paste0(round(dt$n_pairs[i+1]/total *100,2), "%")
}

ggplot(data=dt, aes(x=ANI, y=n_pairs, fill=`same/diff`)) +
  geom_text(aes(label=percent), position=position_dodge(width=0.9), hjust=0.75, vjust=-0.25) +
  geom_bar(stat="identity") + scale_x_continuous(breaks = dt$ANI) +
  labs(x ="ANI", y = "Number of pairs", fill = "Share one common species taxonomy?") + 
  theme_classic() + theme(legend.position="bottom")

标签: rggplot2geom-baryaxis

解决方案


这是我所做的主要更改的列表:

  • 我通过放大图表缩小了 y 轴coord_cartesian(由 调用coord_flip)。

  • coord_flip还应该通过切换 x 和 y 来提高图表的可读性。我不知道开关是否适合您。

  • 现在position_dodge,按预期工作:两个条彼此相邻,标签位于顶部(在本例中位于左侧)。

  • geom_bar之前geom_text进行了设置,以便文本始终位于图表中的条形之前。

  • 我设置scale_y_continuous更改 y 轴的标签(在图表中 x 轴因为开关)以提高零点的可读性。

ggplot(data=dt, aes(x = ANI, y = n_pairs, fill = `same/diff`)) +
    geom_bar(stat = "identity", position = position_dodge2(width = 1), width = 0.8) + 
    geom_text(aes(label = percent), position = position_dodge2(width = 1), hjust = 0, size = 3) +
    scale_x_continuous(breaks = dt$ANI) +
    scale_y_continuous(labels = scales::comma) +
    labs(x ="ANI", y = "Number of pairs", fill = "Share one common species taxonomy?") + 
    theme_classic() + 
    theme(legend.position = "bottom") +
    coord_flip(ylim = c(0, 2e6))

在此处输入图像描述


编辑

像这样的列和标签是堆叠的,但标签永远不会重叠。

ggplot(data=dt, aes(x = ANI, y = n_pairs, fill = `same/diff`)) +
    geom_bar(stat = "identity", width = 0.8) + 
    geom_text(aes(label = percent,
                                hjust = ifelse(`same/diff` == "yes", 1, 0)), 
                        position = "stack", size = 3) +
    scale_x_continuous(breaks = dt$ANI) +
    scale_y_continuous(labels = scales::comma) +
    labs(x ="ANI", y = "Number of pairs", fill = "Share one common species taxonomy?") + 
    theme_classic() + 
    theme(legend.position = "bottom") +
    coord_flip(ylim = c(0, 2e6))

在此处输入图像描述

或者,您可以避免标签与 重叠check_overlap = TRUE,但有时其中一个标签不会显示。

ggplot(data=dt, aes(x = ANI, y = n_pairs, fill = `same/diff`)) +
    geom_bar(stat = "identity", width = 0.8) + 
    geom_text(aes(label = percent), hjust = 1, position = "stack", size = 3, check_overlap = TRUE) +
    scale_x_continuous(breaks = dt$ANI) +
    scale_y_continuous(labels = scales::comma) +
    labs(x ="ANI", y = "Number of pairs", fill = "Share one common species taxonomy?") + 
    theme_classic() + 
    theme(legend.position = "bottom") +
    coord_flip(ylim = c(0, 2e6))

在此处输入图像描述


推荐阅读