0

How can I add scale_fill_gradient for both columns in dodge_position?

Code:

 ggplot(df, aes(x=msc, y=as.integer(value), fill=variable)) + 
        geom_col(position='dodge')+
        theme(legend.position = "bottom")+
        labs(x = "Miesiąc",y="Ilość zapytań")+
        theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1,size = 11),
              axis.title = element_text(size = 12))+
        geom_text(aes(label=value),position =position_dodge(width = 1),vjust = -0.2) + 
        scale_fill_manual("legend", values = c("unikalne_zapytania" = '#009999', "zapytania" = '#4d0099')) +
        ggtitle("Wykres zapytań za ostatni rok") +
        guides(fill=guide_legend(title='Rodzaj'))

enter image description here

Final example: enter image description here

brtk
  • 107
  • 7
  • 1
    Please add a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). That way you can help others to help you! – dario Feb 10 '20 at 11:39
  • Also: The 'final example' plot does **not** use `scale_fill_gradient` (it uses ``scale_fill_discrete` because cyl is a factor. – dario Feb 10 '20 at 11:55

1 Answers1

1

Without a minimal reproducible example it's difficult to help you, but I think the thing you ask for is impossible. You are using the categorical variable variable to group the bars, but now you want to use a continuous variable to color the same bars. Anyway, here is a suggestion how to achieve something similar as your request:

library(ggplot2)
mtcars %>%
  mutate(group = sample(c("A", "B"), nrow(mtcars),  replace=TRUE)) %>% 
  group_by(group, cyl) %>% 
  summarise(mean_mpg = mean(mpg),
            mean_disp = mean(disp)) %>% 
ggplot(aes(factor(cyl), mean_mpg)) +   
  geom_bar(aes(color=group, fill = mean_disp), position = "dodge", stat="identity")

Resulting in this figure:

enter image description here

dario
  • 6,415
  • 2
  • 12
  • 26