为geom_point创建标签

时间:2018-11-27 00:04:22

标签: r ggplot2

library(ggplot2)
usa <- map_data("state")
myData <- data.frame(
    states = c("AL","AZ","AR","CA","CO","CT","DE","FL","GA","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY"),
    lat = c(32,33,34,36,39,41,39,38,27,33,44,40,39,42,38,37,31,44,39,42,43,45,32,38,46,41,38,43,40,34,42,35,47,40,35,44,40,41,33,44,35,31,40,44,37,47,38,44),
    long = c(-86,-111,-92,-119,-105,-72,-75,-77,-81,-83,-114,-88,-86,-93,-96,-84,-91,-69,-76,-71,-84,-93,-89,-92,-110,-98,-117,-71,-74,-106,-74,-79,-99,-82,-96,-122,-77,-71,-80,-99,-86,-97,-111,-72,-78,-121,-89,-89),
    pop = c(4.8,7,3,39.5,5.6,3.5,.961,20.9,10.4,1.7,12.8,6.7,3.1,2.9,4.4,4.6,1.3,6,6.8,9.9,5.5,2.9,6.1,1,1.9,2.9,1.3,9,2,19.8,10.2,.755,11.6,3.9,4.1,12.8,1,5,.869,6,28.3,3.1,.623,8.4,7.4,1.8,1,2))

ggplot() +
    geom_path(data = usa, aes(x = long, y = lat, group = group)) +
    geom_point(data = myData, aes(x = long, y = lat, size = pop), color = "blue")

如何将标签添加到正在创建的地图上?

1 个答案:

答案 0 :(得分:2)

  1. 我假设通过“ 如何将标签添加到正在创建的地图上?”表示要添加状态标签。
  2. myData中的数据似乎有问题。各州的名称及其纬度/经度似乎不匹配。

此外,通常您可以通过以下方式添加标签:

ggplot() +
    geom_path(data = usa, aes(x = long, y = lat, group = group)) +
    geom_point(data = myData, aes(x = long, y = lat, size = pop), color = "blue") +
    geom_text(data = myData, aes(long, lat, label = states))

enter image description here