r - 用 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")
解决方案
这是我所做的主要更改的列表:
我通过放大图表缩小了 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))
推荐阅读
- node.js - expresscart 安装错误 npm ERR!在 fiber@2.0.0 安装脚本中失败
- python - 具有多个循环的 Python 列表创建
- python - python模式装饰器中的奇怪类行为
- c - 在 freopen() 之后是否需要 fclose() 两个文件?
- reactjs - 未正确显示图标
- php - 如何将关联数组导出为 CSV?
- amazon-web-services - AWS Data Pipeline 无法验证 S3 访问 [权限警告]
- reactjs - react hooks 引用值如何传递给依赖数组?
- reactjs - react js中登录或注册后如何推送用户到首页?
- objective-c - 如何修复''预期';' 在顶级声明符之后”和“未知类型名称‘类’?