Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

geom_point overlay on top of geom_line in R

Tags:

r

ggplot2

Here is a screenshot of a graph made in another software that creates a scatter plot on top of a line graph, with the line hidden where the scatter plot is. This is what I am going for in R.

enter image description here When I run the following code on my graph and data:

dput(my_df)
structure(list(lastFinancingYear = c(2010, 2011, 2012, 2013, 
2014, 2015, 2016, 2017, 2018), raiseMedVal = c(5.33287671232877, 
5.03424657534247, 4.96986301369863, 7.36986301369863, 6.44383561643836, 
7.73835616438356, 8.4958904109589, 9.9054794520548, 9.43013698630137
), foundMedVal = c(11.0821917808219, 10.5178082191781, 8.62191780821918, 
10.2520547945205, 10.9643835616438, 10.9342465753425, 12.9945205479452, 
13.5397260273973, 12.6301369863014)), row.names = c(NA, -9L), class = c("tbl_df", 
"tbl", "data.frame"))

my_df %>% ggplot() +
  geom_line(aes(x = lastFinancingYear, y = raiseMedVal), size = 1.0, color = "#DDBB7B") +
  geom_point(aes(x = lastFinancingYear, y = raiseMedVal), shape = 1, size = 3.0, color = "#DDBB7B") +
  geom_line(aes(x = lastFinancingYear, y = foundMedVal), size = 1.0)

... then i get a graph that looks like this:

enter image description here

Where the scatter markers are present and on top of the line, but the line is not hidden behind the marker, and also the scatter markers do not have a bold/thick enough marker. I am not sure how to fix these, and any help is appreciated!

Thanks in advance!

like image 397
Canovice Avatar asked Dec 08 '22 14:12

Canovice


1 Answers

I created geom_pointline for this exact situation (with some flair):

library(lemon)
library(tidyr)

my_df %>%
 gather(stat, val, raiseMedVal, foundMedVal) %>%
 ggplot(aes(lastFinancingYear, val, colour=stat)) + 
 geom_pointline(distance=0.1, fill='white', shape=21, size=3.5, stroke=2, linesize=2)

Notice that I am changing the dataframe to a long format, such that the colour is linked to a variable and not hardcoded. Secondly, the aesthetics are compounded in the main ggplot call to avoid redundant settings. (And my apoligies that distance had to be 0.1, not 0).

enter image description here

To control the colours and appearence of legend, use the usual ggplot2 functions, i.e. scale_colour_manual (or _hue, or _brewer, or...).

Edit: Wow, I learnt about using stroke to control the border width of the points. Thanks @Marius.

like image 158
MrGumble Avatar answered Dec 29 '22 06:12

MrGumble