繁体   English   中英

在 ggplot 中调整 geom_bar (position="dodge")

[英]Adjusting geom_bar (position=“dodge”) in ggplot

我想在 ggplot 中创建一个 2 个变量条形图,其中一个度量部分隐藏在另一个度量后面。 我可以在 Excel 中使用 Series Overlap 来完成并得到这个结果

使用 geom_bar (position="dodge") 将两个条并排放置。 有没有办法调整这个?

一些代码:

library (ggplot2)
library(reshape2)
x <- c(19, 18, 21, 19)
y <- c(17, 16, 18, 19)
z <- c("a", "b", "c", "d")

df <- melt (data.frame (x,y,z))

ggplot (df, aes(x=z, y=value, fill=variable)) + geom_bar (stat="identity", position ="dodge")

您可以通过指定position = position_dodge(...)来自定义闪避。

ggplot (df, aes(x=z, y=value, fill=variable)) + 
  geom_bar (stat="identity", position = position_dodge(width = 0.5))

在此处输入图片说明

我在上面的帮助下解决了它,但进一步调整:

ggplot (df, aes(x=z, y=value, fill=variable)) + 
  geom_bar (stat="identity", position = position_dodge2(width = 0.5, preserve = "single", padding = -0.5))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM