【问题标题】:Make multiple separate plots from single data frame in R从R中的单个数据框制作多个单独的图
【发布时间】:2013-07-26 05:15:10
【问题描述】:

我有一个大型数据集,我不想拆分它,因为它会相当耗时。一列包含一个公园列表,我想为其制作单独的地块,因为每个地块属于不同的地方。每个公园都需要按区域和年份分组为时间序列图。 Height_mm 的平均值也需要使用标准误差进行计算。有 5 个不同的公园,每个公园有 3 个不同的区域和 10 个不同的年份。 csv中有超过5000条记录。

head(data)

  Park_name  Zone Year  Height_mm
1     Park1 Zone1 2011        380
2     Park1 Zone1 2011        510
3     Park1 Zone1 2011        270
4     Park1 Zone2 2011        270
5     Park1 Zone2 2011        230
6     Park1 Zone2 2011        330

我希望能够操纵下面的代码来完成这项工作,尽管我只是想不通。不过,我很乐意接受任何其他建议。

library(ggplot2)
library(plyr)

data=read.table("C:/data.csv", sep=",", header=TRUE)

ggplot(data, aes(x=Year, y=Height_mm)) + 
  #geom_errorbar(aes(ymin=mean-se, ymax=mean+se), width=.05, colour="black", position=pd) +
  geom_line() +
  geom_point(size=3, fill="black") +
  xlab("Year") + 
  ylab("Mean height (mm)") +
  #facet_wrap(~Park_name, scales = "free", ncol=2) + #I'd like something like this but with all plots as separate figures
  theme_bw() +
  theme(axis.text.x=theme_text(),  
        #axis.title.x=theme_blank(), 
        #axis.title.y=theme_blank(), 
        axis.line=theme_segment(colour="black"), 
        panel.grid.minor = theme_blank(),
        panel.grid.major = theme_blank(),
        panel.border=theme_blank(),
        panel.background=theme_blank(),
        legend.justification=c(10,10), legend.position=c(10,10), 
        legend.title = theme_text(),
        legend.key = theme_blank()
  )

我假设我需要某种“for”循环,但我不知道该放在哪里或如何使用它。谢谢

【问题讨论】:

  • 您是否考虑过 aggregatesplitbytapply 来分割数据?
  • 5000 条记录现在被视为 小型 数据集,并且不会花费太多时间来拆分
  • 如果我打电话给p你原来的情节,你可以做d_ply(Y, "Park_name", "%+%", e1=p)

标签: r ggplot2


【解决方案1】:

您似乎想做类似以下的事情。如果我误解了您的问题,请修改您的问题。您可能还希望提供来自多个公园、区域和年份的数据。

# load packages
require(ggplot2)
require(plyr)
# read data 
Y <- read.table("C:/data.csv", sep=",", header=TRUE)
# define the theme
th <- theme_bw() +
  theme(axis.text.x=element_text(),  
        axis.line=element_line(colour="black"), 
        panel.grid.minor = element_blank(),
        panel.grid.major = element_blank(),
        panel.background=element_blank(),
        legend.justification=c(10,10), legend.position=c(10,10), 
        legend.title = element_text(),
        legend.key = element_blank()
        )
# determine park levels
parks <- levels(Y[,"Park_name"])
# apply seperately for each park
p <- lapply(parks, function(park) {
ggplot(Y[Y[, "Park_name"]==park,], aes(x=as.factor(Year), y=Height_mm)) +
  facet_grid(Zone~.) + # show each zone in a seperate facet
  geom_point() + # plot the actual heights (if desired)
  # plot the mean and confidence interval
  stat_summary(fun.data="mean_cl_boot", color="red") 
})       
# finally print your plots
lapply(p, function(x) print(x+th))

【讨论】:

  • 完美!正是我想要的。只剩下一点点个性化,一切都很好!非常感谢!!
  • 我已修改此代码,以便它构建出适合我的数据的图表,并希望将每个图表打印到 1 个 PDF 文件中的单独页面。我试过: pdf("filename",onefile=TRUE) lapply(p, function(x) print(x+th)) dev.off() 但我得到一个单页空白 PDF。知道我错过了什么吗?
猜你喜欢
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 2022-11-26
  • 1970-01-01
  • 1970-01-01
  • 2020-05-10
  • 1970-01-01
  • 2016-12-03
相关资源
最近更新 更多