变量条形图 [英] Barplot of variables by factors

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

问题描述

我希望针对不同变量(v1-v5)的值绘制不同类别​​(虚拟数据中的V,C和S)的条形图,以查看不同类别的范围在变量中如何变化.有没有办法在R中做到这一点?

I wish to plot barplots for different categories (V, C and S in the dummy data) for values of different variables (v1-v5) to see how ranges of the different categories vary across the variable. Is there a way to do it in R ?

 Factor   v1  v2  v3  v4  v5
      v 12.0 8.4 5.3 3.0 3.5
      v 11.8 7.5 4.7 2.8 3.7
      c 13.2 9.5 5.7 3.0 4.1
      c 13.4 9.3 6.3 3.3 3.8
      c 10.5 7.7 5.0 2.5 3.2
      s 13.1 9.6 5.4 3.1 3.9
      s 15.0 9.0 5.1 4.5 9.0

它应该是使用某些软件包的非常简单的代码,但我无法弄清楚.

Its should be a very simple code using some package, but I have not been able to figure it out.

推荐答案

您可以这样做:

library(data.table)
df2 <- melt(setDT(df), measure.vars = patterns("^v"))

library(ggplot2)
ggplot(df2, aes(x=Factor, y=value1)) +
  stat_summary(aes(fill=variable), fun.y="sum", geom = "bar", position="dodge") +
  theme_bw()

这给出了:

使用的数据:

df <- read.table(text="Factor   v1  v2  v3  v4  v5
v 12.0 8.4 5.3 3.0 3.5
v 11.8 7.5 4.7 2.8 3.7
c 13.2 9.5 5.7 3.0 4.1
c 13.4 9.3 6.3 3.3 3.8
c 10.5 7.7 5.0 2.5 3.2
s 13.1 9.6 5.4 3.1 3.9
s 15.0 9.0 5.1 4.5 9.0", header=TRUE)

这篇关于变量条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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