Reputation: 155
I have a data set for which I have done a facetted geom_col. The code is attached along with the dput(). The data is the permutations of heritability of different phenotypes along with p-values for each permutations.
So far I have a graph that accurately plots the data but I want to have the color of each bar reflect the p-value. Ideally, Green would be <0.1, Yellow would be <.2, and red would be >0.2. I tried scale_fill_manual() but I have no idea how to use a condition with that function.
A <- ggplot(res2, aes(Phenotype, heritability))
#uses a bar chart, geom_col represents hereditity values as the hights of the bars.
A + geom_col(position = 'stack', fill = "#0000ff") +
# Facets the data according to the Phenotypes in the X column of the data
facet_wrap(.~ X,scales='free_x') +
# Theme info: tilts the x-axis labels 90 degrees and pushes labels to be centered below the bars
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = .4), plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5))+
labs(title ="Heritability of Phenotype Permutations", subtitle = "P-values indicated")+
# adds the pvalues above the bars, sets their position to be above or below the bar.
geom_text(aes(y = heritability + .06 * sign(heritability), label = pvalue), position = position_dodge(width = 0.9), size = 3.3)
dput(res2)
structure(list(X = structure(c(8L, 1L, 7L, 9L, 6L, 4L, 5L, 3L,
2L, 1L, 7L, 9L, 6L, 4L, 5L, 3L, 2L, 8L, 7L, 9L, 6L, 4L, 5L, 3L,
2L, 8L, 1L, 9L, 6L, 4L, 5L, 3L, 2L, 8L, 1L, 7L, 6L, 4L, 5L, 3L,
2L, 8L, 1L, 7L, 9L, 4L, 5L, 3L, 2L, 8L, 1L, 7L, 9L, 6L, 5L, 3L,
2L, 8L, 1L, 7L, 9L, 6L, 4L, 3L, 2L, 8L, 1L, 7L, 9L, 6L, 4L, 5L
), .Label = c("Blue", "Green", "Magenta", "Maroon", "Orange",
"Pink", "Purple", "Red", "Yellow"), class = "factor"), Phenotype = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Blue", "Green", "Magenta",
"Maroon", "Orange", "Pink", "Purple", "Red", "Yellow"), class = "factor"),
heritability = c(0.12, 0.14, 0.34, 0.21, 0.33, 0.35, 0.25,
0.49, 0.19, 0.42, -0.12, 0.4, 0.13, 0.42, 0.47, 0.2, 0.17,
0.14, -0.1, 0.14, 0.45, 0.24, 0.47, -0.28, 0.34, 0.18, 0.15,
0.37, -0.47, 0.12, 0.17, -0.11, 0.53, 0.41, -0.2, 0.14, 0.26,
0.45, 0.41, 0.48, 0.15, -0.35, 0.22, 0.32, 0.29, 0.47, 0.17,
-0.25, 0.27, 0.38, 0.52, -0.11, 0.5, 0.28, 0.34, 0.31, 0.52,
0.14, -0.23, 0.21, 0.11, -0.42, 0.39, 0.32, 0.51, 0.39, 0.15,
0.46, 0.5, 0.42, 0.46, 0.18), pvalue = c(0.05, 0.09, 0.05,
0.05, 0.09, 0.02, 0.01, 0.1, 0.05, 0.04, 0.08, 0.01, 0.08,
0.05, 0.07, 0.06, 0.01, 0.04, 0.04, 0.01, 0.06, 0.1, 0.07,
0.01, 0.05, 0.02, 0.08, 0.1, 0.03, 0.06, 0.02, 0.08, 0.09,
0.01, 0.06, 0.04, 0.07, 0.03, 0.03, 0.07, 0.01, 0.01, 0.06,
0.05, 0.04, 0.06, 0.04, 0.03, 0.04, 0.04, 0.09, 0.1, 0.07,
0.01, 0.08, 0.06, 0.01, 0.07, 0.06, 0.08, 0.09, 0.1, 0.09,
0.01, 0.07, 0.05, 0.07, 0.06, 0.1, 0.1, 0.08, 0.09)), class = "data.frame", row.names = c(NA,
-72L))
Thanks for any help.
Upvotes: 1
Views: 2092
Reputation: 598
With Dplyr you can create groups and then use scale_fill_manual to specify colors:
library(dplyr)
res2 <- res2 %>%
mutate(pGroup = case_when(
pvalue < 0.02 ~ "meh",
pvalue < 0.01 ~ "sig",
pvalue >= 0.02 ~ "bleh"
))
A <- ggplot(res2, aes(Phenotype, heritability, fill = pGroup))
#uses a bar chart, geom_col represents hereditity values as the hights of the bars.
A + geom_col(position = 'stack') +
# Facets the data according to the Phenotypes in the X column of the data
facet_wrap(.~ X,scales='free_x') +
# Theme info: tilts the x-axis labels 90 degrees and pushes labels to be centered below the bars
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = .4), plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5))+
labs(title ="Heritability of Phenotype Permutations", subtitle = "P-values indicated")+
# adds the pvalues above the bars, sets their position to be above or below the bar.
geom_text(aes(y = heritability + .06 * sign(heritability), label = pvalue), position = position_dodge(width = 0.9), size = 3.3) +
scale_fill_manual(values = c("#00ff00", "#ffff00", "#ff0000"))
Upvotes: 3
Reputation: 1094
You can do this in two steps. First, create a column (I'm calling it color) to store the colors you want for each bar.
res2$color <- NA
res2$color[res2$pvalue >= .2] <- 'red'
res2$color[res2$pvalue < .2] <- 'yellow'
res2$color[res2$pvalue < .1] <- 'green'
Next, tell ggplot to use that column for colors, and to use an identity scale for the fill
A <- ggplot(res2, aes(Phenotype, heritability))
#uses a bar chart, geom_col represents hereditity values as the hights of the bars.
A + geom_col(position = 'stack', mapping = aes(fill = color)) + # fill is wrapped in aes and passed to mapping
# Facets the data according to the Phenotypes in the X column of the data
facet_wrap(.~ X,scales='free_x') +
# Theme info: tilts the x-axis labels 90 degrees and pushes labels to be centered below the bars
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = .4), plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5))+
labs(title ="Heritability of Phenotype Permutations", subtitle = "P-values indicated")+
# adds the pvalues above the bars, sets their position to be above or below the bar.
geom_text(aes(y = heritability + .06 * sign(heritability), label = pvalue), position = position_dodge(width = 0.9), size = 3.3) + scale_fill_identity() # identity scale
There are no red bars, but all the p-value are lowish.
Upvotes: 1