问题
Does anyone know how to create a scatterplot in R
to create plots like these in PRISM's graphpad:

I tried using boxplots but they don't display the data the way I want it. These column scatterplots that graphpad can generate show the data better for me.
Any suggestions would be appreciated.
回答1:
As @smillig mentioned, you can achieve this using ggplot2. The code below reproduces the plot that you are after pretty well - warning it is quite tricky. First load the ggplot2 package and generate some data:
library(ggplot2)
dd = data.frame(values=runif(21), type = c("Control", "Treated", "Treated + A"))
Next change the default theme:
theme_set(theme_bw())
Now we build the plot.
Construct a base object - nothing is plotted:
g = ggplot(dd, aes(type, values))
Add on the points: adjust the default jitter and change glyph according to type:
g = g + geom_jitter(aes(pch=type), position=position_jitter(width=0.1))
Add on the "box": calculate where the box ends. In this case, I've chosen the average value. If you don't want the box, just omit this step.
g = g + stat_summary(fun.y = function(i) mean(i), geom="bar", fill="white", colour="black")
Add on some error bars: calculate the upper/lower bounds and adjust the bar width:
g = g + stat_summary( fun.ymax=function(i) mean(i) + qt(0.975, length(i))*sd(i)/length(i), fun.ymin=function(i) mean(i) - qt(0.975, length(i)) *sd(i)/length(i), geom="errorbar", width=0.2)
Display the plot
g

- In my R code above I used
stat_summary
to calculate the values needed on the fly. You could also create separate data frames and usegeom_errorbar
andgeom_bar
. - To use base R, have a look at my answer to this question.
回答2:
If you don't mind using the ggplot2
package, there's an easy way to make similar graphics with geom_boxplot
and geom_jitter
. Using the mtcars
example data:
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot() + geom_jitter() + theme_bw()
which produces the following graphic:

The documentation can be seen here: http://had.co.nz/ggplot2/geom_boxplot.html
来源:https://stackoverflow.com/questions/12399506/how-do-i-create-a-categorical-scatterplot-in-r-like-boxplots