Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I plot multiple variables side-by-side in a dotplot in R?

Tags:

plot

r

ggplot2

I'm still pretty new to R, and have come up against a plotting problem I can't find an answer to.

I've got a data frame that looks like this (though a lot bigger):

df <- data.frame(Treatment= rep(c("A", "B", "C"), each = 6),
                 LocA=sample(1:100, 18), 
                 LocB=sample(1:100, 18), 
                 LocC=sample(1:100, 18))

And I want dot plots that look like this one produced in Excel. It's exactly the formatting I want: a dotplot for each of the treatments side-by-side for each location, with data for multiple locations together on one graph. (Profuse apologies for not being able to post the image here; posting images requires a 10 reputation.)

enter image description here

It's no problem to make a plot for each location, with the dots color-coded, and so on:

ggplot(data = df, aes(x=Treatment, y=LocA, color = Treatment)) + geom_point() 

but I can't figure out how to add locations B and C to the same graph.

Any advice would be much appreciated!

like image 854
phosphorelated Avatar asked Feb 10 '13 23:02

phosphorelated


2 Answers

As a couple of people have mentioned, you need to "melt" the data, getting it into a "long" form.

library(reshape2)
df_melted <- melt(df, id.vars=c("Treatment"))
colnames(df_melted)[2] <- "Location"

In ggplot jargon, having different groups like treatment side-by-side is achieved through "dodging". Usually for things like barplots you can just say position="dodge" but geom_point seems to require a bit more manual specification:

ggplot(data=df_melted, aes(x=Location, y=value, color=Treatment)) +
  geom_point(position=position_dodge(width=0.3))

enter image description here

like image 161
Marius Avatar answered Sep 21 '22 00:09

Marius


You need to reshape the data. Here an example using reshape2

library(reshape2)
dat.m <- melt(dat, id.vars='Treatment')
library(ggplot2)
ggplot(data = dat.m, 
        aes(x=Treatment, y=value,shape = Treatment,color=Treatment)) + 
                  geom_point()+facet_grid(~variable)

enter image description here

Since you want a dotplot, I propose also a lattice solution. I think it is more suitable in this case.

dotplot(value~Treatment|variable,
        groups = Treatment, data=dat.m,
        pch=c(25,19),
        par.strip.text=list(cex=3),
        cex=2)

enter image description here

like image 24
agstudy Avatar answered Sep 18 '22 00:09

agstudy