如何在ggplot

时间:2017-05-29 11:24:17

标签: r ggplot2

我有以下融化的数据框:

>df
    cluster.kmeans       variable  Stats   value 
1                1    mob.sms.amt      n  1072.00
2                1        mob.sms      n  1072.00
3                1       mob.data      n  1072.00
4                1      mob.voice      n  1072.00
5                1 mob.data.dwnld      n  1072.00
6                2    mob.sms.amt      n  1498.00
7                2        mob.sms      n  1498.00
8                2       mob.data      n  1498.00
9                2      mob.voice      n  1498.00
10               2 mob.data.dwnld      n  1498.00
11               3    mob.sms.amt      n   847.00
12               3        mob.sms      n   847.00
13               3       mob.data      n   847.00
14               3      mob.voice      n   847.00
15               3 mob.data.dwnld      n   847.00

str(df)
'data.frame':   270 obs. of  4 variables:
 $ cluster.kmeans: Factor w/ 9 levels "1","2","3","4",..: 1 1 1 1 1 2 2 2 2 2 ...
 $ variable      : Factor w/ 10 levels "mob.data","mob.sms",..: 8 2 1 3 7 8 2 1 3 7 ...
 $ Stats         : Factor w/ 6 levels "n","Mean","SD",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ value         : num  1072 1072 1072 1072 1072 ...

我试图通过对变量进行分组来绘制条形图群集?如下: enter image description here

1 个答案:

答案 0 :(得分:1)

ggplot(df, aes(cluster.kmeans, value, fill=variable)) + 
    geom_bar(stat="identity", position="dodge")

enter image description here