0

I'm new to R and having a few issues with using ggplot2.

This is an example of my data (subset of larger data set) :

df <- 
structure(list(logpvalue = c(22.36, 6.93, 16.78, 1.78, 17.75, 
20.99, 21.03, 9.19, 15.01, 22.25, 13.4, 6.47, 1.34, 13.4, 3.21, 
0.37, 0.5, 0.12, 1.8, 0.71, 1.15, 6.73, 0.12, 6.97, 0.64, 9.85, 
1.45, 1.67, 2.6, 1.8, 1.35, 4.69, 0.37, 1.91, 0.31, 0, 2.45, 
1.68, 2.31, 1.35, 6.48, 4.68), SNP = structure(c(1L, 7L, 6L, 
5L, 11L, 1L, 9L, 5L, 8L, 11L, 7L, 5L, 8L, 11L, 1L, 7L, 1L, 4L, 
2L, 3L, 10L, 7L, 1L, 4L, 2L, 3L, 10L, 4L, 2L, 3L, 10L, 4L, 2L, 
3L, 10L, 4L, 2L, 3L, 7L, 9L, 5L, 1L), .Label = c("rs10244", "rs10891244", 
"rs10891245", "rs11213821", "rs12296076", "rs138567267", "rs45615536", 
"rs6589218", "rs7103178", "rs7127721", "rs7944895"), class = "factor"), 
X173 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L), .Label = c("het", "hom"), class = "factor")), .Names = c("logpvalue", 
"SNP", "X173"), class = "data.frame", row.names = c(NA, -42L))

I want to plot a boxplot of logpvalue on y axis, with SNP on the x-axis but with each SNP also categorized by whether the patient is het or hom for X173. So from this data I'd imagine 4 boxes on my boxplot.

If possible I'd also like to incorporate the individual data points (dotplot-boxplot overlay) with jitter.

This is the usual code I'd use for a boxplot of logpavlue vs SNP:

qplot(logpvalue, SNP, data = mydata, geom="boxplot")
+ geom_jitter(position=position_jitter(w=0.1, h=0.1)) + theme_bw()

How do I add the extra x variable into this code?

zx8754
  • 52,746
  • 12
  • 114
  • 209
user3778714
  • 13
  • 1
  • 3
  • 2
    Please make your [data reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and read about [box plots](http://en.wikipedia.org/wiki/Box_plot) – zx8754 Jun 26 '14 at 10:43
  • Thanks for the reply - I've tried to make my data reproducible however its a large dataset and I don't want to paste all my data online. Not sure what your point about boxplots is - I know they are for multiple data points not individual as my previous example data would have given. I barely understand any script so I apologize for my ineptitude! – user3778714 Jun 26 '14 at 12:09

1 Answers1

0

Try this:

boxplot(df$logpvalue~paste(df$SNP,df$X173))

Or using ggolot2 :

library(ggplot2)

ggplot(data=df,aes(SNP,logpvalue,colour=SNP)) +
  geom_boxplot() +
  geom_jitter() +
  facet_grid(.~X173)

enter image description here

zx8754
  • 52,746
  • 12
  • 114
  • 209