如何根据单独的变量将geom_col的条形设置为颜色 [英] How to set the bars of a geom_col to a color according to an seperate variable

查看:101
本文介绍了如何根据单独的变量将geom_col的条形设置为颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

感谢您的帮助.

推荐答案

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

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'

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

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

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

There are no red bars, but all the p-value are lowish.

这篇关于如何根据单独的变量将geom_col的条形设置为颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆