LoneWolf
LoneWolf

Reputation: 214

Creating a grouped bar plot in R

I have a data frame df in R, that looks like this:

        D     C     B     E     K     R
Rd      80    80    80    80    80    80
Sw      100   100   100   100   100   100
Sf      100   100   100   100   100   100

I'm trying to plot the data in a bar plot. I need the y axis to have the range 0-100, and the x axis to be the category names. Basically, I need it to look like this:

100 |               _ _ _ _ _ _    _ _ _ _ _ _
    |_ _ _ _ _ _   | | | | | | |  | | | | | | |
    | | | | | | |  | | | | | | |  | | | | | | |
    | | | | | | |  | | | | | | |  | | | | | | |
    | | | | | | |  | | | | | | |  | | | | | | |
 0  |_|_|_|_|_|_|__|_|_|_|_|_|_|__|_|_|_|_|_|_|_
     D C B E K R    D C B E K R    D C B E K R
         Rd             Sw            Sf

With all of the Ds the same color, all of the Cs the same colour, and so on.

I'm not sure how to do this, or which libraries to use.

So far I have:

counts <- as.matrix(df$D, df$C, df$B, df$E, df$K, df$R)

barplot(counts, beside = TRUE, space = c(0, 0, 0, 0, 0, 0), xlab = "",
col = c("coral", "coral1", "coral2", "coral3", "coral4", "cornflowerblue"),
    names.arg = c("D", "C", "B", "E", "K", "R"))
mtext(side = 1, text = "x label", line = 7)

But it only displays something like this:

100 |  _ _   _ _
    |_| | |_| | |
    | | | | | | |
    | | | | | | |
    | | | | | | |
 0  |_|_|_|_|_|_|_
     D C B E K R
       x label

I'm not sure why I'm getting only this.

Any help would be much appreciated.

Upvotes: 4

Views: 3116

Answers (3)

thelatemail
thelatemail

Reputation: 93938

Try this:

df2 <- t(as.matrix(df))
bp <- barplot(df2,beside=TRUE,col=1:6)
mtext(rownames(df2),1,at=bp)

enter image description here

If you would like to edit things to spin axis labels or exactly position the group/bar labels, then you can do something like the below code. Change the line= arguments to affect the vertical position of the labels and the las=1 argument on the barplot call to spin the labels as required.

df2 <- t(as.matrix(df))
bp <- barplot(df2,beside=TRUE,col=1:6,axisnames=FALSE,las=1)
mtext(rownames(df2),1,at=bp,line=0.6)
mtext(colnames(df2),1,at=colMeans(bp),line=2)

Upvotes: 4

lukeA
lukeA

Reputation: 54287

set.seed(1)
df <- data.frame(groups = rep(c("Rd", "Sw", "Sf"), each=6),
                 bars = rep(c("D", "C", "B", "E", "K", "R"), 3),
                 values = sample(1:100,18))
library(ggplot2)
ggplot(df, aes(x=bars, y=values, fill=bars)) + 
  geom_bar(stat="identity", position="dodge") + 
  facet_wrap(~groups, ncol=3)

Result: see the figure below.

Or, if you take the data frame from the question:

df <- read.table(sep=" ", header=T, text="
D C B E K R
Rd 80 80 80 80 80 80
Sw 100 100 100 100 100 100
Sf 100 100 100 100 100 100")

df$facet <- row.names(df)
library(reshape2)
df.long <- melt(df, "facet")
ggplot(df.long, aes(x=variable, y=value, fill=variable)) + 
  geom_bar(stat="identity", position="dodge") + 
  facet_wrap(~facet, ncol=3)

enter image description here

Upvotes: 4

jimmyb
jimmyb

Reputation: 4387

It looks like you don't want that as.matrix call with the column vectors from the data.frame -- it is most likely that you want: as.matrix(D), for instance:

barplot(as.matrix(data.frame(rpois(10, 2), rpois(10, 1))), beside = T)

Basically, I am giving a simple example, imagine you have:

df = data.frame(x = c(1,2,3), y = c(2,3,4))

you were doing as.matrix(df$x, df$y) - that's not what you want, you want to do: as.matrix(df), thus:

barplot(as.matrix(df), beside = T)

will give you what you want. The key is to look at what "as.matrix" does.

Upvotes: 0

Related Questions