0

I know this question has been asked before, but I can't get the accepted answer to work. Here's my dataset:

library(ggplot2)
library(scales)    
full_data<-NULL
full_data$race<-c(1,2,3,4,1,2,3,4,3,4,3,4,4)
full_data$race<-as.factor(full_data$race)
full_data$Year<-sample(2000:2005,13,replace=TRUE)
full_data$number<-sample(5:10,13,replace=TRUE)
full_data$program<-c(rep(1,6),rep(2,7))
full_data<-as.data.frame(full_data)
program1<-subset(full_data, full_data$program==1)
program2<-subset(full_data, full_data$program==2)
identical(levels(program1$race),levels(program2$race))

I made sure the levels for race were the same in each dataset, but when I made two bar plots, even with the named vector from the other answer, the colors are different between the bar plots.

MyPalette <- c("1" = "#5DD0B9", "2" = "#E1E7E9", "3" = "#1f78b4", 
           "4" =  "#a6cee3")

p1_chart<-ggplot(data=program1, aes(x=program1$Year, y=program1$number, fill=program1$race)) +
  geom_bar(stat="identity", position=position_dodge())+
  scale_colour_manual(values = MyPalette)
p1_chart

p2_chart<-ggplot(data=program2, aes(x=program2$Year, y=program2$number, fill=program2$race)) +
geom_bar(stat="identity", position=position_dodge())+
   scale_colour_manual(values = MyPalette)
p2_chart

I'd really appreciate any ideas!

Community
  • 1
  • 1
user5457414
  • 137
  • 3
  • 7

1 Answers1

0

just to show you where the colour_scale is actually coloring, using your MyPalette for the fill_scale:

library(RColorBrewer)

p1_chart<- ggplot(data=program1, aes(x=Year, y=number, fill=race, colour = race)) +
  geom_bar(stat="identity", position=position_dodge())+
  scale_fill_manual(values = MyPalette) +
  scale_color_brewer(palette = "Dark2") +
  theme_bw()
p1_chart

p2_chart<-ggplot(data=program2, aes(x=Year, y=number, fill=race, colour = race)) +
  geom_bar(stat="identity", position=position_dodge())+
  scale_fill_manual(values = MyPalette) +
  scale_color_brewer(palette = "Dark2") +
  theme_bw()
p2_chart

enter image description here

enter image description here

Side note: You don't really want to be using the df$references in your ggplot calls, because you have already set your data = df.

Community
  • 1
  • 1
Nate
  • 10,361
  • 3
  • 33
  • 40