Consider the following data frame:
x = read.table(text = 'Lo Re Pe
1 T 33
1 F 22
1 H 11
2 T 22
2 F 22', header = TRUE)
and the following plot:
qplot(factor(Lo), data=x, geom='bar', fill=Re, weight=Pe,
xlab='L', main='Title', ylab='Pe')
Now consider this data frame:
x <- read.table(text = 'Lo Re Pe
1 D 33
1 K 22
2 D 22
2 K 22', header=TRUE)
with the same qplot
statement.
The colors assigned to each Re
value are not consistent between the plots, so it is difficult to compare the plots directly.
How do I specify that Re
value T
should always be "Red", for example, and that Re
value F
should always be "Blue", for example, so that the qplot
command always uses consistent colors for each Re
value, regardless of the contents of the data frame? There are a finite and known number of values for Re
, so I could specify them all.
I tried the following when the data frame contained values T
, F
and H
:
qplot(factor(Lo), data=x, geom='bar', fill=Re, weight=Pe,
xlab='Loci', main='Title', ylab='Pe',
scale_fill_manual(values=c("Blue","Red","Green"),labels=c("T","F","H")))
but R reports an error about incorrect length and does not produce a plot.
The solution would ideally allow me to specify colors for all possible values of Re
, even though all of these may not be present in the data frame.