Problem with creating maps

HI all!

I'm very new to R and this is my first post asking for help. Hope it works.

I'm trying to make maps using a dataframe that I have. I'm working in RStudio and am using ggplot2 and maps packages. I have a variable in my dataframe called 'Ten year FDI'/ I want to color the map based on that variable, using the following code:

> ggplot(data = Combined_FDI_maps) + 
    geom_polygon(aes(x = long, y = lat, fill = Ten year FDI, group = group), color = "white") + 
    coord_fixed(1.3) +
    guides(fill=FALSE)

I get two error messages which I don't understand (you can also see the attached image):

Error: unexpected symbol in:
"ggplot(data = Combined_FDI_maps) +
geom_polygon(aes(x = long, y = lat, fill = Ten year"
coord_fixed(1.3) +

  • guides(fill=FALSE)
    

Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?

Interestingly, when I change my variable to another one ('region'), then all works our perfectly (you can see the result in the map that is shown in the attached image)

In there any solution to this problem? I would greatly appreciate your support!

This is a non-syntacticall variable name (also a bad practice in R) you have to reference it surrounded by backticks, like this `Ten year FDI`

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

1 Like

What is the point in having Antractica when you want to plot FDI. Economise on computer resources by reducing polygon.

1 Like

Got the point. Thanks! I think I can do that

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

Thanks so much! This solves the issue.

You should be able to filter the Combined_FDI_maps data frame based on some value; as your example is not quite reproducible I can not give a more specific hint.

Also consider that the {maps} package is by now not really bleeding edge stuff, and drawing maps from fortified polygons is no longer considered best practice. It still works, but geom_sf is more flexible and works with the {sf} format, which seems to be the new cool kid.

You may also consider the excellent {tmap} package, it uses as "hello world" example something very similar to your task: https://cran.r-project.org/web/packages/tmap/vignettes/tmap-getstarted.html

Thanks you. Will definitely need to play with the map. Is there an easy way to reduce a polygon? Sorry for a dumb question. I really am extremely newbie to this.

I won't be able to give you the code here. But, the principle is that if you get it as a dataframe then you can use dplyr to filter out the relevant rows minus the antarctica for the rest of the countries.

Posting for the first time here, I had not realized how quickly one can get very useful advice. Thanks a lot!