How do I change the fill of individual circles in ggforce::geom_circle to custom hex code colors?

164 Views Asked by At

For the following geom_circle() example:

library(ggplot2)
library(ggforce)
circles <- data.frame(
  x0 = rep(1:3, 3),
  y0 = rep(1:3, each = 3),
  r = seq(0.1, 1, length.out = 9)
)

plt <- ggplot() +
  geom_circle(aes(x0 = x0, y0 = y0, r = r), data = circles)
plt

It correctly produces the following plot:

I wanted to fill the individual circles based on a vector of hex codes, for example colors <- rep("#00FF00",9)

However, when trying to manipulate the coloring with the following code:

colors <- rep("#00FF00",9)
ggplot() +
   geom_circle(aes(x0 = x0, y0 = y0, r = r), fill = colors, data = circles)

It produces the following error:

Error in `geom_circle()`:
! Problem while setting up geom aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (3249)
✖ Fix the following mappings: `fill`

The number of circles is clearly 9, so I'm assuming that the actual geom_circle() transforms the input into a bunch of smaller datapoints or something like that. In any case, how would one go about transforming the colors individually? A potential solution is to iteratively add each circle individually but in my usecase there are thousands of circles that need to be added.

2

There are 2 best solutions below

0
stefan On BEST ANSWER

One option would be to add you colors as a column to your dataset which could be mapped on the fill aes and by adding scale_fill_identity.

library(ggplot2)
library(ggforce)

circles$colors <- rep("#00FF00", 9)

ggplot() +
  geom_circle(aes(x0 = x0, y0 = y0, r = r, fill = colors), data = circles) +
  scale_fill_identity() +
  coord_fixed()

enter image description here

0
Eirik Fesker On

The geom_circle() function is indeed generating a large number of points to plot each circle, which is why you are seeing an error when you try to set the fill color to a vector of length 9.

One way to color each circle individually is to use the scale_fill_manual() function to manually assign colors to each circle. You can do this by adding a column to your circles data frame that specifies the color for each circle and then mapping that column to the fill aesthetic in geom_circle().

enter image description here

Here's my example:

circles <- data.frame(
  x0 = rep(1:3, 3),
  y0 = rep(1:3, each = 3),
  r = seq(0.1, 1, length.out = 9),
  color = rep(c("#00FF00", "#FF0000", "#0000FF"), each = 3)
)

ggplot(circles) +
  geom_circle(aes(x0 = x0, y0 = y0, r = r, fill = color)) +
  scale_fill_manual(values = unique(circles$color))

In this example, I added a new column called color to the circles data frame and assigned a unique color to each circle. Then, in geom_circle(), I mapped the color column to the fill aesthetic. Finally, I used scale_fill_manual() to manually assign the colors to each circle using the unique values in the color column.