r - ggplot geom_point 上的点改变大小而没有图例?

标签 r ggplot2 maps ggmap

好的,这是我的 basemap 代码:

gg <- ggmap(Peru) +
  geom_map(data = peru.coast, map = peru.coast, aes(x = long, y = lat, map_id = region),
           fill="gray", color="black") +
  xlim(-86, -70) +
  ylim(-20, -4) + 
  labs(x = "Longitude", y = "Latitude") +
  coord_map()

然后我添加了我想手动命名的城镇(不知道如何使用谷歌 map 来命名,因为我只想要这 4 个)
gg <- gg + geom_point(aes(x=-78.981885, y=-8.229354, size=3)) +
  annotate("text", label='Salaverry', size=4, x=-77.2, y=-8.229354) +
  geom_point(aes(x=-71.345838, y=-17.644347, size=3)) +
  annotate("text", x=-70.545838, y=-17.644347, label = 'Ilo', size=4) +
  geom_point(aes(x=-77.142375, y=-12.047544, size=3)) +
  annotate("text", x=-75.9, y=-12.047544, label = 'Callao', size=4) +
  geom_point(aes(x=-78.610677, y=-9.074166, size=3)) +
  annotate("text", x=-76.9, y=-9.074166, label = 'Chimbote', size=4)
gg <- gg + guides(size=FALSE) #this removes the legend with the black dot and '3' on it 
gg

我得到了这张可爱的 map :

然后我使用这个数据集来添加数据点,我希望根据“n”丰度使点更大或更小
Trip_Set sex        Set.Lon     Set.Lat     n
119_1    hembra -81.09390   -9.32338    2
119_7    hembra -81.03117   -9.09622    1
161_3    macho  -83.76533   -9.74533    5
193_8    hembra -81.00888   -9.00950    7
255_5    macho  -80.14992   -8.64592    1
271_6    hembra -72.20233   -18.05117   6
271_6    macho  -72.20233   -18.05117   7
328_7    hembra -78.66667   -12.91700   2
403_3    hembra -80.03037   -10.03900   1
428_2    hembra -83.01305   -8.74883    2
655_4    hembra -71.58363   -18.24882   1

使用此代码:
ggAB <- gg + geom_point(data=dframe4, aes(Set.Lon, Set.Lat, colour='red', size=n)) 
ggAB <- ggAB + theme(legend.title = element_text(colour="black", size=12, face="bold")) 
ggAB <- ggAB +  guides(colour=FALSE) #This removes the legend for the red colour
ggAB <- ggAB +  scale_size(name='Sharks per line', range = c(5,9)) 
ggAB <- ggAB +  theme(legend.key=element_rect(fill = NA)) #This removes the boxes around the points 
ggAB

但是,当我这样做时......我明白了:

数据点绘制得很好(呸!),但为什么它使我的城镇名称的点更大?我似乎无法让它只保留我的“n”个数据点的丰度......它也没有放置自动图例(就像 ggplot 通常所做的那样),即使我尝试手动放置一个使用scale_discrete 函数。

我认为这可能与我在第一部分使用 gg + guides(size=FALSE) 的事实有关,但即使将其取出也不起作用,但为我的城镇数据点添加了一个烦人的图例。

有任何想法吗?

最佳答案

问题在于,在添加城镇的代码中,您已将 size 放在 aes 中。因此,当您调用 scale_size(name='Sharks per line', range = c(5,9)) 时它也会被转换。只需在 size 之外使用 aes :

gg <- gg + geom_point(aes(x=-78.981885, y=-8.229354), size=3) +
  annotate("text", label='Salaverry', size=4, x=-77.2, y=-8.229354) +
  geom_point(aes(x=-71.345838, y=-17.644347), size=3) +
  annotate("text", x=-70.545838, y=-17.644347, label = 'Ilo', size=4) +
  geom_point(aes(x=-77.142375, y=-12.047544), size=3) +
  annotate("text", x=-75.9, y=-12.047544, label = 'Callao', size=4) +
  geom_point(aes(x=-78.610677, y=-9.074166), size=3) +
  annotate("text", x=-76.9, y=-9.074166, label = 'Chimbote', size=4)

关于r - ggplot geom_point 上的点改变大小而没有图例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27522011/

相关文章:

r - 我可以告诉 dplyr 的 select() 获取索引行吗?

sql - 如何在 R 中表示非规范化表?

r - naiveBayes为什么会在R中返回所有NA以进行多类分类?

r - ggplot2中离散发散尺度的中点

r - 如何根据以前的方 block 递归创建方 block ?

r - R Markdown 中带有 pdf_document 输出的表格和绘图之间的换行符

android - 在 Android 中加载 MapView 瓦片

ios - MKMap 不显示 Open Street Map 瓦片覆盖

swift - 搜索和调用兴趣点的电话号码

r - Data.table:组合列(聚合的个别功能)