ggplot geom_point 上的点大小发生变化且没有图例?

CDa*_*Dav 4 maps r ggplot2 ggmap

好的,这是我的底图代码:

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()
Run Code Online (Sandbox Code Playgroud)

然后我添加我希望手动命名的城镇(不知道如何使用谷歌地图来做到这一点,因为我只想要这 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
Run Code Online (Sandbox Code Playgroud)

我得到了这张可爱的地图: 在此输入图像描述

然后我使用这个数据集添加数据点,我希望根据“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
Run Code Online (Sandbox Code Playgroud)

使用此代码:

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
Run Code Online (Sandbox Code Playgroud)

然而,当我这样做时......我得到这个: 在此输入图像描述

数据点绘制得很好(唷!),但为什么它使我的城镇名称的点更大?我似乎无法让它只保留我的“n”个数据点的丰富性...它也不会自动添加图例(正如 ggplot 通常所做的那样),即使我尝试使用手动添加一个图例scale_discrete 函数。

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

有任何想法吗?

sha*_*dow 5

问题是,在添加城镇的代码中,您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)
Run Code Online (Sandbox Code Playgroud)