0

I have the following dataset:

df <- structure(list(id = c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 
2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 
5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 
7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 
10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 
12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 
14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 
16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17), year = c(2011, 
2012, 2013, 2014, 2015, 2016, 2017, 2018, 2011, 2012, 2013, 2014, 
2015, 2016, 2017, 2018, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 
2018, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2011, 2012, 
2013, 2014, 2015, 2016, 2017, 2018, 2011, 2012, 2013, 2014, 2015, 
2016, 2017, 2018, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 
2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2011, 2012, 2013, 
2014, 2015, 2016, 2017, 2018, 2011, 2012, 2013, 2014, 2015, 2016, 
2017, 2018, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2011, 
2012, 2013, 2014, 2015, 2016, 2017, 2018, 2011, 2012, 2013, 2014, 
2015, 2016, 2017, 2018, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 
2018, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2011, 2012, 
2013, 2014, 2015, 2016, 2017, 2018, 2011, 2012, 2013, 2014, 2015, 
2016, 2017, 2018), ave = c(36.1, 25.1, 31.4, 30.8, 30.5, 30.2, 
26.8, 31, 29.5, 26.2, 25.5, 31, 30.4, 28.1, 30.3, 30.1, 14.3, 
26, 21.9, 17.1, 14.4, 10.3, 7.5, 18.7, 21.2, 21.9, 16, 12.4, 
17.9, 17.9, 13.5, 17.7, 21.8, 27.4, 27, 25.7, 23.4, 21, 18.2, 
17.2, 16.3, 14.9, 16.4, 18.1, 15.8, 12.5, 13.5, 13.6, 14.2, 14.8, 
14.8, 13.4, 15.3, 12.6, 12.8, 11.6, 17.5, 15.3, 14.2, 20.9, 7, 
8.7, 5, 6.2, NaN, NaN, NaN, 7.9, 5, 15.9, 12.8, 6.1, 14.6, 9.6, 
3.1, 7.5, 5.1, 1.3, 2.5, 5.7, 4.5, 9.8, 8.4, 9.2, -3.5, 4.1, 
4.2, 3, 9.2, 9.7, 10.3, 7.2, 10.5, 7.3, 10.9, 0.6, 1, 0, -0.3, 
-3.4, -6.8, -7.7, -2.4, -0.5, 0.3, 11.9, 2.6, 6.2, 7.5, 4.7, 
0.9, -1.1, 0.1, -4.9, -2.9, -1.7, -3.4, -1.2, -0.7, -2.9, 22.5, 
NaN, NaN, 12.1, 6.3, 19.4, 17, -7, 8.1, 10.8, 6.4, NaN, -6.3, 
-8.4, 4, NaN)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-136L))

And I'm using this ggplot2 code to make the graph below.

library(tidyverse)
df %>%
  mutate(id = as.factor(id)) %>% 
  ggplot(aes(year, ave, color = id, group = id))+
  geom_line(size = 1.25, position = position_dodge(width = .1))+
  geom_point(size = 3, position = position_dodge(width = .1))

enter image description here

It may be hard to see, but the lines and points in the plots seem to be at a different 'depth'. If you look at the x-axis label 2016, you will see that the blue line goes below the pink but the blue point is below the pink point.

I'd like to lay the lines and points on the same plane. If possible I'd like the 'top' lines/points to be plotted last, so they are easier to view.

Is this possible using dplyr?

elliot
  • 1,844
  • 16
  • 45
  • The _first_ factor level in your `color` variable is plotted _last_, so you need to set factor levels in desired order. Smaller example: `d <- data.frame(x = 1, y = 1, z = c("a", "b"))`; Default levels of 'z' is `c("a" and "b")`, but here I set levels explicitly for demo: `ggplot(d, aes(x, y, color = factor(z, levels = c("a", "b")))) + geom_point(position = position_dodge(width = 0.04), size = 40) + xlim(0.9, 1.1)`; "a" plotted on top of "b". – Henrik Feb 21 '19 at 22:09
  • To plot "b" after "a", reverse order of levels: `ggplot(d, aes(x, y, color = factor(z, levels = c("b", "a")))) + geom_point(position = position_dodge(width = 0.04), size = 40) + xlim(0.9, 1.1)` - – Henrik Feb 21 '19 at 22:09
  • Check that plotting order is independent of the order in the data set: `d <- data.frame(x = 1, y = 1, z = c("b", "a"))`; `ggplot(d, aes(x, y, color = z)) + geom_point(position = position_dodge(width = 0.04), size = 40) + xlim(0.9, 1.1)` – Henrik Feb 21 '19 at 22:16

1 Answers1

1

if you want to highlight certain lines try this as shown here:

df <- df %>% 
  mutate(id=as.factor(id))   
df_highlight <- df %>%   
  group_by(id) %>%    
  filter(id %in% c(15,16,17))

df %>% 
  ggplot(aes(year, ave, color = id, group = id)) +   
  geom_line(size=1.25, alpha = 0.2) +    
  geom_point(size=3, alpha =0.2) +
  geom_line(data=df_highlight,size=1.25) +   
  geom_point(data=df_highlight,size=3)

plot

Also note by adding additional geom layers we ensure those get drawn after earlier geoms and will therefore be on top.

Art
  • 1,165
  • 6
  • 18