从data.frame创建barplot [英] Create barplot from data.frame

查看:409
本文介绍了从data.frame创建barplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在RI中有一个像图片顶部的数据框架。



是否有可能创建一个像在底部的一个barplot图像?



解决方案

假设你不想要ascii输出,这里是使用 ggplot2

 #加载/生成您的数据
mydf< - data.frame(X1 = c 2,4,1),X2 = c(3,2,NA),x3 = c(4,1,NA),row.names = c(A,B,C))
mydf $类别< - row.names(mydf)

#根据需要ggplot
库(reshape2)
mydf.molten的数据为长格式$ - fs(mydf,value.name =Count,variable.name =Variable,na.rm = TRUE)

#plot and facet by categories
library(ggplot2)
qplot(data = mydf.molten,x = Variable,y = Count,geom =bar,stat =identity)+ facet_wrap(Category)
/ pre>



有关详细信息,建议您参考 ggplot2手册,特别是关于 geom_bar facet_wrap


In R I have a data.frame like the one on the top of the picture.

Is there a possibility to create a barplot like the one on the bottom of the image?

解决方案

Assuming, that you don't want ascii output, here is a solution using ggplot2:

# load / generate your data
mydf <- data.frame( X1 = c(2,4,1), X2 = c(3,2,NA), x3 = c(4,1,NA), row.names=c("A","B","C") )
mydf$Category  <- row.names(mydf)

# bring your data to long format as needed by ggplot
library(reshape2)
mydf.molten <- melt(mydf, value.name="Count", variable.name="Variable", na.rm=TRUE)

# plot and facet by categories
library(ggplot2)
qplot( data=mydf.molten, x = Variable, y = Count, geom="bar", stat = "identity" ) + facet_wrap( "Category" )

For further details, I'd recommend to consult the ggplot2 manual, especially the chapter about geom_bar and facet_wrap.

这篇关于从data.frame创建barplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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