我有一个csv文件,如下所示:
Name,Count1,Count2,Count3
application_name1,x1,x2,x3
application_name2,x4,x5,x6
x变量表示数字,applications_name变量表示不同应用程序的名称。
现在,我想通过使用ggplot2为每一行制作一个barplot。该小节应将application_name作为标题。x轴应显示Count1,Count2,Count3,y轴应显示相应的值(x1,x2,x3)。
我希望每一行都有一个单独的barplot,因为我必须将不同的图存储在不同的文件中。所以我想我不能使用“融化”。
我想要一些类似的东西:
for each row in rows {
print barplot in file
}
谢谢你的帮助。
您可以使用melt
重新排列数据,然后使用facet_wrap
或facet_grid
为每个应用程序名称获取单独的图
library(ggplot2)
library(reshape2)
# example data
mydf <- data.frame(name = paste0("name",1:4), replicate(5,rpois(4,30)))
names(mydf)[2:6] <- paste0("count",1:5)
# rearrange data
m <- melt(mydf)
# if you are wanting to export each plot separately
# I used facet_wrap as a quick way to add the application name as a plot title
for(i in levels(m$name)) {
p <- ggplot(subset(m, name==i), aes(variable, value, fill = variable)) +
facet_wrap(~ name) +
geom_bar(stat="identity", show_guide=FALSE)
ggsave(paste0("figure_",i,".pdf"), p)
}
# or all plots in one window
ggplot(m, aes(variable, value, fill = variable)) +
facet_wrap(~ name) +
geom_bar(stat="identity", show_guide=FALSE)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句