Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.3k views
in Technique[技术] by (71.8m points)

ggplot2 - Irregular Shaped Grids in R (ggplot)

I am using the R programming language. I made several plots in ggplot2 and now I am trying to arrange them on the same page:

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

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

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

In a previous post: Combining Different Types of Graphs Together (R) , someone (user "monkeytennis") showed me how to accomplish this using the "patchwork" library:

# install.packages('patchwork')
library(patchwork)

(Pie_2014_graph | Pie_2015_graph | Pie_total_graph) /
  (Bar_years_plot | Bar_total_plot) / 
  (ts_1 | ts_2)

enter image description here

This is perfect - the only problem is, I am using a computer that does not have access to the internet or a USB port. I am not able to install the "patchwork" library. Therefore, I am trying to solve the same problem using libraries such as "gridExtra" and "ggpubr".

I figured out how to use the basic syntax of ggpubr:

library(ggpubr)
 ggarrange(Pie_2014_graph, Pie_2015_graph , Pie_total_graph, Bar_total_plot, Bar_years_plot, ts_1, ts_2) 

enter image description here

But the formatting is not the same as before (as in patchwork) - I am trying to get all the pie charts on one line, all the barplots on one line and all the time series on one line.

I also tried this in gridExtra:

library(gridExtra)

grid.arrange(Pie_2014_graph, Pie_2015_graph , Pie_total_graph, Bar_total_plot, Bar_years_plot, ts_1, ts_2)

But this produces a very similar plot as the one earlier.

Can someone please show me how to recreate the "patchwork" answer using either ggpubr or gridExtra?

Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You almost got it with the gridExtra library. The philosophy behind the use of grid.arrange() and the | and operators of the patchwork library is very similar. Instead of | you set the nrow argument of grid.arrange() to 1 to keep the plots in the same row, and instead of you set the ncol argument to 1 to keep the plots in the same column (although by default ncol = 1).

Solution for your problem:

library(gridExtra)
g1 <- grid.arrange(Pie_2014_graph, Pie_2015_graph , Pie_total_graph, nrow = 1)
g2 <- grid.arrange(Bar_total_plot, Bar_years_plot, nrow = 1)
g3 <- grid.arrange(ts_1, ts_2, nrow = 1)
grid.arrange(g1, g2, g3, ncol = 1)

Plot result


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...