I've been trying to gain some experience with ggplot2 using Kieran Healy's excellent online book as a starting point, but I've run into a quirk I can't figure out. Using Gapminder data, I'm trying to create a scatterplot showing life expectancy vs GDP per capita. I'd like to include two years of data, distinguishing the years using both color and shape. Finally, I would like to label an outlier, Kuwait in 1952.
I know I could use annotate to do this manually, but I was hoping someone might have a more elegant solution. In addition, I'd love to know why this code, which seems perfectly legitimate to this novice, is not working the way it seems it should. Thanks very much!
library(ggplot2)
library(gapminder)
gap <- subset(gapminder,year==min(year) | year==max(year))
gap$year <- as.character(gap$year)
p <- ggplot(data = gap,
mapping = aes(y = lifeExp,
x = gdpPercap,
col = year))
p + geom_point(aes(shape=year)) + theme_classic() +
scale_x_log10(labels=scales::dollar) +
geom_text_repel(data=subset(gap,gdpPercap>100000),
mapping=aes(label=country)) +
labs(title="Life expectancy by output per capita",
y="",x="GDP per capita")