如何根据单独的变量将geom_col的条形设置为颜色

时间:2019-01-08 19:40:23

标签: r ggplot2

我有一个数据集,为此我做了一个多面的geom_col。该代码与dput()一起附加。数据是不同表型的遗传力的排列以及每个排列的p值。

到目前为止,我有一个图形可以准确地绘制数据,但是我希望每个条形的颜色都能反映p值。理想情况下,绿色将<0.1,黄色将<.2,红色将> 0.2。我尝试过scale_fill_manual(),但不知道如何在该函数中使用条件。

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))

感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

使用Dplyr,您可以创建组,然后使用scale_fill_manual指定颜色:

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"))

答案 1 :(得分:1)

您可以分两步执行此操作。首先,创建一列(我称其为颜色)以存储每个条形所需的颜色。

res2$color <- NA
res2$color[res2$pvalue >= .2] <- 'red'
res2$color[res2$pvalue < .2] <- 'yellow'
res2$color[res2$pvalue < .1] <- 'green'

接下来,告诉ggplot使用该列作为颜色,并使用同一性刻度进行填充

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

没有红色条形,但是所有p值都很低。

Bars colored by p-value.