如何在R中生成具有4个组的条形图?

时间:2019-04-09 18:10:17

标签: r data-visualization

请帮助我如何生成如图所示的四组条形图?Example Chart

1 个答案:

答案 0 :(得分:0)

没有提供数据,所以我创建了一个虚拟数据。我在下面创建了一个用例,否则有关于SO的解决方案,例如Simplest way to do grouped barplot

library(tidyverse)

# Data
trees <- data.frame(age = sample(c("Old", "Adult", "Young"), 
                                 size = 20, replace = TRUE, prob = c(.5, .25, .25)), 
                    tree = factor(sample(1:5, size = 20, replace = TRUE)), 
                    circumference = rnorm(n = 20, mean = 60, sd = 15))

trees %>% 
  ggplot(aes(x = age, y = circumference, fill = tree)) + 
  geom_col(position = "dodge") + 
  coord_flip()

enter image description here