0

Hi this is the last piece of my R script I have points on a map I would like to be able to change the points to different colours to reflect organisations and also add a legend to my map reflecting the different organisations

My data headers are

org code, organisation_name, long, lat

ggplot() + 
  geom_polygon(data = worldmap, 
           aes(x = long, y = lat, 
               group = group), 
           fill = 'gray90', 
           color = 'black') + 
  coord_fixed(ratio = 1.3, 
          xlim = c(-10,3), 
          ylim = c(50, 59)) + 
  theme_void() +
  geom_point(data = Data, aes(x = long, y = lat))
  • If you want color your points you have to map the column containing the orgaization name or type or ... on the `color` aes, i.e. `geom_point(..., aes(..., color = ORGANIZATION))`. This will automatically add a legend. See e.g. https://stackoverflow.com/questions/64888609/how-to-overlay-lat-lon-points-on-a-spatial-map-in-ggplot/64888909#64888909 for an example using different shaqpes – stefan Jan 19 '22 at 11:20
  • when i script like so i'm only left with a list of organisations with colour points but the map is gone ggplot() + geom_polygon(data = worldmap, aes(x = long, y = lat, group = group), fill = 'gray90', color = 'black') + coord_fixed(ratio = 1.3, xlim = c(-10,3), ylim = c(50, 59)) + theme_void() + geom_point(data = Data, aes(x = long, y = lat,color = Provider)) – user2822467 Jan 19 '22 at 12:02

2 Answers2

0

I am not from a background of R but if same I need to do in Mobile App I must pass the lat or long value after removing . and +,- which is 6 digits in the colour parameter Since the Value of lat and long is of 6 digits and hex code is of also 6 digit #123456 it will always give some new colour but maybe sometimes the same shade of colour if lat and long is nearby

fill = #+lat.OnlyNumber(), 
color = #+lat.OnlyNumber()
Gaurav
  • 114
  • 3
  • 7
  • Thanks where would I add fill = #+lat.OnlyNumber(), color = #+lat.OnlyNumber() to ggplot() + geom_polygon(data = worldmap, aes(x = long, y = lat, group = group), fill = 'gray90', color = 'black') + coord_fixed(ratio = 1.3, xlim = c(-10,3), ylim = c(50, 59)) + theme_void() + geom_point(data = Data, aes(x = long, y = lat,color = Provider)) – user2822467 Jan 19 '22 at 12:22
0

you are plotting two different geospatial objects in the ggplot, one is the polygons, the other are points representing organisations. You are correct in using both geom_polygon() and geom_point() in this manner, and keeping the constant, "black" as the polygon colour outside of the aesthetics.

The aesthetics function should not include constants, but rather anything you want to have change, based on the value of some variable, in this case, the name of the organisation. You will need to include the organisation variable as a colour in the aes() for the geom_point().

Although you wish to include the legend, it may be that you have too many organisations, which is why the map disappeared when you added colour as an aesthetic. You can experiment with this by adding:

+ theme(legend.position = "none")

If this brings back the map, you might want to consider ways of reducing the size of your legend - maybe through font size or something similar, or maybe by grouping organisations together and therefore reducing the number of colours required.

  • Thank you when I did the following the points where coloured on the map, however there is no legend visible ggplot() + geom_polygon(data = worldmap, aes(x = long, y = lat, group = group), fill = 'gray90', color = 'black') + coord_fixed(ratio = 1.3, xlim = c(-10,3), ylim = c(50, 59)) + theme_void() + geom_point(data = Data, aes(x = long, y = lat,color = Provider))+ theme(legend.position = "none") – user2822467 Jan 19 '22 at 12:35