如何一起生成多个相似的ggplots

时间:2019-07-31 20:38:38

标签: r loops ggplot2

大多数可用的答案是将多个ggplots组合在一起。我正在尝试一起生成多个ggplots。 我分别为多个变量生成了条形图,然后使用“ ggarrange”将它们组合在一起。

创建样本数据集

y0 = c(1,0,1,1,0,1,0,1,1,0)
x1 = c("A","B","A","B","A","B","A","B","A","A")
x2 = c(1,2,2,1,1,2,2,1,2,2)
x3 = c("A","B","C","D","E","E","D","C","B","A")
df<- data.frame(y0,x1,x2,x3);
df

计算x1变量的统计信息

x1_count <- df %>% 
group_by(x1) %>%
summarise(Count=n(), Num=sum(y0))  %>%
mutate(Rate=Num/Count*100.0)

为x1可用生成ggplot

A<- ggplot(x1_count, aes(x=x1, y=Rate)) +
geom_bar(width=0.5, stat="identity") +
ggtitle('Rate by x1') +
xlab("x1") +
ylab("Rate (%)") +
theme(plot.title = element_text(hjust = 0.5),legend.position='bottom') 

计算x2变量的统计信息

x2_count <- df %>% 
  group_by(x2) %>%
  summarise(Count=n(), Num=sum(y0))  %>%
  mutate(Rate=Num/Count*100.0)

为x2可用生成ggplot

B<- ggplot(x2_count, aes(x=x2, y=Rate)) +
  geom_bar(width=0.5, stat="identity") +
  ggtitle('Rate by x2') +
  xlab("x2") +
  ylab("Rate (%)") +
  theme(plot.title = element_text(hjust = 0.5),legend.position='bottom') 
B

将它们组合在一起

figure1 <- ggarrange(A,B,ncol = 2, nrow = 1)
figure1

我正在尝试生成ggplots A和B,并将计算与之关联在一起,而不是分别进行。

4 个答案:

答案 0 :(得分:2)

您可以创建一个函数。

R函数具有以下结构:

name <- function(argument) {
    what the function does
}

考虑到绘图工作流程正在创建另一个变量,然后对其进行绘图,您可以将函数参数设置为原始数据帧df,然后使函数使用dplyr和{ {1}}命令:

ggplot2

答案 1 :(得分:1)

考虑从宽到长的reshape数据(大多数分析方法的首选格式),然后使用aggregate进行 Rate 计算,并使用ggplot facet_wrap

reshape

rdf <- reshape(transform(df, x2 = as.character(x2)), 
               varying = list(names(df)[-1]), v.names = "Value",
               times = names(df)[-1], timevar = "Categ",
               new.row.names = 1:1E3, direction="long")
rdf

#    y0 Categ Value id
# 1   1    x1     A  1
# 2   0    x1     B  2
# 3   1    x1     A  3
# 4   1    x1     B  4
# 5   0    x1     A  5
# 6   1    x1     B  6
# 7   0    x1     A  7
# 8   1    x1     B  8
# 9   1    x1     A  9
# 10  0    x1     A 10
# 11  1    x2     1  1
# 12  0    x2     2  2
# 13  1    x2     2  3
# 14  1    x2     1  4
# 15  0    x2     1  5
# 16  1    x2     2  6
# 17  0    x2     2  7
# 18  1    x2     1  8
# 19  1    x2     2  9
# 20  0    x2     2 10
# 21  1    x3     A  1
# 22  0    x3     B  2
# 23  1    x3     C  3
# 24  1    x3     D  4
# 25  0    x3     E  5
# 26  1    x3     E  6
# 27  0    x3     D  7
# 28  1    x3     C  8
# 29  1    x3     B  9
# 30  0    x3     A 10

aggregate

agg_raw <- aggregate(y0 ~ Categ + Value, rdf, 
                    function(x) c(Num=sum(x), Count=length(x),
                                  Rate=sum(x)/length(x) * 100.00))
agg_df <- do.call(data.frame, agg_raw)
agg_df <- setNames(agg_df, gsub("y0.", "", names(agg_df)))
agg_df

#   Categ Value Num Count Rate
# 1    x1     A   3     6   50
# 2    x3     A   1     2   50
# 3    x1     B   3     4   75
# 4    x3     B   1     2   50
# 5    x2     1   3     4   75
# 6    x2     2   3     6   50
# 7    x3     C   2     2  100
# 8    x3     D   1     2   50
# 9    x3     E   1     2   50

ggplot + facet_wrap

ggplot(agg_df, aes(x=Value, y=Rate)) +
  geom_bar(width=0.5, stat="identity") +
  ggtitle('Rate by x1') +
  xlab("x1") +
  ylab("Rate (%)") +
  theme(plot.title = element_text(hjust = 0.5),legend.position='bottom') +
  facet_wrap(~Categ, ncol=2, scales="free_x")

Plot Output

答案 2 :(得分:1)

与Parfait完全相同的解决方案,但使用了tidyverse

中更多的现代功能

数据

library(tidyverse)
y0 = c(1,0,1,1,0,1,0,1,1,0)
x1 = c("A","B","A","B","A","B","A","B","A","A")
x2 = c(1,2,2,1,1,2,2,1,2,2)
x3 = c("A","B","C","D","E","E","D","C","B","A")
df<- data.frame(y0,x1,x2,x3)

重塑和聚合

res<-
df %>% 
  tidyr::gather("Categ", "Value", x1, x2, x3) %>% 
  group_by(Categ, Value) %>% 
  summarise(Num=sum(y0),
            Count=length(y0),
            Rate=sum(y0)/length(y0) * 100.00)

情节

  ggplot(res, aes(x=Value, y=Rate)) +
  geom_bar(width=0.5, stat="identity") +
  ggtitle('Rate by x1') +
  xlab("x1") +
  ylab("Rate (%)") +
  theme(plot.title = element_text(hjust = 0.5),legend.position='bottom') +
  facet_wrap(~Categ, ncol=2, scales="free_x")

答案 3 :(得分:1)

如果要重复执行同一任务,则可以创建一个函数,只需将要计算或图形化的值放入该函数中即可。

为了进行计算,您可以:

counting <- function(variable(x1 or x2)){ 
df %>% 
  group_by{{variable}} %>% 
  mutate(Rate=Num/Count*100.0)}

变量是放置特定变量的地方。接下来,您可以使用上面的函数创建一个函数来为每个变量生成ggplot:

graphic <- function(variable){ 
 counting({{variable}}) %>%
 ggplot(aes(x = {{variable}}, y = Rate)) +
  geom_bar(width = 0.5, stat = "identity") +
  ylab("Rate (%)") +
  theme(plot.title = element_text(hjust = 0.5, 
        legend.postion = 'bottom')}

这样做之后,您可以通过执行以下操作为每个变量创建图形:

graphic(x1) +
 xlab("x1")

graphic(x2)

希望这能回答您的问题。