Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R ggplot geom_jitter duplicates outlier

Tags:

r

ggplot2

Q1. I am plotting a dataset using ggplot's geom_boxplot. However, when I am trying to plot all data points using geom_jitter(), the outlier I have in my data is duplicated. All other data points are fine. Where is the problem?

Sample code:

PeakPeriod_24h <- c (31.05820, 23.83500, 24.36254, 25.31609, 24.21623, 23.90320) 
condition <- rep("HL",6)
data_HL <- data.frame(condition, PeakPeriod_24h)
p <- ggplot(data_HL, aes(x=condition, y=PeakPeriod_24h, fill=condition))
p + geom_boxplot()+
  geom_jitter(width = 0.3)+
  theme_bw()+
  coord_flip()+
  geom_hline(aes(yintercept=24.18), colour="brown1", linetype="dotted", size = 1.4)+
  scale_y_continuous(limits=c(), name = "Period Length")+
  ggtitle("Boxplots\nHabitual Light")+
  scale_fill_manual(values = c("gray60"))+
  theme(plot.title = element_text(size=14, face="bold", vjust = .5),
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.title.x = element_text(size=12, face = "bold"),
    axis.text.x = element_text(size = 10, face = "bold", colour = "gray20"))+
  guides(fill=FALSE)

enter image description here

Thanks!

like image 685
Christine Blume Avatar asked May 28 '16 11:05

Christine Blume


1 Answers

Try

ggplot(data_HL, aes(x=condition, y=PeakPeriod_24h, fill=condition)) + 
  geom_boxplot(outlier.shape = NA) +  
  geom_jitter(width = 0.3) 

The outlier is doubled, because it is plotted by geom_boxplot (unless you specify that you don't want it to plot points for outliers) and another time by geom_jitter.

And for the second question, you can use

geom_jitter(width = 0.3, aes(color=I(c("black", "blue")[code+1L]))) 
like image 165
lukeA Avatar answered Oct 21 '22 01:10

lukeA