使用plyr和ggplot2从数据框的几列打印数据
我有一个包含124列和观察值的数据框。部分原因可能是:使用plyr和ggplot2从数据框的几列打印数据,r,ggplot2,plyr,R,Ggplot2,Plyr,我有一个包含124列和观察值的数据框。部分原因可能是: date <- c("2014-01-03", "2014-05-03","2014-02-04") App <- c(0,2,4) Email <- c(1,5,0) Print <- c(0,0,1) mgt <- c(1,9,12) df<- data.frame (date, App, Email, Print, mgt) 我想打印应用程序的日期,然后电子邮件的日期,然后打印日期等在不同的绘图
date <- c("2014-01-03", "2014-05-03","2014-02-04")
App <- c(0,2,4)
Email <- c(1,5,0)
Print <- c(0,0,1)
mgt <- c(1,9,12)
df<- data.frame (date, App, Email, Print, mgt)
我想打印应用程序的日期,然后电子邮件的日期,然后打印日期等在不同的绘图。我尝试使用plyr和ggplot2输出这些图,并得出以下结论:
Plots <- function (Y){print(ggplot(df, aes(x=date, y= Y)) + geom_line() +
scale_x_date(breaks = date_breaks('month'), label= date_format('%b-%Y')) +
labs(title="A", x="Date Issued", y="Number of tickets issued")+
theme_bw()) }
ServicePlots <- d_ply (df, col , Plots, .print=TRUE)
lubridate、chron和scales软件包也在地块中使用。然而,这似乎根本不起作用。有人能指出我做错了什么吗?也许能帮我一点忙?以下不是基于ggplot的,但它可以工作:
par(mfrow=c(1,3))
for(i in 2:5)
plot(mydf[,1], mydf[,i], main=colnames(mydf)[i])
不知道plyr与此处的任何内容有何关系,只需将数据融化并按原样绘制即可:
library(reshape2)
library(ggplot2)
library(scales)
df <- melt(df)
ggplot(df, aes(as.Date(date), value)) +
geom_line(aes(group = 1)) +
scale_x_date(breaks = date_breaks('month')) +
facet_wrap( ~ variable, scales = "free") +
labs(title="A", x="Date Issued", y="Number of tickets issued") +
theme_bw()
如果您希望它以函数形式出现,请执行以下操作
Plots <- function(x){
x <- melt(x)
ggplot(x, aes(as.Date(date), value)) +
geom_line(aes(group = 1)) +
scale_x_date(breaks = date_breaks('month')) +
facet_wrap( ~ variable, scales = "free") +
labs(title="A", x="Date Issued", y="Number of tickets issued") +
theme_bw()
}
Plots(df)
我知道如何使用for循环,只是尝试使用plyr,因为这是这项工作的一个要求。不幸的是:-谢谢!所以,基本上,一个小而可能愚蠢的问题:是否有可能将每个情节分开?我有124个这样的图,希望每个图是分开的?我只是不使用facet\u wrap吗?