【问题标题】:Combining Different Types of Graphs Together (R)将不同类型的图组合在一起 (R)
【发布时间】:2021-04-16 23:33:47
【问题描述】:

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

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)

我将这些数据汇总为不同类型的图表。例如:

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. 条形图:

    
    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")
    
    
  2. 时间序列图:


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 将所有这些图表整齐地排列到一个窗口中?

例如:

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

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

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

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

这样的?

非常感谢所有帮助。 谢谢

【问题讨论】:

  • patchwork 库可帮助您在灵活的布局中轻松组合不同的 ggplot 对象。
  • @RonakShah:谢谢你的回复!我只是再次复制并粘贴我的代码,我没有收到任何错误: Pie_2014 % 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("饼图 2014")
  • 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") #### 这将返回以下图:imgur.com/a/8fe3Uk1
  • 我现在就试试
  • @RonakShah:我刚刚重新启动了所有内容,并且图表正常工作。我在这里发布了我的完整代码和会话信息:shrib.com/#Leighton_RwaOoZ ...也许我使用的 R 版本有一些不同的东西可以使图表工作?感谢您的所有帮助!

标签: r ggplot2 plot dplyr data-visualization


【解决方案1】:

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

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

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 包将子图排列在一起非常简单。例如这样的事情会让你接近;

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

编辑:根据对非patchwork替代方案的要求,这是一个让您开始使用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

【讨论】:

  • 感谢您的回答!我正在使用没有互联网连接或 USB 端口的计算机。它只带有 r 和一些预装的软件包(cowplot、ggplot2)。我无法安装补丁。是否可以使用 ggplot2 中的 cowplot 或 facet_wrap 来做到这一点?非常感谢!
  • 现在,我正在看看是否可以使用“gridarrange”或“ggpubr”(ggarrange)来解决这个问题
  • 我到了这里: ggarrange(Pie_2014_graph, Pie_2015_graph , Pie_total_graph, Bar_total_plot, Bar_years_plot, ts_1, ts_2) ...现在我正在尝试像您一样修复格式。跨度>
  • 嗨@Noob,我已经更新了上面的答案,包括使用cowplot而不是拼凑而成的解决方案。希望对您有所帮助。
  • 有时间可以看看这个问题吗? stackoverflow.com/questions/65832784/… 谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
  • 2020-09-04
  • 2020-04-29
  • 2022-01-12
  • 1970-01-01
  • 2022-11-02
  • 2020-08-30
相关资源
最近更新 更多