Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing borders around points in ggplot

Tags:

r

ggplot2

I have a scatter plot with two different sets of points based on different data sets. I want one of these sets of points to have a border, therefore I have used, pch = 21, which changes the points into circles with a border and an interior.

I want the other set of points to have no border (for example, removing the red borders in the example below). In the example below, I have still added pch = 21 for these points because if I don't use this, then I would have to use colour = Petal.Length, rather than fill = Petal.Length for the colour of the (interior of) the points I do not want to use colour instead of fill because I want the two sets of points to share a legend.

Is there a way to use pch = 21 but then remove the border?

iris2 <- iris %>% 
  mutate(Sepal.Length = Sepal.Length + 1)

ggplot() +
  geom_point(data = iris,
             aes(x = Sepal.Length,
                 y = Sepal.Width, 
                 fill = Petal.Length),
             pch = 21, colour = "red", size = 3) +
  geom_point(data = iris2,
           aes(x = Sepal.Length,
               y = Sepal.Width, 
               fill = Petal.Length),
           pch = 21, colour = "black", size = 3)

Plot with red borders

I have tried using stroke to change the border thickness, but while this makes the borders thicker, stroke = 0 still gives red borders

like image 293
emilys Avatar asked Jun 22 '26 11:06

emilys


1 Answers

stroke=NA removes it altogether


ggplot() +
  geom_point(data = iris,
             aes(x = Sepal.Length,
                 y = Sepal.Width,
                 fill = Petal.Length),
             pch = 21, colour = "red", size = 3, stroke=NA) +
  geom_point(data = iris2,
           aes(x = Sepal.Length,
               y = Sepal.Width,
               fill = Petal.Length),
           pch = 21, colour = "black", size = 3)

like image 152
Sirius Avatar answered Jun 25 '26 10:06

Sirius