user55546
user55546

Reputation: 62

Different colors for the bar graph bars in ggplot2

I am trying to color the chart below using the colors of the RColorBrewer package, in which for the CONTROL group the colors would be the shades of gray and black for INITIAL and FINAL respectively and for the IRRADIATED group the colors would be light red a darker red for INITIAL and FINAL respectively, however, the code is only entering the colors gray and black.

library(RColorBrewer)
library(ggplot2)

dados4$VARIAVEIS = as.factor(dados4$VARIAVEIS)
dados4$PERIODO = as.factor(dados4$PERIODO)
dados4$GRUPOS  = as.factor(dados4$GRUPOS)


errors <- dados4 %>%
  group_by(VARIAVEIS,PERIODO,GRUPOS) %>%
  summarise(mean = mean(Resposta), 
            sd = sd(Resposta),
            sem = sd(Resposta)/sqrt(length(Resposta)))

gap3 <- aggregate(Resposta ~ VARIAVEIS + PERIODO + GRUPOS, data=dados4, FUN=mean)
gap31 <- gap3 %>% left_join(errors)

x11()
graf2=ggplot(gap31, aes(x = VARIAVEIS, y = Resposta, fill = factor(PERIODO))) +
  geom_col(position = "dodge") + 
  geom_errorbar(aes(ymin=mean-1.96*sd,ymax=mean+1.96*sd),position = position_dodge(0.9))+
  facet_grid(~ GRUPOS) + theme(strip.text.x=element_text(size = 15)) +
  labs(title = "",
       x = "Variáveis",
       y = expression(paste('Deformação'," ",'(',mu, m,')')),
       fill = "Períodos") +
theme(legend.title = element_text(size = 15),legend.text = element_text(size = 15),
      axis.text.x = element_text(color = "black", hjust=1),
      axis.title = element_text(size = 15),
      axis.text = element_text(size = 12)) 
graf2+scale_fill_manual(values=c("#4D4D4D","black","#FCBBA1","#DE2D26"))

Upvotes: 1

Views: 153

Answers (1)

Duck
Duck

Reputation: 39595

Try this approach:

library(RColorBrewer)
library(ggplot2)
#Load data
dados4 <- read.csv('DADOSGRAPHBARRA.csv',stringsAsFactors = F,sep=';')

dados4$VARIAVEIS = as.factor(dados4$VARIAVEIS)
dados4$PERIODO = as.factor(dados4$PERIODO)
dados4$GRUPOS  = as.factor(dados4$GRUPOS)
#Compute summaries
errors <- dados4 %>%
  group_by(VARIAVEIS,PERIODO,GRUPOS) %>%
  summarise(mean = mean(Resposta), 
            sd = sd(Resposta),
            sem = sd(Resposta)/sqrt(length(Resposta)))
gap3 <- aggregate(Resposta ~ VARIAVEIS + PERIODO + GRUPOS, data=dados4, FUN=mean)
#Merge
gap31 <- gap3 %>% left_join(errors)

Now the plot:

#Plot
ggplot(gap31, aes(x = VARIAVEIS, y = Resposta,
                  fill = interaction(PERIODO,GRUPOS))) +
  geom_col(position = "dodge") +
  geom_errorbar(aes(ymin=mean-1.96*sd,ymax=mean+1.96*sd),position = position_dodge(0.9))+
  facet_grid(~ GRUPOS) + 
  theme(strip.text.x=element_text(size = 15)) +
  labs(title = "",
       x = "Variáveis",
       y = expression(paste('Comprimentos e Diâmetros'," ",'(',mu, m,')')),
       fill = "Períodos") +
  theme(legend.title = element_text(size = 15),legend.text = element_text(size = 15),
        axis.text.x = element_text(color = "black", hjust=1),
        axis.title = element_text(size = 15),
        axis.text = element_text(size = 12))+
  scale_fill_manual(values=c("black","#4D4D4D","#DE2D26","#FB6A4A"))

Output:

enter image description here

Upvotes: 2

Related Questions