我正在ggplot2中绘制100个不同的椭圆图。我希望绘图上的椭圆/多边形稍微半透明,以使上面带有最多线条的区域变暗,而带有一个椭圆的区域将变得很暗。
The opacity I want与the opacity im currently getting
但是,无论我将alpha
更改为什么,它似乎都没有效果?我在做什么错了?
ggplot(data = cssibernobk, aes(iso1, iso2)) +
scale_fill_manual(name = "Marlin Species", labels = c("Blue Marlin", "Striped Marlin"),values=c(col_list)) +
geom_polygon(data = ellipse_dfcs,
show.legend = FALSE,
mapping = aes(iso1, iso2,
group = ellipse_dfcs$group,
color = factor(group),
fill = factor(group)), fill = NA, alpha = 0.2) +
scale_color_manual(values = col_list)+
geom_point(colour="black", shape=21, size = 2,
aes(fill = factor(group))) +
ylab(expression(paste(delta^{34}, "S (\u2030)")))+
xlab(expression(paste(delta^{13}, "C (\u2030)")))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))
我添加了一些数据:
dput(cssibernobk[1:10,])
structure(list(iso1 = c(-19.0666624548854, -17.8943955612859,
-18.9928609367509, -16.4710761874456, -18.2727144857982, -20.5069897964015,
-20.5966996037602, -17.5454265065267, -16.4393689881177, -17.4617536511442
), iso2 = c(19.4934410976126, 19.2364832949763, 19.7701196888491,
19.837594669008, 19.7147010622141, 20.3802264543413, 20.4655925249838,
19.0895171633117, 19.7264242586365, 19.2537859386917), group = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1), community = c(1, 1, 1, 1, 1, 1, 1,
1, 1, 1)), row.names = c(NA, -10L), class = c("tbl_df", "tbl",
"data.frame"))
dput(ellipse_dfcs[1:10,])
structure(list(id = c("1", "1", "1", "1", "1", "1", "1", "1",
"1", "1"), iso1 = c(-15.3019878272902, -15.4740783680741, -15.6530442971316,
-15.8381649818314, -16.0286950065133, -16.2238671740144, -16.4228955949008,
-16.6249788519676, -16.8293032272625, -17.0350459786413), iso2 = c(20.3654052739855,
20.44444500941, 20.5195305006846, 20.5903594050369, 20.6566465194678,
20.718124929163, 20.7745470822651, 20.8256857866775, 20.8713351248865,
20.9113112831188), community = c("1", "1", "1", "1", "1", "1",
"1", "1", "1", "1"), group = c("1", "1", "1", "1", "1", "1",
"1", "1", "1", "1")), row.names = c(NA, 10L), class = "data.frame")
为帮助喝彩!
答案 0 :(得分:1)
问题是您指定的是alpha=
,但是它应用于行的fill=
,而不是行的color=
。例如,在geom_line()
中,可以使用alpha=
使行透明,但是geom_polygon()
则不是这样。由于很难使用您的布景来显示,因此让我用一个示例布景来演示:
# I dunno, I'm making shapes...
d <- data.frame(id=c(rep('this',4),rep('that',4)), x=c(0,1,1,0.5,0.5,2,0.5,0.5), y=c(0,0,2,3,0.5,2,2,1))
p <- ggplot(d, aes(x,y)) + theme_bw()
p + geom_polygon(aes(color=id), size=1.5)
确实,从未有过如此美丽的图画。如果我们对此应用alpha=
,您会看到只有填充会受到影响:
p + geom_polygon(aes(color=id), size=1.5, alpha=0.2)
那么我们如何解决呢?好吧,您需要以另一种方式指定alpha,即使用alpha(): the function
。您可能会认为可以在aes()
中使用并直接应用于color=
美学……但是您会错的。 aes(color=alpha(id, 0.2))
无法正常工作,因为alpha()
希望发送给您实际的颜色名称,而不是像ggplot2
这样的要素名称。不过,您可以在alpha()
中指定scale_color_manual()
,这就是我们得出答案的地方:
p + geom_polygon(aes(color=id), size=1.5, alpha=0.2) +
scale_color_manual(values=alpha(c("this"='blue', "that"='red'),0.2))
从中可能很难看到,但是线条的颜色是透明的。您可以在没有alpha设置的情况下与相同颜色进行比较:
p + geom_polygon(aes(color=id), size=1.5, alpha=0.2) +
scale_color_manual(values=c("this"='blue', "that"='red'))
现在很明显,您需要更改scale_color_manual()
行。我认为这应该可行:
scale_color_manual(values = alpha(col_list, 0.2))