如何使用 R 中的数据帧列表编写箱线图函数
在下面的数据框中,
set.seed(123)
code <- c(5001,5001,5250,5250,5425,5425,5610,5610,5910,5910,5010,5010,6110,6110,6135,6135,6220,6220,6550,6550)
county <- c("A01","A01","A01","A02","A01","A02","A03","A03","A01","A02","A03","A04","A01","A01","A01","A01","A01","A01","A02","A02")
state <- c("PA","PA","NY","NY","DE","DE","PA","PA","NY","NY","PA","PA","NY","NY","DE","DE","PA","PA","NY","NY")
dept <- c("energy",'energy','edu','hous','hous','edu','energy','energy','hous','hous','edu','hous','hous','energy','energy',"energy",'energy','edu','hous','hous')
year <- c(2001,2001,2001,2001,2001,2002,2002,2002,2002,2002,2003,2003,2003,2003,2003,2004,2004,2004,2004,2004)
corp_tax <- runif(20, min=5, max=200)
income_tax <- runif(20, min=4, max=175)
bonus <- runif(20, min=10, max=211)
length(dept)
df <- data.frame(code, state, county, year,dept,corp_tax,income_tax,bonus)
df
# for each year
subset(df,year == 2001)
我需要帮助编写一个用户定义函数
,该函数接收数据框并执行以下操作:
(1)。选择 year
、state
、dept
、corp_tax
列
(2)。对于每个年
中的每个唯一的州
,绘制按部门
分组的corp_tax
箱线图。例如,对于 2001
年,我们将分别绘制 PA
、NY
和 DE
的箱线图。
(3)。将绘图(每页 2 个数字)导出为 pdf
请参阅下面的我的尝试:
library(ggplot2)
library(dplyr)
boxplotter <-function(data){
# select the columns
new_data <-data%>%select(year,state,dept,corp_tax)
# split the data based on unique years
split_data <-split(new_data,new_data$year)
# set the pdf for the plot
pdf("boxplotter.pdf", 7, 5)
#################This where I need help with the most, the looping process#####################
# Looping through each state in each year
for (i in seq(1, length(unique(split_data$state)), 10)){
# the actual plot
ggplot(split_data, aes(x=dept, y=corp_tax)) + geom_boxplot(outlier.colour="red", outlier.shape=8,outlier.size=4) +
scale_y_continuous(limits=c(0, max(split_data$corp_tax, na.rm=TRUE))) +
scale_x_continuous(limits=c(0, max(split_data$corp_tax)))
dev.off()
}
#testing my function
boxplotter(df)
所需的输出应如下所示:
我愿意接受其他方法,请分享您的代码。谢谢!!
In the dataframe below
set.seed(123)
code <- c(5001,5001,5250,5250,5425,5425,5610,5610,5910,5910,5010,5010,6110,6110,6135,6135,6220,6220,6550,6550)
county <- c("A01","A01","A01","A02","A01","A02","A03","A03","A01","A02","A03","A04","A01","A01","A01","A01","A01","A01","A02","A02")
state <- c("PA","PA","NY","NY","DE","DE","PA","PA","NY","NY","PA","PA","NY","NY","DE","DE","PA","PA","NY","NY")
dept <- c("energy",'energy','edu','hous','hous','edu','energy','energy','hous','hous','edu','hous','hous','energy','energy',"energy",'energy','edu','hous','hous')
year <- c(2001,2001,2001,2001,2001,2002,2002,2002,2002,2002,2003,2003,2003,2003,2003,2004,2004,2004,2004,2004)
corp_tax <- runif(20, min=5, max=200)
income_tax <- runif(20, min=4, max=175)
bonus <- runif(20, min=10, max=211)
length(dept)
df <- data.frame(code, state, county, year,dept,corp_tax,income_tax,bonus)
df
# for each year
subset(df,year == 2001)
I need help writing a user defined function
that takes in a dataframe and does the following:
(1). select year
, state
,dept
, corp_tax
columns
(2). for each unique state
in each year
, plot boxplot for corp_tax
grouped by dept
. Example, for the year 2001
, we will have boxplots for PA
,NY
and DE
respectively.
(3). export the plots (2 figures per page) in pdf
See below for my attempt:
library(ggplot2)
library(dplyr)
boxplotter <-function(data){
# select the columns
new_data <-data%>%select(year,state,dept,corp_tax)
# split the data based on unique years
split_data <-split(new_data,new_data$year)
# set the pdf for the plot
pdf("boxplotter.pdf", 7, 5)
#################This where I need help with the most, the looping process#####################
# Looping through each state in each year
for (i in seq(1, length(unique(split_data$state)), 10)){
# the actual plot
ggplot(split_data, aes(x=dept, y=corp_tax)) + geom_boxplot(outlier.colour="red", outlier.shape=8,outlier.size=4) +
scale_y_continuous(limits=c(0, max(split_data$corp_tax, na.rm=TRUE))) +
scale_x_continuous(limits=c(0, max(split_data$corp_tax)))
dev.off()
}
#testing my function
boxplotter(df)
Desired output should look like this :
I am open to other approaches, please share your code. Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个解决方案。
拆分数据时,在同一条指令中按
year
和state
进行拆分。然后循环遍历分割列表,绘制每个数据集。使用ggave
保存。在下面的函数中,输出文件名取决于年份/州的组合,并且我包含了一个参数 verbose ,该参数在文件名写入磁盘时打印文件名。
Here is a solution.
When splitting the data, split by
year
andstate
in the same instruction. Then loop through the split list plotting each data set. Save withggave
.In the function below the output filenames depend on the combination year/state and I have included an argument
verbose
that prints the filenames as they are written to disk.