1

I would like to make this plot:

Plot 1: The plot that I wanted

My data looks like this:

> head(ranges_example)
  labels Minimum Maximum error
1    One    -275    -240     1
2    Two    -265    -210     1
3  Three    -260    -215     1
4   Four    -273    -230     1
5   Five     NaN    -200     1
6    Six     NaN    -240     1

But, alas, I had to make that plot in illustrator by modifying the plot that I did make in R, this one:

Plot 2: The plot that I got

And I made it using geom_linerange, specifically:

ggplot() +
geom_linerange(data = ranges_example, 
               mapping=aes(x = labels, ymin = Minimum, ymax = Maximum, 
                           lwd = 1, color = error, alpha = 0.5),
               position = position_dodge(width = 1)) +
scale_y_continuous(c(-240, -300)) +
coord_flip() 

Plot 2 is good enough for this once--it takes maybe 15 minutes to turn it into Plot 1 in Illustrator--but I'll probably need to make a good few more of these.

The reason why I don't just remove the position_dodge statement is that then it just blends the colors together, like this:

I need them to be their own, distinct colors so that it's easy to tell them apart. The different shades mean different things and I need to be able to easily distinguish between and alter them.

How can I create a plot that looks more like Plot 2 right out of the box?

julianstanley
  • 1,367
  • 4
  • 13
  • 26
  • 1
    Perhaps remove the `alpha = 0.5` - then no blending. And you might need to reverse the direction of the order to keep the narrow linerange on top. – Jon Spring May 24 '19 at 03:37

1 Answers1

2
ggplot() +
  geom_linerange(data = ranges_example %>% arrange(-error), 
                 mapping=aes(x = labels, ymin = Minimum, ymax = Maximum, 
                             lwd = 1, color = error)) +
  scale_y_continuous(c(-240, -300)) +
  scale_color_continuous(high = "lightgreen", low = "forestgreen") +
  coord_flip()  +
  theme_classic()

enter image description here

# Example data
ranges_example <- tribble(
  ~labels, ~Minimum, ~Maximum, ~error,
    "One",    -275,    -240,     1,
    "Two",    -265,    -210,     1,
    "One",    -285,    -215,     2,
    "Two",    -275,    -190,     2,
    "One",    -300,    -200,     3,
    "Two",    -290,    -180,     3)
Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • Beautiful. I felt silly at first seeing that I just had to remove the ‘alpha=0.5’, but I had tried that before—it’s the pipe to ‘arrange(-error)’ that I was missing. That also seems like a generally useful pattern for other things as well. – julianstanley May 24 '19 at 11:08