Reputation: 1721
Here is part of R code
# create a new grouping variable
Percent_SEP12_Assets <- ifelse(sep[,8] <= 33, "Lower Third", ifelse(sep[,8] >= 55, "Upper Third", "Middle Third"))
# get the map
bbox <- make_bbox(sep$Longitude, sep$Latitude, f = 0.3)
map <- get_map(bbox)
# plot the map and use the grouping variable for the fill inside the aes
ggmap(map) +
geom_point(data=sep, aes(x = Longitude, y = Latitude, color=Percent_SEP12_Assets ), size=5, alpha=0.6) +
scale_color_manual(breaks=c("Upper Third", "Middle Third","Lower Third"), values=c("green","orange","red"))
But instead of the map making
"Upper Third" correspond with Green "Middle Third" correspond with Orange "Lower Third" correspond with Red
The color scheme is mixed up, i.e. Upper Third corresponds with Red, and Lower Third corresponds with Green.
Higher numbers = good = green, but the map shows opposite. How to fix this?
The following code
# create a new grouping variable
Percent_SEP12_Assets <- ifelse(sep[,8] <= 33, "Lower Third", ifelse(sep[,8] >= 55, "Upper Third", "Middle Third"))
Percent_SEP12_Assets <- factor(Percent_SEP12_Assets)
levels(Percent_SEP12_Assets) <- c("Upper Third", "Middle Third", "Lower Third")
# get the map
bbox <- make_bbox(sep$Longitude, sep$Latitude, f = 0.3)
map <- get_map(bbox)
# plot the map and use the grouping variable for the fill inside the aes
ggmap(map) +
geom_point(data=sep, aes(x = Longitude, y = Latitude, color=Percent_SEP12_Assets ), size=9, alpha=0.6) +
scale_color_manual(values=c("green","orange","red"))
Will give this, which corrects the data labels, but the points in the map are inverted, i.e. where it is green it is red and vice versa (points in blue circle should be red)
But when I reversed "red" and "green" in original code, it works (area in blue circle supposed to be red), but I believe this is a "band-aid" approach
# create a new grouping variable
Percent_SEP12_Assets <- ifelse(sep[,8] <= 33, "Lower Third", ifelse(sep[,8] >= 55, "Upper Third", "Middle Third"))
# get the map
bbox <- make_bbox(sep$Longitude, sep$Latitude, f = 0.3)
map <- get_map(bbox)
# plot the map and use the grouping variable for the fill inside the aes
ggmap(map) +
geom_point(data=sep, aes(x = Longitude, y = Latitude, color=Percent_SEP12_Assets ), size=9, alpha=0.6) +
scale_color_manual(breaks=c("Upper Third", "Middle Third","Lower Third"), values=c("red","orange","green"))
Upvotes: 1
Views: 875
Reputation: 2828
Turn Percent_SEP12_Assets
into a factor variable and specify the order of the levels:
# create a new grouping variable
Percent_SEP12_Assets <- ifelse(sep[,8] <= 33, "Lower Third", ifelse(sep[,8] >= 55, "Upper Third", "Middle Third"))
Percent_SEP12_Assets <- factor(Percent_SEP12_Assets,
levels = c("Upper Third", "Middle Third", "Lower Third"))
# get the map
bbox <- make_bbox(sep$Longitude, sep$Latitude, f = 0.3)
map <- get_map(bbox)
# plot the map and use the grouping variable for the fill inside the aes
ggmap(map) +
geom_point(data=sep, aes(x = Longitude, y = Latitude, color=Percent_SEP12_Assets ), size=5, alpha=0.6) +
scale_color_manual(values=c("green","orange","red"))
Upvotes: 1