将不同类型的图组合在一起

将不同类型的图组合在一起

本文介绍了将不同类型的图组合在一起 (R)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何在 R 编程语言中将不同类型的图形组合在一起.假设我有以下数据:

I am trying to learn how to combine different types of graphs together in the R programming language. Suppose I have the following data:

library(dplyr)
library(ggplot2)

date= seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day")

var <- rnorm(731,10,10)


group <- sample( LETTERS[1:4], 731, replace=TRUE, prob=c(0.25, 0.22, 0.25, 0.25) )

data = data.frame(date, var, group)

data$year = as.numeric(format(data$date,'%Y'))
data$year = as.factor(data$year)

我总结了这些数据,制作了不同类型的图表.例如:

I summarized this data making different types of graphs. For example:

1) Pie Charts:

    ###Pie

    Pie_2014 <- data %>% filter((data$year == "2014"))
    Pie_2014 %>%
      group_by(group) %>%
      summarise(n = n())

    Pie_2014_graph = ggplot(Pie_2014, aes(x="", y=n, fill=group)) +
      geom_bar(stat="identity", width=1) +
      coord_polar("y", start=0) +ggtitle( "Pie Chart 2014")


    Pie_2015 <- data %>% filter((data$year == "2015"))
    Pie_2015 %>%
      group_by(group) %>%
      summarise(n = n())

    Pie_2015_graph = ggplot(Pie_2015, aes(x="", y=n, fill=group)) +
      geom_bar(stat="identity", width=1) +
      coord_polar("y", start=0) +ggtitle( "Pie Chart 2015")


    Pie_total = data %>%
      group_by(group) %>%
      summarise(n = n())

    Pie_total_graph = ggplot(data, aes(x="", y=n, fill=group)) +
      geom_bar(stat="identity", width=1) +
      coord_polar("y", start=0) +ggtitle( "Pie Chart Average")
  1. 条形图:

  1. Bar Plots:


Bar_years = data %>%
  group_by(year, group) %>%
  summarise(mean = mean(var))

Bar_years_plot = ggplot(Bar_years, aes(fill=group, y=mean, x=year)) +
    geom_bar(position="dodge", stat="identity") + ggtitle("Bar Plot All Years")

Bar_total = data %>%
  group_by(group) %>%
  summarise(mean = n())

Bar_total_plot = ggplot(Bar_total, aes(x=group, y=mean, fill=group)) +
  geom_bar(stat="identity")+theme_minimal() + ggtitle("Bar Plot Average")

  • 时间序列图:

  • Time Series Plots:

    
    New <- data %>%
      mutate(date = as.Date(date)) %>%
      group_by(group, month = format(date, "%Y-%m")) %>%
      summarise( Mean = mean(var, na.rm = TRUE), Count = n())
    
    #Plot
    ts_1 <- ggplot(New) +
      geom_line(aes(x=month, y=Mean, colour=group,group=1))+
      scale_colour_manual(values=c("red","green","blue", "purple"))+
      theme(axis.text.x = element_text(angle=90))  + ggtitle("time seres 1")
    
    ts_2 <- ggplot(New) +
      geom_line(aes(x=month, y=Count, colour=group,group=1))+
      scale_colour_manual(values=c("red","green","blue", "purple"))+
      theme(axis.text.x = element_text(angle=90)) + ggtitle("time seres 2")
    

    所有这些图表都能完美运行.现在我正在寻找一种更好的方式来呈现它们.我的问题:是否可以使用 R 和 ggplot2 将所有这些图形整齐地排列到一个窗口中?

    All these graphs work perfectly. Now I am looking for a better way to present them.My question: Is it possible to neatly arrange all these graphs into a window using R and ggplot2?

    例如:

    第 1 行:所有饼图(Pie_2014_graph、Pie_2015_graph、pie_total_graph)

    Row 1: All Pie Charts (Pie_2014_graph, Pie_2015_graph, pie_total_graph)

    第 2 行:所有条形图(Bar_years_plot、Bar_total_plot)

    Row 2: All Bar Graphs (Bar_years_plot, Bar_total_plot)

    第 3 行:所有时间序列图(ts_1、ts_2)

    Row 3: All Time Series Graphs (ts_1, ts_2)

    现在,我单独创建所有这些图形,将它们粘贴到 MS Paint 中并手动重新排列它们.

    Right now, I creating all these graphs individually, pasting them into MS Paint and manually rearranging them.

    有这样的吗?

    非常感谢所有帮助.谢谢

    All help is greatly appreciated.Thanks

    推荐答案

    您上面发布的代码失败,因为您尝试使用变量 n 但没有在 之后的任何位置分配数据>summarise(n = n()) 饼图数据的步骤.

    The code you posted above fails because you are trying to use the variable n but have not assigned the data anywhere after your summarise(n = n()) step for your pie chart data.

    您可以将汇总数据直接通过管道传输到 ggplot 中,否则您必须使用类似的方式分配中间步骤;

    You can either pipe the summarised data straight into ggplot or otherwise you must assign the intermediary steps with something like this;

    Pie_2014 <- data %>%
      filter((data$year == "2014")) %>%
      group_by(group) %>%
      summarise(n = n())
    
    Pie_2014_graph = ggplot(Pie_2014, aes(x="", y=n, fill=group)) +
      geom_bar(stat="identity", width=1) +
      coord_polar("y", start=0) +ggtitle( "Pie Chart 2014")
    
    
    Pie_2015 <- data %>%
      filter((data$year == "2015")) %>%
      group_by(group) %>%
      summarise(n = n())
    
    Pie_2015_graph = ggplot(Pie_2015, aes(x="", y=n, fill=group)) +
      geom_bar(stat="identity", width=1) +
      coord_polar("y", start=0) +ggtitle( "Pie Chart 2015")
    
    
    Pie_total = data %>%
      group_by(group) %>%
      summarise(n = n())
    
    Pie_total_graph = ggplot(Pie_total, aes(x="", y=n, fill=group)) +
      geom_bar(stat="identity", width=1) +
      coord_polar("y", start=0) +ggtitle( "Pie Chart Average")
    

    之后,使用 patchwork 包将子图排列在一起非常简单.例如这样的事情会让你接近;

    After that arranging the subplots together is pretty straightforward with the patchwork package. e.g. something like this will get you close;

    # combine plots
    
    # install.packages('patchwork')
    library(patchwork)
    
    (Pie_2014_graph | Pie_2015_graph | Pie_total_graph) /
      (Bar_years_plot | Bar_total_plot) /
      (ts_1 | ts_2)
    

    根据对非拼凑替代方案的要求,这里有一个让您开始使用cowplot的版本:

    Following request for a non-patchwork alternative, here is a version to get you started using cowplot:

    library(cowplot)
    
    # arrange subplots in rows
    top_row <- plot_grid(Pie_2014_graph, Pie_2015_graph, Pie_total_graph, nrow = 1)
    middle_row <- plot_grid(Bar_years_plot, Bar_total_plot)
    bottom_row <- plot_grid(ts_1, ts_2)
    
    # arrange our new rows into combined plot
    p <- plot_grid(top_row, middle_row, bottom_row, nrow = 3)
    p
    

    这篇关于将不同类型的图组合在一起 (R)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 09-05 20:54