[英]Grouped bar chart in ggplot2
I want to replicate this graph (with faceting between groups)我想复制这个图(组之间有分面)
(facetting as done in) enter image description here This is my try:
(分面完成)在此处输入图像描述这是我的尝试:
library (ggplot2)
data<- data.frame(
d = rep(LETTERS[21:26], 10),
val = rnorm (60),
c = rep(LETTERS[1:10], each = 6)
)
ggplot(data, aes(c, val)) +
geom_bar(stat = 'identity', aes(fill = val), position = "dodge") +
facet_grid(data[, 1] ~ .)
and graphs are arranged vertically instead of the dodged position.和图形垂直排列而不是躲避位置。 What should I do?
我该怎么办?
Thank you in advanced.先谢谢了。
I think you don't need facet_grid()
, just fill by 'd'
.我认为您不需要
facet_grid()
,只需填写'd'
。
ggplot(data, aes(c, val)) +
geom_bar(stat = 'identity', aes(fill = d), position = "dodge")
As per OP's comment we can also use facet_grid(cols = vars(d))
.根据OP的评论,我们还可以使用
facet_grid(cols = vars(d))
。
ggplot(data, aes(c, val)) +
geom_bar(stat = 'identity', aes(fill = val), position = "dodge") +
facet_grid(cols = vars(d)) # or facet_grid(. ~ d)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.