数据帧每一行的单个条形图 [英] Single barplot for each row of dataframe

查看:86
本文介绍了数据帧每一行的单个条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个csv文件,如下所示:

I have a csv file which looks like the following:

Name,Count1,Count2,Count3
application_name1,x1,x2,x3
application_name2,x4,x5,x6

x变量代表数字,applications_name变量代表不同应用程序的名称.

The x variables represent numbers and the applications_name variables represent names of different applications.

现在,我想通过使用ggplot2为每一行创建一个barplot.该小节应将application_name作为标题. x轴应显示Count1,Count2,Count3,y轴应显示相应的值(x1,x2,x3).

Now I would like to make a barplot for each row by using ggplot2. The barplot should have the application_name as title. The x axis should show Count1, Count2, Count3 and the y axis should show the corresponding values (x1, x2, x3).

我希望每一行都有一个单独的地块,因为我必须将不同的地块存储在不同的文件中.所以我想我不能使用融化".

I would like to have a single barplot for each row, because I have to store the different plots in different files. So I guess I cannot use "melt".

我想要类似的东西:

for each row in rows {
  print barplot in file
}

感谢您的帮助.

推荐答案

您可以使用melt重新排列数据,然后使用facet_wrapfacet_grid为每个应用程序名称获取单独的图

You can use melt to rearrange your data and then use either facet_wrap or facet_grid to get a separate plot for each application name

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)

这篇关于数据帧每一行的单个条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆