RColorBrewer help!

I am creating a circular histogram with ggplot to plot my circular data. I am trying to add color to it, but it keeps producing a plot without color. Any suggestions?

ggplot(data = df, aes(x = time.active)) +
geom_histogram(breaks = seq (0,24),
binwidth = 1) +
coord_polar(start = 0) +
ylab ("Count") +
labs (title = "Human Activity by Time of Day\nIn Madre Selva Research Station",
subtitle = "subtitle)+
scale_fill_brewer(palette = "PuOr") +
mytheme_present1 +
scale_x_continuous("",
limits = c(0, 24),
breaks = seq(0, 24),
labels = seq(0, 24))

The aesthetic should be geom_bar, not geom_histogram. See the gallery for detailed examples.

1 Like

One way to add color to geom_histogram is by passing a vector of colors to the fill argument. The length of the vector needs to match the number of breaks in your plot. In the example below (which is a modified version of your plot using the mtcars dataset), I create the colors vector by combining three different RColorBrewer palettes together. Since the length of this vector is greater than the 24 breaks of the plot, I create the plot_colors vector, which is a random sample of 24 colors from the colors vector.

library(tidyverse)
library(RColorBrewer)

# create a vector of RColorBrewer colors from various palettes
colors = c(brewer.pal(n = 11, name = 'PuOr'),
           brewer.pal(n = 8, name = 'Blues'),
           brewer.pal(n = 8, name = 'Reds')
           )

# the plot has 24 breaks, so randomly sample 24 colors
set.seed(44)
plot_colors = sample(colors, 24, replace = F)

ggplot(data = mtcars, aes(x = cyl)) +
  geom_histogram(breaks = seq (0,24),
                 binwidth = 1,
                 fill = plot_colors # colors added here
                 ) +
  coord_polar(start = 0) +
  ylab ("Count") +
  labs (title = "Human Activity by Time of Day\nIn Madre Selva Research Station",
        subtitle = "subtitle") +
  scale_x_continuous("",
                     limits = c(0, 24),
                     breaks = seq(0, 24),
                     labels = seq(0, 24))

image

Created on 2022-11-09 with reprex v2.0.2.9000