Reputation: 15
Here is some dummy data:
dummy <- c(rep("castor", 20), rep("dandelion", 20), rep("goose", 20), rep("teapot", 20), rep("bee", 20), rep("fan" , 20))
groups <- c(rep(c(rep(1, 10), rep(2, 10)), 6))
mydata <- data.frame(a = rnorm(120, 0, 1), b = rnorm(120, 0, 1), dummy = dummy, groups = groups)
Here is the function I wrote to plot a level of my choosing from the dummy column in blue. I want the remaining levels to be plotted in gray. Those remaining factor levels (let's call them reference) are hardcoded now which is not a problem because in my real data I have more levels of my choice (let's call them test) to plot, which is why I am writing a function for this.
custom_plot <- function(level){
df <- subset(mydata, mydata$dummy == level | mydata$groups == "1")
# Check that the test level is indeed in the subset df
return(table(df$dummy, df$groups))
p <- ggplot(df, aes(x = a, y = b, color = dummy, shape = dummy)) + geom_point() + theme_bw()
p <- p + scale_color_manual(values = c(level = "blue", "castor" = "gray", "dandelion" = "gray", "fan" = "gray", "goose" = "gray", "teapot" = "gray"))
p
}
custom_plot("bee")
I can't post an image because I need at least 10 reputation, but essentially the graph I get has the test level in the legend, but the actual datapoints are missing from the graph, and the shape/color of that level is blank. I tried using the breaks, limits, and labels arguments but nothing worked.
Thank you for your help!
Upvotes: 0
Views: 436
Reputation: 19960
I believe this should provide what you are looking for. It is typically much easier if you just pass the list of colors to ggplot
. I took the liberty of showing you a way to index the colors so that the dummy
level you choose will be the one that is colored blue.
library(ggplot2)
custom_plot <- function(level){
df <- subset(mydata, mydata$dummy == level | mydata$groups == "1")
# create colors vector
idx <- which(levels(mydata$dummy) == level)
nlev <- nlevels(mydata$dummy)
colors <- c(rep("gray", idx-1), "blue", rep("gray", nlev-idx))
p <- ggplot(df, aes(x = a, y = b, color = dummy, shape = dummy)) +
geom_point() +
theme_bw()
p <- p + scale_color_manual(values = colors)
p
}
custom_plot("bee")
Upvotes: 1