Reputation: 91
Would very much appreciate some assistance in helping me color the geom_points using 5 different colors. Each point in the map represents a ZipCode. The data is laid out as follows
ID zip city state latitude longitude num colorBuckets
-------------------------------------------------------------------------------
1 00210 xxx NH 43 -71.013 10 1
2 45013 yyy OH 43 -88.304 200 5
.
.
I would like the colorBucket 5
to appear as a dark red, colorBucket 4
to appear as a lighted red and finally colorBucket 1
to appear as a very light grey. Can someone help me with the code?
Here's what I have so far:
g <- ggplot(data=mapdata,
aes(x=longitude, y=latitude)) +
geom_point(aes(fill=colorBuckets), size = 0.04) +
scale_x_continuous(limits = c(-125,-66), breaks = NULL) +
scale_y_continuous(limits = c(25,50), breaks = NULL) +
labs(x=NULL, y=NULL)
I cant post an image yet [because i dont have 10 reputations yet - not sure that the even means].
But the current plot is coming up with all BLACK dots and the legend is showing various shades of blue with a scale going from 1 to 5.
I have tried unsuccessfully adding the following piece of code to the end of this snipper:
scale_fill_manual(values=c("<20"="red", "21 - 60" = "blue", "61 - 80" = "green", "81-100" = "yellow"))
scale_fill_brewer(palette="<palette name>")
scale_colour_nrewer(palette="<palette name>")
Any help will be greatly appreciated.
Thanks,
Upvotes: 2
Views: 1219
Reputation: 91
So this is what i ended up doing thanks to Gregor's feedback.
g <- ggplot(data=mapdata,
aes(x=longitude, y=latitude)) +
geom_point(aes(color=factor(colorBuckets)), size = 0.04) +
scale_x_continuous(limits = c(-125,-66), breaks = NULL) +
scale_y_continuous(limits = c(25,50), breaks = NULL) +
labs(x=NULL, y=NULL) +
theme_bw() +
scale_color_manual(values=c("bisque", "darkseagreen2", "maroon", "red")) +
geom_polygon(data=states, aes(long, lat, group=group), fill=NA, colour="#000000") +
labs(x="", y="", title="Some Title")
Upvotes: 0
Reputation: 145745
Had another comment, so I figured I should just post an answer. A few things going on here:
check your column classes. If ggplot
shows you a continuous color scale (as you describe), it's because your data is continuous. If you want 5 distinct colors, turn colorBuckets
into a factor
with geom_point
, you should set color
, not fill
. (There's the edge-case if you use shapes 21:25 then you can set both color and fill
With the above changes, you can then use scale_color_manual
if you know each of the colors you want, but from your description I think you'd do well with
+ scale_color_brewer(palette = "Reds")
Upvotes: 1