3

I have a dataframe that has multiple variables, and I would like to know how can I plot them like the plotting option in Excel.

Just a simple data example:

  > 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

What I'd like to have, is an x axis with V1, a y axis with all the count of V3 when V2 is A or B.

Can somebody please share some thoughts on how to do this? The barplot function doesn't seem capable because it can only work with 2*2 table?

Thank you.

Edit:

This plot is not generated by the data given though: enter image description here

Consider the y axis as the percentage of V3, the x axis of V1 and for each level of V2 a bar chart is created.

user95902
  • 149
  • 2
  • 3
  • 13

2 Answers2

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

using 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

Data:

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 )
Sathish
  • 12,453
  • 3
  • 41
  • 59
  • Thank you! But does `ggplot` have a "beside" option like the `barplot` does, so I don't get stacked bars but bars next to each other? – user95902 Mar 24 '17 at 09:25
  • Thanx! When I tried to reproduce it, I get "could not find function ".fun"" when running `dcast`? – user95902 Mar 24 '17 at 09:38
  • Hi @Sathish after re-start it's still the same. But I'm sure this is a problem with the package and function, and not with you code. Thank you a lot! =) – user95902 Mar 24 '17 at 09:54
2

First you need to get a summary dataframe that contains the values you want to plot.

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

OR, this if you don't want them to be stacked on top of each other. Adding some labels.

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

EDIT

I tried to use ggplot directly on the dataframe without making a summary, and the results are the same.

## 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

Fadwa
  • 1,717
  • 5
  • 26
  • 43