I have a continuous response variable, and a binary predictor variable. However, that binary predictor also comes in two flavors (two different years). I'd like to create a box plot with the two years separate but in the same x-variable column.
Here's a hypothetical dataframe setup like mine
Wingspan Infected Year
15.3 1 2015
14.9 1 2015
15.9 0 2016
15.0 1 2016
13.8 0 2015
16.1 0 2016
14.2 1 2015
15.9 1 2015
13.7 0 2016
16.4 0 2016
13.9 0 2016
14.0 1 2015
It's easy for me to get an output by doing
Model <- Wingspan ~ Infected
plot(Model)
However, I want the Infected columns to have 2 boxes per column, one for 2015 and one for 2016. I've tried all sorts of functions to split the data like split() and various bind functions but I can't seem to partition this data in any way and get an output. Any ideas would be appreciated.
Is this what you would like:
require(read.so) #Awesome package by @Alistaire47
dat <- read.so()
require(ggplot2)
ggplot(dat, aes(as.character(Infected), Wingspan, color = as.character(Year))) +
geom_point()
#I have used as.character in order to prevent R reading the numbers as ,
#... well... , numbers

edit 1
For boxplots, simply change geom_point() to geom_boxplot()... that's all :)
edit 2 for different colors in base R, add the following to @thelatemail's code:
boxplot(Wingspan ~ Infected + Year, data=dat, boxfill = dat$Year)
#again, try ggplot. Very rewarding, in terms of getting nice graphs.

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With