Make rings in R ggplot2

Viewed 134

I'm trying to color sections of a target that I have created. I like the color selection. I'm trying to make the transparency of an individual ring less than the surrounding rings. For example, in the picture below for the second circle in... I only want the second ring from the outside to be highlighted (or darker). Does that make sense at all? My code so far is as follows:

#install.packages("ggforce") # for geom_circle()
library(ggplot2)
library(ggforce)
circle_data <- data.frame(
            x0 = c(rep(0.5,4),rep(3,4),rep(5.5,4),rep(8,4)),
            y0 = c(rep(3,4),rep(3,4),rep(3,4),rep(3,4)),
            r = c(rev(seq(0.1, 1, length.out = 4)),rev(seq(0.1, 1, length.out = 4)),rev(seq(0.1, 1, length.out = 4)),rev(seq(0.1, 1, length.out = 4))),
            col = c(rev(c(seq(1,4))),rev(c(seq(1,4))),rev(c(seq(1,4))),rev(c(seq(1,4))))
        )
        circle_data_add2 <- data.frame(
            x0 = rep(3,1),
            y0 = rep(3,1),
            r = rev(seq(0.1, 1, length.out = 4))[2],
            col = rev(c(seq(1,4)))[2]
        )
        circle_data_add1 <- data.frame(
            x0 = rep(0.5,1),
            y0 = rep(3,1),
            r = rev(seq(0.1, 1, length.out = 4))[1],
            col = rev(c(seq(1,4)))[1]
        )
        circle_data_add3 <- data.frame(
            x0 = rep(5.5,1),
            y0 = rep(3,1),
            r = rev(seq(0.1, 1, length.out = 4))[3],
            col = rev(c(seq(1,4)))[3]
        )
        circle_data_add4 <- data.frame(
            x0 = rep(8,1),
            y0 = rep(3,1),
            r = rev(seq(0.1, 1, length.out = 4))[4],
            col = rev(c(seq(1,4)))[4]
        )
        ggplot()+
            geom_circle(aes(x0 = x0,y0 = y0,r = r,fill = col,col = col),alpha = 0.15,data = circle_data)+
            coord_fixed()+
            scale_fill_gradient(low = "red", high = "blue")+
            scale_color_gradient(low = "red", high = "blue")+
            geom_circle(aes(x0 = x0,y0 = y0,r = r,fill = col,col = col),alpha = 0.6,data = circle_data_add1,inherit.aes = FALSE)+
            geom_circle(aes(x0 = x0,y0 = y0,r = r,fill = col,col = col),alpha = 0.6,data = circle_data_add2,inherit.aes = FALSE)+
            geom_circle(aes(x0 = x0,y0 = y0,r = r,fill = col,col = col),alpha = 0.6,data = circle_data_add3,inherit.aes = FALSE)+
            geom_circle(aes(x0 = x0,y0 = y0,r = r,fill = col,col = col),alpha = 0.6,data = circle_data_add4,inherit.aes = FALSE)+
            theme(
                panel.border = element_blank(), 
                panel.grid.major = element_blank(),
                panel.grid.minor = element_blank(),
                panel.background = element_blank(),
                axis.ticks = element_blank(),
                axis.text = element_blank(),
                legend.position = "none"
            )+
            xlab("")+
            ylab("")

This is the output of the current code:

enter image description here

And my desired output, in which only a certain ring is highlighted, is the following:

enter image description here

1 Answers

To make a ring you need to have a smaller circle on top of a bigger circle. So you can hack this together by adding more circles to your plot and thinking carefully about the order you add the circles in. In a way, this is like the Tower of Hanoi game.

enter image description here

circle_data <- data.frame(
  x0 = c(rep(0.5,4),rep(3,4),rep(5.5,4),rep(8,4)),
  y0 = c(rep(3,4),rep(3,4),rep(3,4),rep(3,4)),
  r = c(rev(seq(0.1, 1, length.out = 4)),rev(seq(0.1, 1, length.out = 4)),rev(seq(0.1, 1, length.out = 4)),rev(seq(0.1, 1, length.out = 4))),
  col = c(rev(c(seq(1,4))),rev(c(seq(1,4))),rev(c(seq(1,4))),rev(c(seq(1,4))))
)
circle_data_add2 <- data.frame(
  x0 = rep(3,1),
  y0 = rep(3,1),
  r = rev(seq(0.1, 1, length.out = 4))[2],
  col = rev(c(seq(1,4)))[2]
)
circle_data_add2_negative <- data.frame(
  x0 = rep(3,1),
  y0 = rep(3,1),
  r = rev(seq(0.1, 1, length.out = 4))[3]
)
circle_data_add1 <- data.frame(
  x0 = rep(0.5,1),
  y0 = rep(3,1),
  r = rev(seq(0.1, 1, length.out = 4))[1],
  col = rev(c(seq(1,4)))[1]
)
circle_data_add1_negative <- data.frame(
  x0 = rep(0.5,1),
  y0 = rep(3,1),
  r = rev(seq(0.1, 1, length.out = 4))[2]
)
circle_data_add3 <- data.frame(
  x0 = rep(5.5,1),
  y0 = rep(3,1),
  r = rev(seq(0.1, 1, length.out = 4))[3],
  col = rev(c(seq(1,4)))[3]
)
circle_data_add3_negative <- data.frame(
  x0 = rep(5.5,1),
  y0 = rep(3,1),
  r = rev(seq(0.1, 1, length.out = 4))[4]
)
circle_data_add4 <- data.frame(
  x0 = rep(8,1),
  y0 = rep(3,1),
  r = rev(seq(0.1, 1, length.out = 4))[4],
  col = rev(c(seq(1,4)))[4]
)
ggplot()+
  coord_fixed()+
  scale_fill_gradient(low = "red", high = "blue")+
  scale_color_gradient(low = "red", high = "blue")+
  geom_circle(aes(x0 = x0,y0 = y0,r = r,fill = col,col = col),alpha = 0.6,data = circle_data_add1,inherit.aes = FALSE)+
  geom_circle(aes(x0 = x0,y0 = y0,r = r), fill = "white",alpha = 1,data = circle_data_add1_negative,inherit.aes = FALSE)+
  geom_circle(aes(x0 = x0,y0 = y0,r = r,fill = col,col = col),alpha = 0.6,data = circle_data_add2,inherit.aes = FALSE)+
  geom_circle(aes(x0 = x0,y0 = y0,r = r), fill = "white",alpha = 1,data = circle_data_add2_negative,inherit.aes = FALSE)+
  geom_circle(aes(x0 = x0,y0 = y0,r = r,fill = col,col = col),alpha = 0.6,data = circle_data_add3,inherit.aes = FALSE)+
  geom_circle(aes(x0 = x0,y0 = y0,r = r), fill = "white",alpha = 1,data = circle_data_add3_negative,inherit.aes = FALSE)+
  geom_circle(aes(x0 = x0,y0 = y0,r = r,fill = col,col = col),alpha = 0.6,data = circle_data_add4,inherit.aes = FALSE)+
  # and this one is moved to the end
  geom_circle(aes(x0 = x0,y0 = y0,r = r,fill = col,col = col),alpha = 0.15,data = circle_data)+
  theme(
    panel.border = element_blank(), 
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    axis.ticks = element_blank(),
    axis.text = element_blank(),
    legend.position = "none"
  )+
  xlab("")+
  ylab("")

enter image description here You can play around with the radii calculations to get it to look exactly like your example but the idea is the same.

Related