Reputation: 97
I am trying to draw a scatterplot, then overlay some polygons and fill in the polygons with specific colors in ggplot 2 and I am having some issue getting the right colours for the fill. I can draw the scatterplot, and the polygons, and colour the scatterplot and the border of the polygons the sames, but when i try to fill the polygons, they always come out different colours or give me errors.
My data looks like this,
id lat long group
Ak 0.5109300 30.43713 1
Ak 0.5109300 30.43713 2
An 0.4709994 30.43434 1
An 0.4860330 30.44015 2
At 0.4956100 30.44610 2
At 0.4938700 30.44640 2
At 0.4837816 30.44658 3
Be 0.4932194 30.43455 3
Bo 0.4922330 30.44582 1
Bo 0.4922330 30.44582 3
cb 0.4929994 30.44486 5
de 0.4926486 30.45684 5
de 0.5000001 30.45331 5
eg 0.4854526 30.46824 6
eh 0.4765586 30.46987 6
gh 0.4822123 30.54835 7
I have two lists of colours, one for the points in the scatterplot, and one for the border and fill of the polygons, the reason there is two lists, is because I am grouping the points, by the data column network, and want to draw polygons around all the points with levels 1 -5 (but not 6 and 7). So I create the following color lists
col_list<-c("#FF222C", "#1DFBFF", "#FDFF24", "#2CFF18", "#FF38F4", "#C3C4C9", "#000000")
col_list5<-c("#FF222C", "#1DFBFF", "#FDFF24", "#2CFF18", "#FF38F4")
I draw the plot with
g<-ggplot(data= samples, aes(x=long, y=lat))+
geom_point(color = col_list[samples$group])+
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill=NA, size=1))
I then use the following code to generate the polygons
library(plyr)
samples_group1_5<-subset(samples, network < 6)
find_hull <- function(samples_group1_5) samples_group1_5[chull(samples_group1_5[,3], samples_group1_5[,2]), ]
hulls <- ddply(samples_group1_5, "group", find_hull)
g.col <- col_list5[hulls$group]
And then overlay the polygons with
g+
geom_polygon(data = hulls, alpha = 0.5, aes(fill=factor(group))) +
geom_polygon(data = hulls, alpha = 0, aes(group=factor(group)), colour=g.col)
Everything works, except the fill colors are different, the borders and the points are both the same, correct colours, but I cannot get the fill colors to match.
Upvotes: 3
Views: 25662
Reputation: 35392
You are trying to apply base plot methods to ggplot by using vectors of colors directly as a col
argument. It is much easier to use the proper ggplot way of mapping colors to variables in your data.frame. Here's what I would do:
First, calculate the hulls (your code didn't work for me):
library(dplyr)
hulls <- samples %>%
group_by(group) %>%
do(.[chull(.[2:3]), ])
Then all we need is two layers, and a discrete (factor
) mapping to group
for both col
and fill
:
p <- ggplot(samples, aes(x = long, y = lat, col = factor(group), fill = factor(group)))+
geom_polygon(data = hulls, alpha = 0.3) +
geom_point()
Giving you some nice colors automagically, and you get a legend for free!
Now, if we want to use your colors instead, we have to introduce scales:
p +
scale_color_manual(values = col_list) +
scale_fill_manual(values = col_list)
If you want to keep the missing group (4) in the palette, you need to use a named vector for values
instead, so in this case you can supply setNames(col_list, 1:7)
.
Upvotes: 8