【问题标题】:how to plot summarized data in ggplot如何在ggplot中绘制汇总数据
【发布时间】:2019-12-11 12:59:40
【问题描述】:

我有一个数据集,我想通过计算 2 列的比率来总结。但是,我还需要通过数据集的不同“切割”来计算这个比率。即总体数据的比率、按年份的比率、按类型的比率等。
我还需要将每个比率计算放在一个条形图中。

我想知道的是,我是否可以绘制所有这些条形图,而无需先创建单独的汇总分组数据集。

例如,现在,在我将它发送到 ggplot 之前,我首先使用 group_by/summarize 对我的数据来计算比率。然后我将它发送到ggplot。

Chart1 <- data %>% group_by(cut1) %>% summarise(ratio=sum(column1)/sum(column2))
ggplot(Chart1, aes(x=cut1, y=ratio)) + geom_bar(stat='identity', fill = "tomato2")

对于图表 2 和图表 3,我再次做同样的事情

Chart2 <- data %>% group_by(cut2) %>% summarise(ratio=sum(column1)/sum(column2))
ggplot(Chart2, aes(x=cut2, y=ratio)) + geom_bar(stat='identity', fill = "tomato2")
Chart3 <- data %>% group_by(cut3) %>% summarise(ratio=sum(column1)/sum(column2))
ggplot(Chart3, aes(x=cut3, y=ratio)) + geom_bar(stat='identity', fill = "tomato2")

还有其他方法可以做到这一点吗?最初,我想有一种方法可以只创建一次比率,然后我可以多次使用它(类似于 excel 数据透视表中的计算字段)。有没有比上述方法更好的方法?

另外,如果分别总结每个比率是最好的方法,我该如何制作分面图?例如,我可能想做一个比率方面的切割 1 和切割 2

编辑:更多信息,例如使用创建的数据:

c1 <- c('a','a','a', 'b','b', 'b', 'c','c','c')
c2 <- c('aa','aa','aa', 'bb','bb', 'bb', 'cc','cc','cc')
v1 <-c(1,2,3,4,5,6,7,8,9)
v2<-c(9,8,7,6,5,4,3,2,1)
mydata <-data.frame(c1,c2,v1,v2)

Chart1 <- mydata %>% group_by(c1) %>% summarise(ratio=sum(v1)/sum(v2))
ggplot(Chart1, aes(x=c1, y=ratio)) + geom_bar(stat='identity', fill = "tomato2") + theme(axis.text.x=element_text(angle=90))

我想要的结果是在绘制数据之前了解如何最好地汇总数据。我是否需要通过每个分组单独总结每个计算,还是有更简单的方法? 对于上面的示例,如果我想计算比率并按 c1 分组,然后创建另一个比率图表并按 c2 分组,然后按 c3 分组....我需要做 3 种不同的聚合吗?

【问题讨论】:

  • 我们能看到数据和预期的输出吗?
  • 如果您的每个“剪辑”只是按特定列分组,并且这些列都包含相同的类型(或可以强制为相同的类型),我认为您可以收集这些列,让 x = 值,并按键分面。
  • @Sada93 用一个例子编辑了我的帖子,如果有帮助的话。乔,不会撒谎的。我还是 R 新手,所以不太熟悉你的建议,但会仔细研究并尝试理解它。
  • @semidevil,我会输入我认为现在可以使用的内容,如果到那时没有其他人回答,我会在几分钟后发布。

标签: r ggplot2 dplyr


【解决方案1】:

这能实现你想要的吗?

library(tidyverse)

c1 <- c('a','a','a', 'b','b', 'b', 'c','c','c')
c2 <- c('aa','aa','aa', 'bb','bb', 'bb', 'cc','cc','cc')
v1 <-c(1,2,3,4,5,6,7,8,9)
v2<-c(9,8,7,6,5,4,3,2,1)
mydata <-data.frame(c1,c2,v1,v2)

Chart1 <- mydata %>% 
  gather(key = 'cuts', value = 'categories', -(v1:v2)) %>% 
  group_by(cuts, categories) %>% 
  summarise(ratio=sum(v1)/sum(v2))

# This lets you facet them onto the same chart, 
#   but that doesn't really make sense,
#   since the cuts will have different x axes
ggplot(Chart1, aes(x=categories, y=ratio)) + 
  geom_bar(stat='identity', fill = "tomato2") +
  facet_grid(cuts~.) +
  theme(axis.text.x=element_text(angle=90))

# This lets you make each plot separately
Chart1 %>% 
  filter(cuts == 'c1') %>% 
  ggplot(aes(x=categories, y=ratio)) + 
  geom_bar(stat='identity', fill = "tomato2") +
  theme(axis.text.x=element_text(angle=90))

# Use a for loop to save all of the plots to files
for(i in 1:(length(mydata)-2)){
  p <- 
    Chart1 %>% 
    filter(cuts == names(mydata)[[i]]) %>% 
    ggplot(aes(x=categories, y=ratio)) + 
    geom_bar(stat='identity', fill = "tomato2") +
    theme(axis.text.x=element_text(angle=90))


  ggsave(paste0("myPlot",i,".png"), plot = p)
}

唯一我不确定的是,如果不同的切口在 x 轴上的值不同,如何刻面。如果您只想将它​​们堆叠在一起,可以使用 gridExtra 包:

library(gridExtra)

plot1 <- Chart1 %>% 
  filter(cuts == 'c1') %>% 
  ggplot(aes(x=categories, y=ratio)) + 
  geom_bar(stat='identity', fill = "tomato2") +
  theme(axis.text.x=element_text(angle=90))

plot2 <- Chart1 %>% 
  filter(cuts == 'c2') %>% 
  ggplot(aes(x=categories, y=ratio)) + 
  geom_bar(stat='identity', fill = "tomato2") +
  theme(axis.text.x=element_text(angle=90))

grid.arrange(plot1, plot2, ncol=1, nrow = 2)

【讨论】:

  • 您可以在构面之间改变 x 轴。请参阅scales 参数。
  • 是的,只是不确定@semidevil 希望它看起来如何。如果有 4 个或更多切割,都使用 facet 在同一个 ggplot 上,但都具有不同的 x 轴,我认为这看起来不太理想。
  • 谢谢大家。我相信这是我需要的。只需要注意这个“聚集”声明!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-17
  • 2021-08-15
  • 2021-12-13
  • 2011-07-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多