Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Points changing sizes on ggplot geom_point and no legend?

Okay so here is my code for a base map:

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()

I then add in towns I wish to name manually (wasn't sure how to do it using google maps as I only wanted these 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

I get this lovely map: enter image description here

I then use this dataset to add datapoints, and I wish to make the points bigger or smaller according to 'n' abundance

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

using this code:

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

However, when I do this... I get this: enter image description here

The datapoints are plotted great (phew!), but why does it make the points bigger for my town names? I can't seem to get it to just keep the abundance for my 'n' number datapoints... It also doesn't put an automatic legend on (as ggplot usually does), even when I try and put one in manually using the scale_discrete function.

I thought it might be something to do with the fact that I use gg + guides(size=FALSE) in the first part, but even when taking that out it doesn't work, but adds in an annoying legend for my town datapoints.

Any ideas?

like image 542
CDav Avatar asked Oct 15 '25 03:10

CDav


1 Answers

The problem is that in the code where you add the towns, you have put the size inside the aes. Therefore it also gets transformed when you call scale_size(name='Sharks per line', range = c(5,9)). Just use size outside the 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)
like image 192
shadow Avatar answered Oct 16 '25 16:10

shadow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!