R条形图有3个变量

时间:2017-03-24 08:57:41

标签: r bar-chart

我有一个包含多个变量的数据框,我想知道如何将它们绘制成Excel中的绘图选项。

只是一个简单的数据示例:

  > V1   V2  V3
    1    A    0
    1    A    0
    1    B    1
    1    B    0
    1    A    1
    2    A    0
    2    B    0
    2    A    0
    2    A    0
    2    A    0

我希望拥有的是xV1y轴,V3V2的所有数量均为A Bbarplot

有人可以就如何做到这一点分享一些想法吗? 2*2函数似乎不具备功能,因为它只能用于y表?

谢谢。

编辑:

此图不是由给出的数据生成的: enter image description here

V3轴视为x的百分比,V1的{​​{1}}轴以及V2的每个级别创建条形图。

2 个答案:

答案 0 :(得分:3)

library( 'ggplot2' )
library( 'reshape2' )
df1 <- dcast( data = df1, formula = V1 ~ V2,  value.var = 'V3',  fun.aggregate = sum )  # get sum of V3 by grouping V1 and V2
df1 <- melt( data = df1, id.vars = 'V1')   # melt data
df1
#    V1 variable value
# 1  1        A     1
# 2  2        A     5
# 3  1        B     1
# 4  2        B     0  


ggplot(data = df1, aes( x = factor( V1 ), y = value, fill = variable ) ) +    # print bar chart
  geom_bar( stat = 'identity' )

enter image description here

使用position = 'dodge

ggplot(data = df1, aes( x = factor( V1 ), y = value, fill = variable ) ) +    # print bar chart
  geom_bar( stat = 'identity', position = 'dodge' )

enter image description here

数据:

df1 <- read.table(text = 'V1   V2  V3
    1    A    0
                  1    A    0
                  1    B    1
                  1    B    0
                  1    A    1
                  2    A    0
                  2    B    0
                  2    A    0
                  2    A    5
                  2    A    0', header = TRUE, stringsAsFactors = FALSE )

答案 1 :(得分:1)

首先,您需要获得包含要绘制的值的摘要dataframe

df <- data.frame(V1 = rep(1:2,each=5), V2 = c("A","A","B", "B", "A", "A", "B","A", "A", "A"), 
                 V3 = c(0,0,1,0,1,0,0,0,0,0))

values <- aggregate(df$V3, list(V1 = df$V1, V2 = df$V2), sum)

#    V1 V2 V3
# 1  1  A  1
# 2  2  A  0
# 3  1  B  1
# 4  2  B  0

ggplot(values, aes(x = factor(V1), y = V3, fill = V2))+
                geom_bar(stat = "identity", width = 0.2)

one

或者,如果您不希望它们堆叠在彼此之上。添加一些标签。

ggplot(values, aes(x = factor(V1), y = V3, fill = V2))+
                geom_bar(stat = "identity", width = 0.2, position = "dodge") +
                labs(list(x = "x", y = "count",fill = "group"))

two

修改

我尝试直接在ggplot上使用dataframe而未做摘要,结果相同。

## a little change in V3
df <- data.frame(V1 = rep(1:2,each=5), 
                 V2 = c("A","A","B", "B", "A", "A", "B","A", "A", "A"), 
                 V3 = c(2,0,1,2,1,3,3,8,1,0))
## plot df directly
ggplot(df, aes(factor(V1), V3, fill = V2)) + 
        geom_bar(stat = "identity", width = 0.2, position = "dodge") +
        labs(list(x = "x", y = "count",fill = "group"))

three