One color for each column

I want to get one colour per column in this example but I only get one uniform colour, what should I add?

df <- data.frame(
        date = c("2021-08-06","2021-08-05","2021-08-04",
                 "2021-08-03","2021-08-02","2021-08-01","2021-07-31",
                 "2021-07-30","2021-07-29","2021-07-28","2021-07-27","2021-07-26",
                 "2021-07-25","2021-07-24","2021-07-23","2021-07-22",
                 "2021-07-21","2021-07-20","2021-07-19","2021-07-18","2021-07-17",
                 "2021-07-16","2021-07-15","2021-07-14","2021-07-13",
                 "2021-07-12","2021-07-09","2021-07-08","2021-07-07","2021-07-06",
                 "2021-07-05","2021-07-02","2021-07-01"),
       plant = c(16L,21L,18L,28L,29L,23L,23L,19L,16L,
                 20L,21L,25L,24L,24L,27L,30L,30L,22L,24L,17L,12L,
                 9L,9L,7L,3L,5L,5L,4L,2L,4L,4L,1L,2L),
        care = c(7L,7L,7L,7L,7L,7L,6L,6L,6L,5L,5L,
                 3L,3L,2L,2L,2L,1L,1L,0L,0L,0L,0L,0L,0L,0L,0L,0L,
                 0L,0L,0L,0L,0L,0L)
)

ggplot(df, aes(x = date, y = plant)) +
  geom_col() +
  scale_fill_identity(guide = "none", aesthetics = "fill")

I get this:

Is this what you want?

ggplot(df, aes(x = date, y = plant, fill = date)) +
  geom_col(show.legend = FALSE)

Yes, that's right, perfect

It's possible to change the colour palette? Thanks for your help

Select one of the scale_fill_*() options:
Function reference • ggplot2 (tidyverse.org)

Thanks again martin
But with any of the palettes I try to do it, I get this error message

In RColorBrewer::brewer.pal(n, pal) :
  n too large, allowed maximum for palette YlOrRd is 9
Returning the palette you asked for with that many colors

I'm not really sure why you are trying to get different colours per bar, but just define them manually. Discrete pre-defined palettes will not have the number of different colours you require for obvious reasons.

Alternatively, convert the date column to actual dates and use a continuous scale:

df$date <- as.Date(df$date)

ggplot(df, aes(x = date, y = plant, fill = date)) +
  geom_col(show.legend = FALSE) + 
  scale_fill_continuous()
1 Like

Got it, thanks a lot!

Regards.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.