phosphorelated phosphorelated - 9 months ago 56
R Question

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

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!

Answer Source

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