2

There are many questions on here about how to get a certain line or point to plot at the top of your data in ggplot2. Currently the way to do this is different for geom_point (which depends on the actual order of the data.frame) and geom_line (which depends on the factor levels).

I am wondering if anyone knows why they are different and if there is any plan to change them. I am hoping if I know why then it can help make it more memorable because currently every time I want to alter plotting order I just end up getting frustrated.

I have made an example to illustrate the point based on this question from years ago

Change the overlaying order of lines in ggplot

Please note I am not asking how to do this (you can see below I know how to do it) but to get more understanding of why it is like this (therefore I don't think this question is a duplicate as far as I can see)

library(ggplot2)
  #data
pl_data <- structure(list(x = c(1, 1.5, 2, 1, 1.3, 1.4, 1.51, 2, 1, 1.31, 2),
                          y = c(0, 0.5, 1, 1, 0.7, 0.7, 0.5, 0, 0.7, 0.7, 0.7),
                          col = c("r", "r", "r", "b", "b", "b", "b", "b", "g", "g", "g")), 
                     row.names = c(NA, -11L), class = c("tbl_df", "tbl", "data.frame"))


#starting plot
plt1 <- ggplot(pl_data, aes(x = x, y = y, color = col)) +
  geom_line(size = 3)+
  scale_colour_manual(values = c("r"="Red","b"="blue","g"="green"))

#change factor to change lines
pl_data2 <- pl_data
pl_data2$col <- factor(pl_data2$col, c("g", "r", "b"))

plt2 <- plt1 %+% pl_data2

#But the points are still the same way around as in original
plt1 + geom_point(size = 5)

plt2 + geom_point(size = 5)

#reorder data to chnage the points
pl_data3 <- pl_data2[order(pl_data2$col),]

plt3 <- plt1 %+% pl_data3
plt3+ geom_point(size = 5)

Created on 2019-11-12 by the reprex package (v0.3.0)

Sarah
  • 3,022
  • 1
  • 19
  • 40
  • 1
    I have upvoted because the question includes working examples of data and code and is easy to follow, which is not the case with all questions so elaborate. But I also think it's off-topic to ask anyone but the developers/maintainers *if anyone knows why they are different and if there is any plan to change them.* Try contacting `maintainer("ggplot2")`? – Rui Barradas Nov 12 '19 at 05:33
  • Hmmm I did wonder if this was the most appropriate place for this question, but I also thought that some people on here might have an idea – Sarah Nov 12 '19 at 05:35
  • Yes, maybe there are but *why* and *plan* are the key words, right? Those are questions about what's going on in their minds... – Rui Barradas Nov 12 '19 at 05:39

0 Answers0