I have a factor which I created as follows:
myfactor<-c("A","B","C","D")
myfactor<-factor(myfactor)
I use that factor for multiple datasets for the value
column:
factor(datasetA$value,levels=myfactor)
factor(datasetB$value,levels=myfactor)
Dataset A
Type count variable value
1: 1 235 1 A
2: 2 31 1 A
3: 3 28 1 B
4: 4 113 1 B
5: 5 40 1 C
Dataset B
Type count variable value
1: 1 235 1 B
2: 2 31 1 B
3: 3 28 1 B
4: 4 113 1 B
5: 5 40 1 C
My Problem
When I plot dataset A with the command
ggplot(data=datasetA, aes(x=as.factor(variable),y=count,fill = value)) +
geom_bar(position = "stack", stat = "identity", width = 1) +
scale_fill_manual(values=c("#4ceb34","#ebd034","#eb34dc","#34ebc3","#3452eb","#eb3434"))
I get the following legend:
When I plot dataset B the legend looks like the following
I want to establish a binding between the levels and the colors: A should be green, B should be yellow... How can I do that?