简体   繁体   English

ggplot2 中的分组条形图

[英]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.

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