divibisan
divibisan

Reputation: 12155

How to highlight lines on ggplot without losing color coding

This question is part R plotting and part graphic design. I am using ggplot2 to make a scatter plot to compare the 3 different populations. I want to show the individual points as well as a linear regression line, and they all have to be on the same plot for comparison purposes.

The problem is that the points are very densely plotted and when the regression line is the same color as the points they blend in together and it's very hard to see it. But since some of the populations overlap, if I make the lines all black it is impossible to see which goes with which.

What I would like is a way to highlight the regression line, possibly by outlining it with a black border, or making it a darker shade of the same color, so that it stand out against the background of points. Here's an example below. I've made the points large here to exaggerate the over-plotting, but shrinking them or reducing the alpha isn't going to help (I've tried).

library(ggplot2)
df <- data.frame('x' = c(rnorm(1000, 1),
                         rnorm(1000, 2),
                         rnorm(1000, 1)),
                 'y' = c(rnorm(1000, 1),
                         rnorm(1000, 2),
                         rnorm(1000, 1)),
                 'z' = factor(c(rep_len(1, 1000),
                                rep_len(2, 1000),
                                rep_len(3, 1000))))

# To make the angle of this line sharp
df$y[2000:3000] <- df$y[2000:3000] + df$x[2000:3000]

ggplot(data = df) +
    geom_point(aes(x = x, y = y, color = z), size = 3) +
    geom_smooth(aes(x = x, y = y, color = z), method = 'lm', size = 2, fill = NA) +
    scale_color_brewer(palette = 'Set1')

EDIT: As per @Gregor's suggestion, plotting a black line underneath the colored ones does what I want but generates an ugly aliasing effect (which is particularly clear with sharply angled lines) which persists no matter the size of the image(see below image). Any suggestions to deal with this, or is it just a specific problem with my system?

Overlapping geom_smooth lines show ugly aliasing effect

Upvotes: 0

Views: 1274

Answers (2)

Gregor Thomas
Gregor Thomas

Reputation: 145775

Two recommendations you can adjust as you like:

  1. Make the points somewhat transparent
  2. Highlight the line by plotting a lightly bigger black (or gray) line underneath

Here's the result of a little bit of both. I also reduced the size of the points and lines.

ggplot(data = df) +
  geom_point(aes(x = x, y = y, color = z),
             size = 1, alpha = 0.4) +
  geom_smooth(aes(x = x, y = y, group = z),
              color = "black", method = 'lm', size = 1.3, fill = NA) +
  geom_smooth(aes(x = x, y = y, color = z), method = 'lm', size = 1.1, fill = NA) +
    scale_color_brewer(palette = 'Set1')

enter image description here

To deal with aliasing with the overlapping lines, try using the cairoDevice package.

Upvotes: 3

A Duv
A Duv

Reputation: 403

I believe the links will answer better than I can.

You should use scale_color_manual() to color the points manually, or within geom_point() set alpha = 0.4 or something to make the points transparent. http://www.sthda.com/english/wiki/ggplot2-colors-how-to-change-colors-automatically-and-manually

You can also change the shape and size too to distinguish between populations or key individuals within populations as well. http://www.sthda.com/english/wiki/ggplot2-point-shapes

As for the line, you can adjust it manually, and use scale_color_manual(), scale_size_manual(), or scale_linetype_manual().

http://www.sthda.com/english/wiki/ggplot2-line-types-how-to-change-line-types-of-a-graph-in-r-software#change-manually-the-appearance-of-lines

Upvotes: 0

Related Questions