What i want to do is loop over a dataset with different samples and make overlay density plots with ggplot2. 1 reference sample is plotted in every figure together with one of the other samples. The plotting itself is not a problem but the colors are:
dataset:
df <- data.frame(SampleName = c("a","a","a","b","b","b","c","c","c"),
Data = c(1,1,2,4,6,7,3,4,9))
With scale_fill_manual i can assign a color to a specific sample:
#1
ggplot() +
geom_density(data=subset(df, SampleName == "a"),
aes(x = Data, group=SampleName, fill=SampleName), alpha= 0.6) +
geom_density(data=subset(df, SampleName == "b"),
aes(x = Data, group=SampleName, fill=SampleName), alpha= 0.6) +
scale_fill_manual(values = c("b" = "red", "a" = "green"))
With a vector containing all sample names i can make a loop which plots all overlay plots with "b" as the fixed sample:
#2
Samples <- c("a","b","c")
for(i in 1:length(Samples)){
print(ggplot() +
geom_density(data=subset(df, SampleName == Samples[i]),
aes(x = Data, group=SampleName, fill=SampleName), alpha= 0.6) +
geom_density(data=subset(df, SampleName == "b"),
aes(x = Data, group=SampleName, fill=SampleName), alpha= 0.6) +
scale_fill_manual(values = c("red", "green"))
)
}
The problem in #2 is the samples change colors when b is fixed -> in the first plot b is green and a is red, in plot 3 b is red and c is green. When i try to assign the colors like in #1 to a specific sample (see #3) this error message is displayed:
"Error: unexpected '=' in: " aes(x = Data, group=SampleName, fill=SampleName), alpha= 0.6)) + scale_fill_manual(values = c("b" = "red", Samples[i] ="
I tried different things to make the variable in 'Samples[i] = "green"' more 'text-like' with as.character, paste(),... but this doesn't seem to work. Any solutions?
#3
for(i in 1:length(Samples)){
print(ggplot() +
geom_density(data=subset(df, SampleName == Samples[i]),
aes(x = Data, group=SampleName, fill=SampleName), alpha= 0.6) +
geom_density(data=subset(df, SampleName == "b"),
aes(x = Data, group=SampleName, fill=SampleName), alpha= 0.6)) +
scale_fill_manual(values = c("b" = "red", Samples[i] = "green"))
}
You can create the named vector easily with setNames
, in which you can use expressions that need evaluation. Example:
setNames(c('red', 'green'), c('b', Samples[i]))
Which makes:
for(i in 1:length(Samples)){
print(ggplot() +
geom_density(data=subset(df, SampleName == Samples[i]),
aes(x = Data, group=SampleName, fill=SampleName), alpha= 0.6) +
geom_density(data=subset(df, SampleName == "b"),
aes(x = Data, group=SampleName, fill=SampleName), alpha= 0.6)) +
scale_fill_manual(values = setNames(c('red', 'green'), c('b', Samples[i])))
}