点数在ggplot geom_point上改变大小而没有传说?

时间:2014-12-17 09:18:36

标签: r ggplot2 maps 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()

然后我在城镇中添加我想手动命名的名称(不知道如何使用Google地图进行操作,因为我只想要这些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

我得到了这张可爱的地图: enter image description here

然后我使用这个数据集添加数据点,我希望根据&#39; n&#39;丰度

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

然而,当我这样做时......我明白了: enter image description here

数据点的绘制很棒(p!),但为什么它会使我的城镇名称更大?我似乎无法让它为我的&#39; n&#39;保持丰富。数字数据...它也没有自动设置图例(如ggplot通常那样),即使我尝试使用scale_discrete函数手动设置一个。

我认为这可能与我在第一部分中使用gg + guides(size=FALSE)这一事实有关,但即使这样做也无法奏效,但为我的城镇增添了烦人的传说数据点。

有什么想法吗?

2 个答案:

答案 0 :(得分:4)

问题是,在添加城镇的代码中,您已将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)

答案 1 :(得分:2)

除了@ shadow的回答之外,我还想为OP提供以下内容作为补充信息。这来自与OP的聊天。如果您想避免使用annotate,可以在geocode包中使用ggmap。在这里,我从上一个OP问题的答案中添加了一些内容,并且我合并/修改了OP的代码。一个变化是我使用了alpha,这样你就可以看到海洋中的红色/粉红色点。需要注意的是,城市名称的位置并不完美;你在地图上向南走得越远,你就越能看到点和城市名称之间的差距。这可能与地图投影有关。根据Wiki,googlemap使用的是接近marcator的东西,但不完全一样。这可能是原因。这里的一些GIS专家将能够提供更多信息。

library(ggmap)
library(mapdata)
library(ggplot2)

# Get Peru map
Peru <- get_map(location = "Peru", zoom = 5, maptype="satellite") 

# This is the layer I wish to put over the top
coast_map <- fortify(map("worldHires", fill = TRUE, plot = FALSE)) 

# Subset data for Peru
peru.coast <- subset(coast_map, region == "Peru")

### Get lon and lat using geocode() in the ggmap package and crate a data frame
cities <- c("Salaverry", "Chimbote", "Callao", "Ilo")
locations <- geocode(cities)
locations$city <- cities
locations2 <- transform(locations, lon2 = lon + 1.1) # This is for text position


ggmap(Peru) +
geom_map(data = peru.coast, map = peru.coast, aes(x = long, y = lat, map_id = region),
         fill="gray", color="black") +
geom_point(data = locations2, aes(x = lon, y = lat, color = city), size = 4) +
geom_text(data = locations2, aes(x = lon2, y = lat, label = city), size = 3) +
scale_color_manual(values = rep(c("black"), times = 4)) + 
geom_point(data = newdata, aes(Set.Lon, Set.Lat, size = n), colour = "red", alpha = 0.5) +
scale_size(name = "Sharks per line", range = c(5,9)) +
xlim(-86, -70) +
ylim(-20, -4) + 
labs(x = "Longitude", y = "Latitude") +
coord_map("mercator") +
guides(colour=FALSE) +
theme(legend.key=element_rect(fill = NA))

enter image description here