简体   繁体   English

分组条形图,R中的ggplot2,需要一次显示一个观察

[英]Grouped bar chart, ggplot2 in R, need to display one observation at a time

I have a dataset that looks like the following: 我有一个如下所示的数据集:

df <- data.frame(Name=rep(c('Sarah', 'Casey', 'Mary', 'Tom'), 3), 
                 Scale=rep(c('Scale1', 'Scale2', 'Scale3'), 4), 
                 Score=sample(1:7, 12, replace=T))

I am trying to create a barchat in ggplot2 that currently looks like this: 我想在ggplot2中创建一个barchat,目前看起来像这样:

ggplot(df, aes(x=Name, y=Score, fill=Scale)) + geom_bar(stat='identity', position='dodge') +
  coord_flip() +
  scale_y_continuous(breaks=seq(0, 7, 1), limits = c(0, 7)) +  
  scale_x_discrete() +    
  scale_fill_manual(values=c('#253494', '#2c7fb8', '#000000')) + 
  theme(panel.background = element_blank(),
        legend.position = 'right',  
        axis.line = element_line(),  
        axis.title = element_blank(),    
        axis.text = element_text(size=10)) 

However, I only want to show one observation (one Name) at a time. 但是,我只想一次显示一个观察(一个名称)。 Is this possible to do without creating a ton of separate datasets, one for each person? 这可能不会创建大量单独的数据集,每个人一个? I would like the end result to look like the example below, where I can just iterate through the names to produce a separate plot for each, or some similar process. 我希望最终结果看起来像下面的例子,我可以遍历名称,为每个或类似的过程生成一个单独的图。

# Trying to avoid creating separate datasets, but for the sake of the example:
df2 <- data.frame(Name=rep(c('Sarah'), 3), 
                 Scale=c('Scale1', 'Scale2', 'Scale3'), 
                 Score=sample(1:7, 3, replace=T))

ggplot(df2, aes(x=Name, y=Score, fill=Scale)) + geom_bar(stat='identity', position='dodge') +
  coord_flip() +
  scale_y_continuous(breaks=seq(0, 7, 1), limits = c(0, 7)) +  
  scale_x_discrete() +    
  scale_fill_manual(values=c('#253494', '#2c7fb8', '#000000')) + 
  theme(panel.background = element_blank(),
        legend.position = 'right',  
        axis.line = element_line(),  
        axis.title = element_blank(),    
        axis.text = element_text(size=10)) 

Since your data is already tidy ie. 由于您的数据已经整洁,即。 in long format, you can use facet_wrap as suggested and set the scales as "free" thus creating facets with your different Name groups. 在长格式中,您可以按照建议使用facet_wrap ,并将比例设置为"free"从而创建具有不同Name组的facet。

df %>% ggplot(aes(y = Score, x = Name)) +
  geom_bar(stat = "identity", aes(colour = Scale, fill = Scale),
           position = "dodge") +
  coord_flip() + 
  facet_wrap(~Name, scales = "free")

You can get rid of the facet labels or the axis labels depending which you prefer. 您可以根据自己的喜好去掉刻面标签或轴标签。

EDIT: in response to comment. 编辑:回应评论。 You can use the same data frame to create seperate plots by just piping a filter in at the start, hence, 您可以使用相同的数据框来创建单独的图,只需在开始时输入一个过滤器,因此,

df %>% 
  filter(Name == "Sarah") %>%
  ggplot(aes(y = Score, x = Name)) +
  geom_bar(stat = "identity", aes(colour = Scale, fill = Scale),
           position = "dodge") +
  coord_flip()

Since you are using Rmarkdown you could throw a for loop around that to plot all the names 由于你正在使用Rmarkdown,你可以在它周围抛出一个for循环来绘制所有的名字

for(i in c("Sarah", "Casey", "Mary", "Tom")){
  df %>% 
    filter(Name == i) %>%
    ggplot(aes(y = Score, x = Name)) +
    geom_bar(stat = "identity", aes(colour = Scale, fill = Scale),
             position = "dodge") +
    coord_flip()
}

If you want to arrange all these into a group you can use ggpubr::ggarrange to place all the plots into the same object. 如果要将所有这些安排到一个组中,可以使用ggpubr::ggarrange将所有绘图放在同一个对象中。

  • facet_grid(.~Name) facet_grid(。〜名称)

Maybe somehow implement this, it'll plot them all, but should do so in individual plots. 也许以某种方式实现这一点,它将绘制所有这些,但应该在个别情节中这样做。

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

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