How to create a geom line plot with single geom point at the end with legend
For geom_point
and geom_text
, you can subset your dataframe by keeping only the last year (2018):
ggplot(df, aes(x = `Gss year for this respondent`, y = Percent*100))+
geom_line(aes(color = `Political party affiliation`), show.legend = FALSE)+
geom_point(data = df[df$`Gss year for this respondent` == max(df$`Gss year for this respondent`),],
aes(x =`Gss year for this respondent`, y = Percent*100, color = `Political party affiliation`), show.legend = FALSE)+
geom_text(data = df[df$`Gss year for this respondent` == max(df$`Gss year for this respondent`),],
aes(x =`Gss year for this respondent`,
y = Percent*100,
colour = `Political party affiliation`,
label = `Political party affiliation`), show.legend = FALSE, hjust = -0.1)+
xlim(1975, 2026)+
ylim(0,80)+
scale_color_manual(values = c("blue","green","red"))
Here are a few updates to @dc37's answer that get OP closer to desired plot. To be clear, @dc37's answer solves the main problem: how to plot just the final dot in the series. All of this was too much to add as a comment to @dc37's answer, so I'm posting it as a separate solution - although it's really more an extension of @dc37's answer, rather than a distinct alternative.
Updates
Fixes x-axis range so that annotations occur after axis stops
Removes color from annotations
Eliminates space between x-axis line and y=0 on plot
Creates space around x-axis tick marks (space between axis line and tick text)
Major y-axis grid lines displayed
Made x- and y- ticks match the original
ggplot(df, aes(x = `Gss year for this respondent`, y = Percent*100, color = `Political party affiliation`)) + geom_line(show.legend = FALSE, size = 1) + geom_point(data = point_df, aes(x =`Gss year for this respondent`), show.legend = FALSE, size = 2) + scale_x_continuous(breaks = seq(1980, 2015, by = 5)) + scale_y_continuous(limits = c(0, 80), breaks = seq(0, 80, by = 20), expand = c(0, 0)) + scale_color_manual(values = c("deepskyblue1","gray80","firebrick3")) + geom_text(data = point_df, aes(x = 2019, y = Percent*100, label = `Political party affiliation`), color = "black", show.legend = FALSE, hjust = -0.1) + coord_cartesian(xlim = c(1978, 2016), clip = 'off') + theme(plot.margin = unit(c(1,7,1,1), "lines"), panel.grid.major.y = element_line(color = "gray90"), axis.line.y = element_blank(), axis.ticks.length.x = unit(.3, units = "cm"), axis.ticks.x = element_line(linetype = 2)) + labs(y = "", x = "")
Important
The graph produced from OP's data - replicated in OP's code, as well as this answer and @dc37's answer - looks slightly different than the original screenshot. (Look at how the lines don't overlap each other in the same way.) I haven't dug into this - the updates here are focused on cosmetics.