Labeling center of map polygons in R ggplot

后端 未结 2 1402
后悔当初
后悔当初 2020-12-09 04:49

I am trying to label my polygons by using ggplot in R. I found a topic here on stackoverflow that I think is very close to what I want except with points.

Label poi

2条回答
  •  庸人自扰
    2020-12-09 05:26

    The accepted answer here may work, but the actual question asked specifically notes that there is an error "ggplot2 doesn't know how to deal with data of class uneval."

    The reason that it is giving you the error is because the inclusion of centroids.df needs to be a named variable (e.g. accompanied by "data=")

    Currently:

    ggplot(data = region_of_interest.fort, aes(x = long, y = lat, fill = id, group = group)) +
      geom_polygon() +
      geom_text(centroids.df, aes(label = id, x = Longitude, y = Latitude)) +
      scale_fill_gradient(high = "green", low = "red", guide = "colorbar") +
      coord_equal() +
      theme() +
      ggtitle("Title")
    

    Should be (note: "data=centroids.df"):

    ggplot(data = region_of_interest.fort, aes(x = long, y = lat, fill = id, group = group)) +
      geom_polygon() +
      geom_text(data=centroids.df, aes(label = id, x = Longitude, y = Latitude)) +
      scale_fill_gradient(high = "green", low = "red", guide = "colorbar") +
      coord_equal() +
      theme() +
      ggtitle("Title")
    

    This issue was addressed here: How to deal with "data of class uneval" error from ggplot2?

提交回复
热议问题