R-使用par()创建ggplot图的网格-不能按预期工作 [英] R - Using par() to create a grid of ggplot plots - Not working as expected

查看:55
本文介绍了R-使用par()创建ggplot图的网格-不能按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些看起来像这样的数据

 #生成示例数据exampleData<-data.frame(Month = sample(1:5,500,replace = T),产品=样本(字母[1:10],500,替换= T),网站=样本(字母[1:5],500,替换= T),已使用=样本(1:100,500,替换= T))exampleData<-汇总(.〜月+产品+网站,数据= exampleData,总和)#合并所有重复项exampleData<-exampleData [order(exampleData $ Month,exampleData $ Product,exampleData $ Site,exampleData $ Used),] 

我想查看不同站点上不同产品的趋势,因此创建了此功能

 #Funciton检索有关产品和网站的信息productSiteInfo<-函数(p,s){return(exampleData [intersect(which(exampleData $ Product == p),which(exampleData $ Site == s)),])} 

为了使我的比较更加容易,我想制作一个线状图网格,其中该网格由所有站点上的特定产品的图组成.所以我尝试了这段代码

 #绘制数据产品<-唯一(exampleData $ Product)#所有产品prod<-sample(prods,1)#选择感兴趣的产品站点<-唯一(exampleData $ Site)#所有站点par(mfrow = c(3,2))#创建网格lapply(头(站点),功能(站点){#所有站点上的产品趋势图aDF<-productSiteInfo(产品,站点)ggplot()+geom_line(数据= aDF,aes(x =月,y =已使用),颜色=黑色")+xlab(月")+ylab("Units")+ggtitle(paste("Consumption of",prod,"at",site))}) 

但是它没有按预期工作.我没有地块的网格,而只是个别地块.我想知道为什么会这样,以及如何才能获得该网格.我的实际数据有大约10个产品和大约160个站点,因此它将比本示例大得多.

感谢您的帮助!

解决方案

之所以不起作用,是因为 ggplot 不遵守标准绘图的规则.通常使用 facet_grid facet_wrap 在网格中创建多个图,在其中您可以使用数据中的现有变量将数据集拆分为多个图.如果您的分组变量位于数据中,则绝对推荐使用此方法.

@ r2evans建议使用 grid.extra ,这也是将任何给定系列绘图分成小节的经典方法(类似于 cowplot ).但是,为方便起见,我建议使用正如您在此处所指出的,我只是将所有地物添加在一起,而我可以使用-删除地物/以将地块彼此重叠,依此类推

I have some data that looks like this

# Generate example data
exampleData <- data.frame(Month = sample(1:5, 500, replace = T),
                          Product = sample(LETTERS[1:10], 500, replace = T),
                          Site = sample(letters[1:5], 500, replace = T),
                          Used = sample(1:100, 500, replace = T))
exampleData <- aggregate(. ~ Month + Product + Site, data = exampleData, sum)      # Consolidating any duplicates
exampleData <- exampleData[order(exampleData$Month, exampleData$Product, exampleData$Site, exampleData$Used),]

I wanted to see trends in different products at different sites, so created this function

# Funciton to retrieve info about a product and site
productSiteInfo <- function(p, s) {
  return(exampleData[intersect(which(exampleData$Product == p), which(exampleData$Site == s)),])
}

To make my comparisons easier, I want to make a grid of line plots, where the grid consits of plots of a specific product at all the sites. So I tried this code

# Plotting the data
prods <- unique(exampleData$Product)  # All products
prod <- sample(prods,1)      # Select a product of interest
sites <- unique(exampleData$Site)     # All sites
par(mfrow=c(3,2))       # Create grid
lapply(head(sites), function(site) {      # Plot trend of prod at all sites
  aDF <- productSiteInfo(prod, site)
  ggplot() +
           geom_line(data = aDF, aes(x = Month, y = Used), color = "black") +
           xlab("Month") +
           ylab("Units") + 
           ggtitle(paste("Consumption of", prod, "at", site))
})

But it's not working as expected. I'm not getting a grid of plots, but just individual plots. I was wondering why that was, and what I can do to get that grid. My actual data has ~10 products and ~160 sites, so it's gonna be much larger than this example.

Thanks for the help!

解决方案

A reason why this doesn't work, is that ggplot doesn't adhere to rules of standard plots. Usually creating multiple plots in a grid is done using facet_grid or facet_wrap where you use an existing variable within your data to split the dataset into multiple plots. This approach is definitely recommended if your grouping variable resides within your data.

@r2evans suggested using grid.extra which is also a classic approach to arrange any given series of plots into subsections (similar to cowplot). However for what I'd call the ultimate convenience I'd suggest using patchwork and checking out their short well written guides. For your specific example it can be as simple as adding the plots together.

plots <- lapply(head(sites), function(site) {      # Plot trend of prod at all sites
  aDF <- productSiteInfo(prod, site)
  ggplot() +
           geom_line(data = aDF, aes(x = Month, y = Used), color = "black") +
           xlab("Month") +
           ylab("Units") + 
           ggtitle(paste("Consumption of", prod, "at", site))
})
library(patchwork)
library(purrr) #for reduce
reduce(plots, `+`)

As you note here I simply add together the plots, while I could use - to remove plots / to arrange plots above each other and so forth.

这篇关于R-使用par()创建ggplot图的网格-不能按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆