在R中从单个数据框中制作多个单独的图 [英] Make multiple separate plots from single data frame in R

查看:111
本文介绍了在R中从单个数据框中制作多个单独的图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的数据集,我不希望分裂,因为它会相当耗时。一列包含我想为每个情节属于不同地方而分开绘制的公园列表。每个公园需要按区域和年份分组,以时间序列图形表示。 Height_mm的平均值也需要用标准误差来计算。有5个不同的公园,每个公园有3个不同的区域和10个不同的年份。 csv中有超过5000条记录。

I have a large dataset that I would prefer not to split up because it will be rather time consuming. One column contains a list of parks which I want to make separate plots for as each plot belongs somewhere different. Each park needs to be grouped by Zone and Year as time series graphs. The mean for Height_mm also needs to be calculated with standard errors. There are 5 different parks each with 3 different zones and 10 different years. There are over 5000 records in the csv.

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

我希望能够操纵下面的代码来完成这项工作,尽管我只是无法弄清楚。我很乐意接受其他建议。

I would like to be able to manipulate the code below to make this work though I just can't figure it out. I'll gladly take any other suggestions though.

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'循环,但我不知道该把它放在哪里或如何使用它。谢谢

I'm assuming I need a 'for' loop of some kind though I don't know where to put it or how to use it. Thanks

推荐答案

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

It seems that you would like to do something similar to the following. If I missunderstood your question, please revise your question. You may also want to provide data from more than one park, zone and year.

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

这篇关于在R中从单个数据框中制作多个单独的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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