Fitting conditions to a map


So I have a data frame that has all the information from climate_extremes.RData along with the city and country name, no. population, latitude , longitude (from packages 'maps', data (world.cities) ) All of it it's joined, everything good.

In the first bit of code I wanted to have a world map with all the cities as dots.
My problem comes on the second part where I had tried to have every city dot coloured according by the wsdi values. At the same time the city has to be grouped based on scenario and year.

I do not really understand why I get this error when everything is specified in the code? I am quite new to R and trying to learn it through a platform but ... yeah.

Error in check_aesthetics():
! Aesthetics must be either length 1 or the same as the data (939): x and y

map <- ggplot() +borders("world",fill="white",colour="gray80") + geom_point(aes(x=data_frame1$lon, y=data_frame1$lat), colour ="blue", size = 0.003) 

data_frame1 %>% group_by(city,wsdi) %>% filter(scenario == 'historical' & year == '1997') %>%
  select(city) %>%
  ggplot() + borders("world",fill="white",colour="gray80") +
  geom_point(aes(x=lon, y=lat), fill = wsdi , size = 0.03) 

I've also tried with color rather than fill, and tried to specify from where it is (like data_frame1&wsdi)

Thank you in advance!

I am not clear on what you intend with each of your code steps but this caught my eye:

data_frame1 %>% group_by(city,wsdi) %>% filter(scenario == 'historical' & year == '1997') %>%
  select(city) %>%

After that select(city) step, the only column in the data frame will be city, yet the ggplot code that follows tries to use the columns lon and lat. Why are you selecting only the city column?

Oh. You're right.. that happens when you copy paste an example code online.
I tried to select Lon and Lat as well, but I get the same error.
Do I even need to use the select? Seems everything I do I get the same result..Maybe there is a problem with how tidy my data is? How can I find out

Neither the select() nor the group_by() make sense to me. Also, you use fill = wsdi outside of the aes() function. Since wsdi is a column in your data, I don't think that is correct. Try

data_frame1 %>% filter(scenario == 'historical' & year == '1997') %>%
  ggplot() + borders("world",fill="white",colour="gray80") +
  geom_point(aes(x=lon, y=lat, fill = wsdi ), size = 0.03) 

I suspect using fill in the aes() will not do anything since points do not usually have a fill aesthetic. Try color = wsdi as an alternative.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.