GFW
保存可以直接用ggsave()
library(ggplot2)
library(data.table)
data = "year eva p8 p9 p10
2015 5 14 11 13
2015 4 53 38 40
2015 3 10 25 26
2015 2 20 23 19
2015 1 3 3 2
2016 5 14 10 12
2016 4 47 37 39"
a <- fread(data)
a <- as.data.frame(a)
idx <- which(grepl("^p",names(a)))
colnm <- names(a)[idx]
a.list <- list()
titles <- list()
for (i in 1:length(colnm)){
a.list[[i]] <- data.frame(year = a$year,eva = a$eva, value = a[,idx[i]])
titles[[i]] <- ggtitle(colnm[i])
}
plots <- list()
for (i in 1:length(colnm)){
plots[[i]] <- ggplot(data=a.list[[i]],aes(x=year, y=value,fill=eva))+
geom_text(aes(label=paste(format(value,digits = 0),"%",sep = "")),position=position_stack(vjust=0.5))+
titles[[i]]
#width and height are in inch
ggsave(filename = paste("plot_",colnm[i],".tiff",sep = ""), plot=plots[[i]], dpi = 600, width = 4.333, height = 3,device = "tiff", compression = "lzw" )
}
# save ggplots
save(plots,file="./plots.rda")