sie*_*fer 1 r bar-chart ggplot2
我有一个csv文件,如下所示:
Name,Count1,Count2,Count3
application_name1,x1,x2,x3
application_name2,x4,x5,x6
Run Code Online (Sandbox Code Playgroud)
x变量表示数字,而application_name变量表示不同应用程序的名称.
现在我想通过使用ggplot2为每一行制作一个条形图.条形图应该将application_name作为标题.x轴应显示Count1,Count2,Count3,y轴应显示相应的值(x1,x2,x3).
我希望每行都有一个条形图,因为我必须将不同的图存储在不同的文件中.所以我想我不能用"融化".
我希望有类似的东西:
for each row in rows {
print barplot in file
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.
您可以使用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)
Run Code Online (Sandbox Code Playgroud)