Home > Software engineering >  Order Grouped Geom_lines in ggplot
Order Grouped Geom_lines in ggplot

Time:06-12

I am wishing to show multiple geom_points in order of "season_pts" from each group "drafted_qbs". The issue though is that I'm not sure what to assign the other variable. I have a "team" column which is just the row number of each group but that will only order the first grouping "2".

Any way of laying on the same graph (not interested in faceting) each groups "fantasy_pts" in order of points would be helpful.

Data

structure(list(team = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), 
season_pts = c(447.44, 381.62, 416.6, 367.96, 419.92, 490.78, 
501.66, 458.56, 484.48, 458.36, 518, 495.7, 511.34, 499.68, 
536.42, 522.92, 536.92, 518.46, 538.06, 525.96, 541.84, 523.26, 
542.98, 527.4, 527), drafted_qbs = c(2, 2, 2, 2, 2, 3, 3, 
3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -25L))

CodePudding user response:

It usually helps to ask yourself "what is it I am trying to show with this plot?"

If you are trying to show that an increased number of drafted QBs tends to produce an increased number of points, then you can do something like this:

ggplot(df, aes(drafted_qbs, season_pts))  
  geom_point(size = 4, aes(color = factor(team)))  
  geom_smooth(color = 'gray20', size = 0.5, linetype = 2, alpha = 0.15)  
  scale_color_brewer(palette = 'Set1')  
  theme_light(base_size = 16)  
  labs(x = 'Drafted QBs', y = 'Season Points', color = 'Team')  
  theme(panel.grid.minor.x = element_blank())

enter image description here

If you want to show that not all teams are affected equally by this effect, then something like this might be preferable:

ggplot(df, aes(team, season_pts, color = drafted_qbs))  
  geom_point(size = 4, alpha = 0.5)  
  scale_color_gradient(low = 'red3', high = 'blue3')  
  theme_light(base_size = 16)  
  labs(x = 'Team', y = 'Season Points', color = 'Drafted QBs')  
  theme(panel.grid.minor.x = element_blank())

enter image description here

  • Related