Reputation: 117
I would like to add a point (lollipop alike) at the end of a smooth line in ggplot2.
and this is the graph I would like to get (image modified in an image editor to show):
Example of code:
df.test <- structure(list(Country = c("USA", "USA", "USA", "USA", "USA",
"USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA",
"USA", "USA", "USA", "USA", "USA", "USA", "USA"), startDate = structure(c(18622,
18623, 18624, 18625, 18626, 18627, 18628, 18629, 18630, 18631,
18632, 18633, 18634, 18635, 18636, 18637, 18638, 18639, 18640,
18641, 18642), class = "Date"), endDate = structure(c(20332,
20878, 20819, 20666, 20485, 20332, 20209, 20089, 20028, 19997,
20028, 19967, 19875, 19754, 19662, 19509, 19389, 19358, 19297,
19266, 19236), class = "Date")), row.names = c(NA, -21L), class = c("tbl_df",
"tbl", "data.frame"))
ggplot(df.test, aes(x=startDate, y=endDate)) +
geom_line(color='lightgrey') +
geom_smooth(method="auto", se=FALSE, fullrange=FALSE, size = 0.75) +
theme_light() +
ggtitle('title') +
xlab('Start date') +
ylab('Expected end date')
Thank you.
Upvotes: 0
Views: 114
Reputation: 389
According to this post (https://stackoverflow.com/a/9790803), you may first obtain the data of smooth line as fit
object by the following code:
ggplot(df.test, aes(x=startDate, y=endDate)) +
geom_smooth(aes(outfit=fit<<-..y..), method="auto")
and use that fit
object to draw the point:
ggplot(df.test, aes(x=startDate, y=endDate)) +
geom_line(color='lightgrey') +
geom_smooth(method="auto", se=FALSE, fullrange=FALSE, size = 0.75) +
geom_point(data=tibble(startDate=df.test %>% filter(row_number()==n()) %>% .$startDate,
endDate=structure(fit[length(fit)], class="Date")), colour="purple") +
theme_light() +
ggtitle('title') +
xlab('Start date') +
ylab('Expected end date')
Upvotes: 2