I have a data frame (df) that looks like the following:
| gene | p_value | p_value_dif | p-value category |
|---|---|---|---|
| a | 0.06 | 0.01 | non-sig |
| c | 0.07 | 0.02 | non-sig |
| d | 0.008 | - 0.03 | sig |
| e | 0.009 | - 0.04 | sig |
I have created a diverging bar graph with the following code:
ggplot(df, aes(x=gene,
y=p_value_dif ,
label=p_value_dif ))
geom_bar(stat='identity',
aes(fill= (as.factor(p_value_dif)),
width=0.9)
scale_fill_manual("legend",
values = c("Significant" = "black", "Insignificant" = "orange"))
coord_flip()
The issue is that only my legend changes colors to black and orange. The bars remain grey. What can I do so that the colors in my legend match the colors of my bars?
Note: if "fill" is not set to "as.factor" I get the following: Error: Continuous value supplied to discrete scale
CodePudding user response:
The issue is that your column p_value_dif does not contain any values "Significant" or "Insignificant". Only these values will be filled "black" or "orange". All other values will be filled with the default na.value of the scale. Instead you could map your column p-value.category on fill and set your fill colors and labels like so:
library(ggplot2)
ggplot(df, aes(x=gene,
y=p_value_dif ,
label=p_value_dif ))
geom_bar(stat='identity',
aes(fill= `p-value.category`),
width=0.9)
scale_fill_manual("legend",
values = c("sig" = "black", "non-sig" = "orange"),
labels = c("sig" = "Significant", "non-sig" = "Insignificant"))
coord_flip()

DATA
df <- data.frame(
stringsAsFactors = FALSE,
check.names = FALSE,
gene = c("a", "c", "d", "e"),
p_value = c(0.06, 0.07, 0.008, 0.009),
p_value_dif = c("0.01", "0.02", "- 0.03", "- 0.04"),
`p-value.category` = c("non-sig", "non-sig", "sig", "sig")
)
