What is the best way to plot points having coordinates in the same column with R?
For instance, having this dataframe :
df <- data.frame(city=c("Paris", "Paris", "New-York", "New-York"), year=c(2010,2016,2010,2016), val=c(10,-10,100,-100))
city year val
1 Paris 2010 10
2 Paris 2016 -10
3 New-York 2010 100
4 New-York 2016 -100
I want to have a plot using val of 2010 on the x-axis and val of 2016 on the y-axis (that is Paris at 10,-10 and New-York at 100,-100): plot example
What I usually do is subsetting/merging to have different columns :
df.2010 <- df[df$year == 2010, c("city","val")]
colnames(df.2010) <- c("city","val.2010")
df.2016 <- df[df$year == 2016, c("city","val")]
colnames(df.2016) <- c("city","val.2016")
df.m <- merge(df.2010, df.2016)
Giving :
city val.2010 val.2016
1 New-York 100 -100
2 Paris 10 -10
And then:
ggplot(df.m, aes(x=val.2010,y=val.2016,label=city)) + geom_label()
But I found this method inelegant and also very heavy with bigger data. Does anyone know a better way to achieve the same goal? Either by better using ggplot or subsetting/merging more elegantly, perhaps?
Many thanks!