ggplot2的坐标轴主要由映射aes确定,expand_limits和coord_cartesian虽然可以调整坐标轴刻度,但对柱形图不大适用。不少使用者对扩展柱形图Y轴坐标很烦恼,有人从源代码层面对开发者提出要求,但似乎Hadley对这些要求无动于衷。其实根本不用动源代码就有简单的解决办法:
library(ggplot2)
dtx <- data.frame(label = LETTERS[1:6], mean = sample(80:100, 6), sd = sample(1:10,
6))
p <- ggplot(dtx, aes(x = label, y = mean, ymin = mean - sd, ymax = mean + sd)) +
theme_bw()
p <- p + geom_bar(stat = "identity", width = 0.6, fill = "gray", color = "black") +
geom_errorbar(width = 0.3)
p
p + scale_y_continuous(expand = c(0, 0))
p + scale_y_continuous(expand = c(0, 0)) + geom_blank(aes(y = (mean + sd) *
1.2))
p + scale_y_continuous(expand = c(0, 0)) + geom_blank(aes(y = 120))
p + scale_y_continuous(expand = c(0, 0)) + geom_text(aes(y = 120), label = "")
- 第1图是ggplot2默认的柱形图Y轴坐标
- 第2图使用了expand=c(0,0)设置,最大数据与边框重叠,这是问题所在
- 第3-5图是解决办法,可以使用geom_blank或geom_text
作者: ZGUANG@LZU
Created: 2017-10-22 日 10:51
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)