emilys
emilys

Reputation: 135

Removing borders around points in ggplot

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

Upvotes: 8

Views: 8874

Answers (2)

brunosm
brunosm

Reputation: 166

You just need to put colour = Petal.Length inside aes() and remove the colour attribute outside.

Like this:

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

Upvotes: 0

Sirius
Sirius

Reputation: 5429

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)

Upvotes: 9

Related Questions