The following describes in a simplified manner an issue into which I run often but never get it to work. It has to do with superposing layers with different information.
Any help is appreciated.
library(dplyr)
library(ggplot2)
df = data.frame(id = c(1,1,1,1,2,2,2,3,3,3),
year = c(2000,2001,2002,2003,2000,2001,2002,2001,2002,2003),
meas = c(1.1,1.2,NA,1.4,2.1,1.9,1.8,3.1,3.0,3.3))
df$id = as.factor(df$id)
df$year = as.factor(df$year)
tib = as.tbl(df)
which produces the tibble:
# A tibble: 10 x 3
id year meas
<fctr> <fctr> <dbl>
1 1 2000 1.1
2 1 2001 1.2
3 1 2002 NA
4 1 2003 1.4
5 2 2000 2.1
6 2 2001 1.9
7 2 2002 1.8
8 3 2001 3.1
9 3 2002 3.0
10 3 2003 3.3
Presently, I compute the median of the meas-ures for each year in the cross section.
tib2 = tib %>% group_by(year) %>%
summarise(medi=median(meas,na.rm=T))
tib2
Which gives as expected
# A tibble: 4 x 2
year medi
<fctr> <dbl>
1 2000 1.60
2 2001 1.90
3 2002 2.40
4 2003 2.35
So far so good. Now, I create my first plot
p = ggplot(tib, aes(x = year, y = meas, group = id, color = id))
p = p + geom_line()
p
which produces
On the other hand, the following produces a set of fat dots as it should
p1=ggplot(tib2, aes(x = year, y = medi)) + geom_point(colour = 'red', size = 3)
p1
I try however to place the red dots in the first figure like so:
p = p + geom_point(data = tib2, aes(x = year, y = medi), colour = 'red', size = 3)
p
This is where I get the error:
Don't know how to automatically pick scale for object of type function. Defaulting to continuous. Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, the arguments imply different numbers of lines : 4, 0
Remark: I noticed that the missing value induces a cut in the red line which I might wish to interpolate. When I replace the NA by some value, the error persists however.




p = p + geom_point(...)?aes(x = year...)might be misinterpreted (islubridateloaded by any chance?) What happens if you doggplot() + geom_line(data = tib2, aes(x = year, y = medi), colour = 'red', size = 3)