user13246133
user13246133

Reputation: 3

ggplot2 how to get rid of duplicate dots?

I made a trial where I compare 2 different conditions for multiple treatments. However, on the plot, some of the value have two points where they should only be one.

here is the code I used for my plot

ggplot(Bites, aes(x=Treatment, y=Biting, colour=Condition, fill=Condition))+
    geom_point(position=position_jitterdodge(dodge.width=0.7), size=2)+
    geom_boxplot(alpha=0.5, position= position_dodge(width=0.8), fatten=NULL)+
    stat_summary(fun.y= mean, geom="errorbar", aes(ymax=..y.., ymin=..y..),
        width=0.65, size= 1.5, linetype= "solid", position = position_dodge(width=0.7))+
    xlab("Treatment") +
    ylab("Bites")+
    labs(Color ="Condition")+   
    theme_classic()

ggplot graph

Here are part of the data I used.

   Biting Treatment Condition
1       0         A         X
2       0         A         X
3       0         A         X
4       0         A         X
5       0         A         X
6       0         A         X
7       0         A         X
8       0         A         X
9       1         A         X
10      1         A         X
11      1         A         X
12      2         A         X
13      4         A         X
14      7         A         X
15      9         A         X

I should only get one point in the graph for 4 bites, 7 bites and 9 bites; yet I get 2 dots (one red and one paler).

How can I get rid of the paler dots ?

Upvotes: 0

Views: 1250

Answers (2)

RaV
RaV

Reputation: 627

This duplicated dots are the outliers from geom_boxplot() (notice, how they are always centered on boxplot)

Example plot

Simply add outlier.shape = NA inside geom_boxplot() (as it was suggested under this question) and then they won't appear.

Solution plot

Data:

Bites <- data.frame(Biting = sample(c(rep(0, 7), rep(1, 3), 2, 4, 7, 9)),
                    Treatment = c(rep("A", 7), rep("B", 7)),
                    Condition = rep(c("X", "Y"), 7))

Upvotes: 3

Mark Neal
Mark Neal

Reputation: 1204

The lighter points are the outliers from the geom_boxplot call. Look up the option to suppress outliers on that call and you should be sweet.

ps - use dput() to share data frames in a stack overflow question to make it easy for others to assist.

Upvotes: 0

Related Questions