changing colour using aes command

Hi,

I have coordinates of 27 locations, with different coordinates(lat and long), with a value that is a statistic, this value varies every location.

I have the map and points on the map working fine, however i cannot change my colour scheme in the aes function, they only do the default. I have tried lots of different commands. I would like a red-blue gradient colour with matching legend. I tried colourRpalette etc, but no luck. Help would be appreciated.

**Code below
orld_map <- map_data("world")

#Create a base plot with gpplot2
p <- ggplot() + coord_fixed() +
xlab("") + ylab("")

#Add map to base plot
base_world_messy <- p + geom_polygon(data=world_map, aes(x=long, y=lat, group=group),
colour="gray65", fill= "gray65")
base_world_messy

###############add data points#################
cities <- read.csv("Coordinates_updated.csv", header=TRUE)

map_data_coloured <-
base_world_messy +
geom_point(data=cities,
aes(x=Longitude, y=Latitude, colour=Value), size=4, alpha=I(1))

map_data_coloured

Hi @bella29,
Welcome to the RStudio Community Forum.

Try this:

library(ggplot2)
world_map <- map_data("world")

# Create some dummy city data
cities <- data.frame(Latitude=c(21,32,43,54,65),
                     Longitude=c(110,92,53,-84,-65),
                     Value=c(10.6, 13.8, 4.9, 8.2, 25.1))

fun_color_range <- colorRampPalette(c("red", "blue")) # Create color generating function
my_colors <- fun_color_range(20)                      # Generate color range
my_colors                                             # Show color range (hex values)
#>  [1] "#FF0000" "#F1000D" "#E4001A" "#D60028" "#C90035" "#BB0043" "#AE0050"
#>  [8] "#A1005D" "#93006B" "#860078" "#780086" "#6B0093" "#5D00A1" "#5000AE"
#> [15] "#4300BB" "#3500C9" "#2800D6" "#1A00E4" "#0D00F1" "#0000FF"

# Create a base plot with ggplot2
p <- ggplot() + coord_fixed() +
       xlab("") + ylab("")

# Add map to base plot
base_world_messy <- p + geom_polygon(data=world_map, aes(x=long, y=lat, group=group),
                                     colour="gray65", fill= "gray65")
base_world_messy


# add city data points
map_data_coloured <-
  base_world_messy + 
    geom_point(data=cities, aes(x=Longitude, y=Latitude, colour=Value), size=4) +
    scale_colour_gradientn(colors = my_colors)

map_data_coloured

Created on 2022-08-13 by the reprex package (v2.0.1)

Thank you.

If I wanted to outline some of the coordinates what would you suggest?

HI @bella29,
You can use any of the "filled" plotting symbols (pch 21...25). In this way you can specify the "fill" colour of the symbol and the colour (and thickness) of the "outline". e.g.

# Change symbol background fill and line color
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
   geom_point(shape = 21, fill = "lightblue", color = "red",  size = 3)

This topic was automatically closed 21 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.