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

R 如何根据单独的变量将几何图形的条形设置为颜色,r,ggplot2,R,Ggplot2,我有一个数据集,我为它做了分面几何运算。代码随dput一起附上。数据是不同表型的遗传力排列以及每个排列的p值 到目前为止,我有一个精确绘制数据的图表,但我想让每个条的颜色反映p值。理想情况下,绿色应该是您可以通过两个步骤完成此操作。首先,创建一个我称之为“颜色”的列来存储每个条所需的颜色 res2$color <- NA res2$color[res2$pvalue >= .2] <- 'red' res2$color[res2$pvalue < .2] <- 'y

我有一个数据集,我为它做了分面几何运算。代码随dput一起附上。数据是不同表型的遗传力排列以及每个排列的p值


到目前为止,我有一个精确绘制数据的图表,但我想让每个条的颜色反映p值。理想情况下,绿色应该是您可以通过两个步骤完成此操作。首先,创建一个我称之为“颜色”的列来存储每个条所需的颜色

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值都较低


您可以通过两个步骤完成此操作。首先,创建一个我称之为“颜色”的列来存储每个条所需的颜色

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值都较低


使用Dplyr,您可以创建组,然后使用缩放填充手册指定颜色:

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

使用Dplyr,您可以创建组,然后使用缩放填充手册指定颜色:

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

这就是你的意思吗?这就是你所说的在“缩放填充”手册中使用标签来获得一个好的图例。你也可以在开始时更改我的好标签:。我使用了0.01和0.02,因为这可能是你在谈论统计意义时的意思,并在“缩放填充”手册中使用标签来获得一个好的图例。你可以也可以在开始时更改我的很棒的标签:。我使用了0.01和0.02,因为这些很可能就是你在谈论统计意义时的意思。谢谢你,这太完美了!不客气。这个社区是学习新技巧的好地方。谢谢你,这太完美了!不客气。这个社区是学习新技巧的好地方。