0

I want to create a map with ggmap where you can see which species found where and how many times. The "how many" should be represented by the size of the point/bubble. But I could imagine representing by colour (like a heatmap) would also be fine, and then seperating the species by shaping the points. What I've got so far: For the background map:

B<-get_map(location = c(lon = -56.731405, lat =-61.4831206),
           zoom = 6, 
           scale = "auto",
           maptype = c("terrain"),
           source = c("google"))

For the scatter plot:

D<-ggmap(B) + 
geom_point(data=Anatomy,
aes(x=Anatomy$Longitude,y=Anatomy$Latitude,
color=Species,
size=??) +
scale_size_continuous(range=c(1,12))

My data=Anatomy looks like this:

  Species Lat Long Station
1       A  50   60       I
2       A  50   60       I
3       A  40   30      II
4       B  50   60       I
5       B  40   30      II
6       C  50   60       I
7       C  10   10     III
8       C  10   10     III
9       C  40   30      II

My idea was to use dplyr and filter by rows and sum the categories somehow. Or what do you think? And do you think this is the best way to present this data?

markus
  • 25,843
  • 5
  • 39
  • 58
  • 1
    See this R-FAQ question on why you basically never want `$` inside an `aes`: https://stackoverflow.com/q/32543340/5325862 – camille Mar 03 '19 at 19:30

1 Answers1

0

welcome to StackOverflow! To help people help you, you should strive to provide a reproducible example. Here for example would be a small code excerpt for your data:

library(tidyverse)
Anatomy <- tribble(~Species,  ~Lat, ~Long, ~Station,
                "A",  50,   60,       "I",
                "A",  50,   60,       "I",
                "A",  40,   30,      "II",
                "B", 50,   60,       "I",
                "B",  40,   30,      "II") 

There are several problems with your data/code:

  • projection: you will most likely need to reproject the data. Just looking at the coordinates, your points are 50, 60, while the map shows -50, -60. Find out the projection, and use for example st_transform from package sf
  • quoting of variables: you do not need to call the data-frame again, as in Anatomy$Latitude. Just use Latitude. Plus latitude is actually lat in your data!?
  • aggregation: I would suggest just using the count() function to see the number of observations per station.

Here is a piece of code. Note I just reverse (60, 50) to (-60, -50) which is obviously wrong!

library(ggmap)
#> Loading required package: ggplot2
#> Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
#> Please cite ggmap if you use it! See citation("ggmap") for details.
library(tidyverse)

B<-get_map(location = c(lon = -56.731405, lat =-61.4831206),
           zoom = 6, 
           scale = "auto",
           maptype = c("terrain"),
           source = c("google"))

library(tidyverse)
Anatomy <- tribble(~Species,  ~Lat, ~Long, ~Station,
                "A",  50,   60,       "I",
                "A",  50,   60,       "I",
                "A",  40,   30,      "II",
                "B", 50,   60,       "I",
                "B",  40,   30,      "II") 

Anatomy_clean <- Anatomy %>% 
  mutate_at(c("Lat", "Long"), funs(-1*.)) %>% 
  count(Species, Lat, Long, Station)
#> Warning: funs() is soft deprecated as of dplyr 0.8.0
#> please use list() instead
#> 
#> # Before:
#> funs(name = f(.)
#> 
#> # After: 
#> list(name = ~f(.))
#> This warning is displayed once per session.


ggmap(B) + 
  geom_point(data=Anatomy_clean,
             aes(x= Lat,y=Long, color=Species, size= n)) +
  scale_size_continuous(range=c(1,12))
#> Warning: Removed 2 rows containing missing values (geom_point).

Matifou
  • 7,968
  • 3
  • 47
  • 52
  • "count" sounds good but it tells me "Error in count(., Species, Latitude, Longitude, Station) : unused arguments (Longitude, Station)" – Toastershock91 Mar 03 '19 at 19:26
  • it looks like your data has `Lat`, not `Latitude`? – Matifou Mar 03 '19 at 20:08
  • I created a new dataframe where really only those 4 factors where in and then just did "count(dataframe)". Then I did "geom_point( ... size =freq)". And now it works. Thank you very much! – Toastershock91 Mar 04 '19 at 08:13