How to change the translucence of ellipses in ggplot?

Viewed 523

I'm making a plot with 100 different ellipses in ggplot2. I want the ellipses/polygons on the plot to be slightly translucent so that the areas with the most lines on them become darker, whereas areas with one ellipse would be quite faint.

The opacity I want vs the opacity im currently getting

However, no matter what I change alpha to, it doesn't seem to have an effect? What am I doing wrong?

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")) 

I've added some data:

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")

Cheers for the help!

1 Answers

The problem is that you're specifying alpha=, but that gets applied to the fill=, not the color= of the line. In geom_line(), for example, you can use alpha= to make the line transparent, but it's not the case with geom_polygon(). Since it's hard to show using your set, let me demonstrate with an example set:

# 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)

enter image description here

Truly, there has never been a more beautiful picture. If we apply alpha= to that, you'll see that only the fill is affected:

p + geom_polygon(aes(color=id), size=1.5, alpha=0.2)

enter image description here

So how do we fix it? Well, you need to specify alpha another way, which is to use alpha(): the function. You may think it could be possible to use within aes() and apply directly to the color= aesthetic... but you'd be wrong. aes(color=alpha(id, 0.2)) will not work, because alpha() expects to be sent the actual color name, not a factor name like ggplot2. You can, however, specify alpha() within scale_color_manual(), which is where we arrive at our answer:

p + geom_polygon(aes(color=id), size=1.5, alpha=0.2) +
  scale_color_manual(values=alpha(c("this"='blue', "that"='red'),0.2))

enter image description here

It might be hard to see from that, but the colors for the lines are transparent. You can compare to the same colors without the alpha setting:

p + geom_polygon(aes(color=id), size=1.5, alpha=0.2) +
  scale_color_manual(values=c("this"='blue', "that"='red'))

enter image description here

Should be obvious now that you'd need to change your scale_color_manual() line. I think this should work:

  scale_color_manual(values = alpha(col_list, 0.2))
Related