HI I am using ggmap and gg_point function to show the measured data in an estuary.
The code that I used is as follows:
library(ggmap) al1 <- get_map(location = c(lon = -87.525, lat = 30.35), zoom = 12, maptype = 'terrain') lon<- c(-87.604474,-87.55) lat<- c(30.362563,30.35) label <- c("A","B") df<-data.frame(lon,lat,label) p <- ggmap(al1)+geom_point(data=df,aes(x=lon,y=lat,shape=label,label=label),size=3) p <- p + xlab("Longitude")+ylab("Latitude") p <- p +geom_text(aes(label=label, size=3,vjust=0)) p <- p + labs(title="Monitoring stations ") p ggsave("plot.pdf")
Here when I use geom_text then I get the following error, "Aesthetics must either be length one, or the same length as the dataProblems:label".
I want to place the label next to the points in the plot. I would like to place both points and label and with some spacing so that it would be easier to read.
I looked at this post "ggplot legend issue w/ geom_point and geom_text" and tried to fix my code as you can see above but I don't know why I am having this issue.
There is another post How can I persuade ggplot2 geom_text to label a specified date in a time series plot? which talks the similar questions. Am I getting different results because I am using ggmap also ?
Please help me to solve this problem. Thank you so much.
Jdbaba
Your problem is that you haven't specified the aesthetics in geom_text
correctly:
geom_text(data = df, aes(x = lon, y = lat, label = label),
size = 3, vjust = 0, hjust = -0.5)
You didn't tell geom_text
to use the variables from the data frame df
. If you don't do this, all aesthetics are inherited from the main call. Finally, when setting aesthetics to a single value, you don't do this inside of aes()
, but outside.
I monkeyed with the hjust
setting to get the labels to be visible.