0

I'm working on making plots in R using ggplot2.

My plot looks like this:

values_range

ggplot(subset, aes(x, y, width = 0.5)) + 
geom_bar(aes(fill = factor(sort(values))), position = "stack", stat="identity")

where values_range is vector of all possible values and subset is subset of my data

How can I force ggplot to choose colors for fill always in the same way independently from values and subset

Eg. when values_range = [1, 2, 3, 4] and for subset1 values = [1, 2, 3] and for subset2 values = [1, 3, 4]

I want 1, 2, 3, 4 values have the same colour in both subset1 and subset2 plots. By default it is not in this way, ggplot would give the same colours for 2(1) - 3(2) and 3(1) - 4(2) values

Do you have any idea how can I achieve this?

Tomek
  • 311
  • 1
  • 2
  • 5
  • 3
    Are you attempting to do something like [this](http://stackoverflow.com/q/6919025/324364)? – joran May 31 '16 at 18:35

1 Answers1

0

convert your values to factor firstly

data$values <- factor(data$values)
ggplot(subset, aes(x, y, width = 0.5)) + 
   geom_bar(aes(fill = values), position = "stack", stat="identity")
myincas
  • 1,500
  • 10
  • 15