【问题标题】:How to set the bars of a geom_col to a color according to an seperate variable如何根据单独的变量将 geom_col 的条形设置为颜色
【发布时间】:2019-01-08 19:40:22
【问题描述】:

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

到目前为止,我有一个可以准确绘制数据的图表,但我想让每个条形的颜色反映 p 值。理想情况下,绿色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))

感谢您的帮助。

【问题讨论】:

  • 你说的

标签: r ggplot2


【解决方案1】:

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

【讨论】:

  • 并在scale_fill_manual 中使用labels 来获得一个不错的图例。
  • 你也可以在开始时更改我很棒的标签:)。
  • 我使用了 0.01 和 0.02,因为这可能是您在谈论统计显着性时的意思
【解决方案2】:

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

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 值都很低。

【讨论】:

  • 不客气。这个社区是学习新技巧的好地方。
猜你喜欢
  • 1970-01-01
  • 2015-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
  • 2019-09-06
相关资源
最近更新 更多