Reputation: 265
I am plotting a map with different study sites of malaria parasite drug resistance. The points of the study sites are sized relative to how many malaria parasites were sampled and the fill of the points is a gradient from 0 to 1 of the proportion of malaria parasites that were drug resistant. The following code reveals the map below:
###GENERATING AFRICA MAP###
africa = readOGR("/Users/transfer/Documents/Mapping Files/Africa Countries/Africa_SHP", layer="dissolved")
#FIXING THE NON-NODED INTERSECTS#
africa = gBuffer(africa, width=0, byid=TRUE)
#CREATING DATA FRAME FOR GGPLOT#
africa.map = fortify(africa, region="ID")
###PLOTTING SPM.437###
#SCALING THE SAMPLE SIZE USING CUBE-ROOT#
size = d.spm.437$Tot437^(1/3)
#PLOTTING#
ggplot(africa.map, aes(x = long, y = lat, group = group)) +
geom_polygon(colour="black", size=0.25, fill="white", aes(group=group)) +
geom_point(data = d.spm.437, aes(x = long, y = lat, fill=Frc437, group=NULL, size=Tot437),
size=size, shape=21, colour="black", size=0.5)
I tried using a color option but it did not work:
ggplot(africa.map, aes(x = long, y = lat, group = group)) +
geom_polygon(colour="black", size=0.25, fill="white", aes(group=group)) +
geom_point(data = d.spm.437, aes(x = long, y = lat, colour="red", fill=Frc437, group=NULL, size=Tot437),
size=size, shape=21, colour="black", size=0.5)
Anybody know how to get the color of the fill to show a scale of red, lighter being 0 and darker being 1?
Upvotes: 4
Views: 3788
Reputation: 173577
As I mentioned in my comment, I think you're a little confused about setting vs mapping aesthetics.
You map aesthetics to a variable in your data frame inside of aes()
. So, aes(color = var)
maps the color aesthetic to the variable var, and you get a legend to show how color varies with var. If you set color, outside of aes()
, to a single value, then you are simply setting all points to be a single color: color = "red"
.
The reason I suspect you're confused is that you have size=Tot437
inside of aes()
, and then both size=size
and size=0.5
outside of aes()
.
Finally, to change the color palette, you need to be aware of the scale_color_*
functions (as well as scale_fill_*, scale_size_*
etc). Here's a simple example to get you started:
dat <- data.frame(x = rnorm(50),
y = rnorm(50),
f = runif(50))
library(ggplot2)
library(munsell)
cl <- mnsl(c("5R 2/4", "5R 7/10"))
ggplot(dat,aes(x,y,fill = f)) +
geom_point(size = 5,shape = 21) +
scale_fill_gradient(low = cl[1],high = cl[2])
How did I come up with that crazy looking color specification? Just from the first few lines of scale_fill_continuous
and then poking around in the munsell package a bit.
Edit: I completely missed that you were actually using pch = 21
which is basically the only point shape for while the fill
aesthetic makes sense, so I've edited to remove my comments on that score.
Upvotes: 5