【问题标题】:How to generate multiple similar ggplots together如何一起生成多个相似的ggplots
【发布时间】:2019-12-09 10:20:09
【问题描述】:

大多数可用的答案是指将多个 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,以及与之相关的计算,而不是单独进行。

【问题讨论】:

  • 似乎您可能想要创建一个函数来进行汇总和绘图,然后您可以在循环中使用它,循环遍历变量的名称并返回绘图列表。虽然比您正在做的事情更简单(因为我没有做任何总结),但我写了一个使用函数和purrr::map() 循环here 的示例,您可能会觉得相关。

标签: r loops ggplot2


【解决方案1】:

您可以创建一个函数。

R 函数具有以下结构:

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

考虑到您的绘图工作流程正在创建一个附加变量然后对其进行绘图,您可以将您的函数参数设置为原始数据框df,然后使用dplyrggplot2 命令使函数对其进行处理:

myfunction <- function(df) {
  # creating new variable
  x_count <- df %>% 
  group_by(x) %>%
  summarise(Count=n(), Num=sum(y0)) %>%
  mutate(Rate=Num/Count*100.0)

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

  # showing the plot
  print(plotX)
}

【讨论】:

  • 嗨,我需要将 A 和 B 一起完成一个功能
  • 使用lapply 使用您的两个变量和我在回答中提出的函数创建一个循环。
  • 循环是在函数内还是函数外?
【解决方案2】:

如果您重复相同的任务,您可以创建一个函数,您只需将要计算的值或图形放入函数中。

对于您的计算,您可以:

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)

希望这能回答你的问题。

【讨论】:

    【解决方案3】:

    考虑将reshape 的数据从宽到长(大多数分析方法的首选格式),然后将aggregate 用于速率 计算,将ggplotfacet_wrap: p>

    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")
    

    【讨论】:

      【解决方案4】:

      与 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")
      

      【讨论】:

        猜你喜欢
        • 2021-04-26
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 2017-03-14
        • 1970-01-01
        • 2019-07-07
        相关资源
        最近更新 更多