我的代码如下
我想随着时间的推移绘制许多变量,在我的代码中我只保留了2个变量: 广泛和电子商务。最终的图必须显示最小值,平均值,中位数,最大值的foreach变量。
一切看起来都很好,除了最新的情节ggplot没有出现,如果我把一个简单的情节作为它上面的线工作,但它不是我想要的... 此外,如何更改我的代码,以使results_melted像res.i.melted。
超过时间
library(reshape) # to use cast
library(ggplot2)
clist <- c("broad","ecom")
col.list <- c("blue","orange","green","red")
for (i in clist) {
ict <- as.character(i)
res = data.frame()
for ( t_year in unique(PROD.ICT.MexElec$year))
{
tmp=subset(PROD.ICT.MexElec, year==t_year)
min = 100*min(tmp[,ict])
mean = 100*mean(tmp[,ict])
median = 100*median(tmp[,ict])
max = 100*max(tmp[,ict])
res = rbind(res, data.frame( t_year , min , mean , median, max))
}
#reshape the data according to YEAR variable (t_year)
results_melted <- melt(res, id.vars=c("t_year"))
colnames(results_melted)[2] <- "stati"
#plot ICT variable according to period (DOTS)
#plot(results_melted$t_year,results_melted$value)
ggplot(data=results_melted, aes(x=results_melted$t_year, y=results_melted$value, colour=stati)) + geom_line() + xlab("Year") + ylab(ict) + ggtitle(paste0("Penetration of ",ict," during the period \n Industry"))
}
res数据帧如下:
t_year min均值中位数最大值 1 2000 NA NA NA NA 2 2001 NA NA NA NA 3 2002 NA NA NA NA 4 2003 30 57.27273 60 80 5 2004 40 62.72727 60 90 6 2005 30 64.54545 70 80 7 2006 30 61.66667 70 80 8 2007 30 60.83333 65 80 9 2008 40 60.00000 65 80 10 2009 30 65.00000 70 80 11 2010 30 63.33333 65 80 12 1999 20 20.00000 20 20