Boxplots in R, plotting multiple points

45 views Asked by At

I am trying to add a list of points to my boxplots, however, the points only show up for one boxplot and not for the another. I was wondering if anyone could help me identify the problem. Here is the code I am using.



#graphics.off()
rm(list=ls())
# Inputs
CapeHenlopen <- c(1,4,3,15,2,3,4,1,10,7,3,1,1,1,4,44,4,2,2,5,1)
Brandywine<- c(1,75,20,15,2,104,3,14,1)
# Outputs 
dev.new() # Open a new window for my boxplot  
boxplot(CapeHenlopen,Brandywine, horizontal = FALSE,
        xlab = c('Location'), 
        ylab= 'Number of Individuals', 
        col=c('red','blue'), 
        names=c('Cape Henlopen', 'Brandywine'))
stripchart(CapeHenlopen, Brandywine, vertical = TRUE, method = "stack", pch = 19, add = TRUE)
dev.copy(png,'myplot.png')

enter image description here

1

There are 1 answers

0
Dave2e On

The easiest way to solve this is to convert your vectors into a data frame with a column for the location and a column for the values. This will make platting an analysis easier.

CapeHenlopen <- c(1,4,3,15,2,3,4,1,10,7,3,1,1,1,4,44,4,2,2,5,1)
Brandywine<- c(1,75,20,15,2,104,3,14,1)
# Outputs 

Brandydf<- data.frame(name="Brandywine", values=Brandywine)
Capedf<- data.frame(name="CapeHenlopen", values=CapeHenlopen)

data<- rbind(Brandydf, Capedf)
boxplot(values~ name, data=data, horizontal = FALSE,
        xlab = c('Location'), 
        ylab= 'Number of Individuals', 
        col=c('red','blue'))
points(x=as.factor(data$name), y=data$values, pch = 19, col="green")

enter image description here