简体   繁体   English

如何在ggplot中的geom_polygon中正确填充颜色?

[英]How to fill colors correctly in geom_polygon in ggplot?

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. 我正在尝试绘制散点图,然后覆盖一些多边形并在ggplot 2中填充具有特定颜色的多边形,并且我在填充时获得正确的颜色有一些问题。 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). 我有两个颜色列表,一个用于散点图中的点,一个用于多边形的边框和填充,原因有两个列表,是因为我按数据列网络对点进行分组,并希望在所有点周围绘制多边形,级别为1-5(但不是6和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. 一切正常,除了填充颜色不同,边框和点都是相同的,正确的颜色,但我不能得到填充颜色匹配。

You are trying to apply base plot methods to ggplot by using vectors of colors directly as a col argument. 您正在尝试通过使用颜色向量作为col参数将基本绘图方法应用于ggplot。 It is much easier to use the proper ggplot way of mapping colors to variables in your data.frame. 使用适当的ggplot方法将颜色映射到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 : 然后我们需要的是两层,以及为colfill group的离散( factor )映射:

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) . 如果要在调色板中保留缺少的组(4),则需要使用命名向量作为values ,因此在这种情况下,您可以提供setNames(col_list, 1:7)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM