BlueDevilPride
BlueDevilPride

Reputation: 147

Add select city text labels - ggplot2

How would I go about adding select cities to a state using ggplot2? I searched high and low on here and couldn't find a solution. Below is some code that plots sales opportunity by zipcode in the state of Iowa. How would I just add city text labels for Des Moines and Sioux City? I presume I'm using a geom_text line of code? Thanks for your help.

#Plot
us<-map_data('county', 'iowa')

  #Plot Map
  ggplot(c,aes(longitude,latitude)) +
    geom_polygon(data=us,aes(x=long,y=lat,group=group),color='black',fill=NA,alpha=.75)+
    #geom_point(aes(size = total), color="red3", alpha=.25)+
    geom_point(aes(size = premium), color="dodgerblue3", alpha=I(.75))+
    scale_size(name="Total Remaining Premium (000s)", labels = scales::dollar, breaks = c(25000,100000,500000,1500000),range = c(2, 40))+
    theme_void()+
    theme(legend.title = element_text(face="bold",size=14))+
    theme(legend.text=element_text(size=14))

Upvotes: 0

Views: 588

Answers (1)

timfaber
timfaber

Reputation: 2070

I cannot see the structure of your data c so I first generated the long/lat for sioux and des moines and added a geom_text element:

Let's say you define your plot as p,

  cit = data.frame(long=c(42.5,41.6),
             lat=c(-96.4,-93.6),
             label=c("sioux","des moines"))

  p + geom_text(aes(x = lat, y = long, label = label),
        data= cit,
        alpha = 1,
        color = "black",
        size = 4) # check size

Alternatively you can select those lines in c that provide the long/lat of the cities directly

Upvotes: 1

Related Questions