本文介绍了带有渐变颜色填充的ggplot2水平条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建水平条形图,并希望根据其对数折叠值填充单个条形,白色为最低值,最暗为最高值.但是,我无法做到.这是我的工作,有人可以帮我解决吗?
I am trying to create horizontal bar plot and would like to fill the individual bar as per their log fold values, white for lowest value and darkest for highest value. However, I am not able to do it.Here is my work, can somebody help me fix it?
df <- data.frame(LogFold = c(14.20, 14.00, 8.13, 5.3, 3.8, 4.9, 1.3, 13.3, 14.7, 12.2),
Tissue = c("liver", "adrenal", "kidney", "heart", "limb", "adipose", "brown", "hypothalamus", "arcuate", "lung"))
df1<-df%>%
arrange(desc(LogFold))
ggplot(data=df1, aes(x=Tissue, y=LogFold, fill = Tissue)) +
geom_bar(stat="identity")+
scale_colour_gradient2()+
coord_flip()+
ylim(0, 15)+
scale_x_discrete(limits = df1$Tissue)+
theme_classic()
提前谢谢!
推荐答案
这是您需要考虑的问题:
Here's what you need to think about:
- 对数据帧值进行排序与ggplot没有区别
- 在您的代码中,您正在将填充颜色映射到组织,而不是LogFold
- 较新的
geom_col
输入的内容少于geom_bar(stat = ...)
- 由于您为组织指定了所有值,因此没有必要限制规模
- 如果要填充渐变色,请使用
scale_fill_gradient2()
- fill =白色在
theme_classic
的白色背景上不可见
- sorting the data frame values makes no difference to ggplot
- in your code you are mapping fill color to Tissue, not to LogFold
- the newer
geom_col
is less typing thangeom_bar(stat = ...)
- the limits to your scale are unnecessary as you specify all values for Tissue
- if you want to fill with a gradient use
scale_fill_gradient2()
- fill = white will not be visible on the white background of
theme_classic
所以您可以尝试这样的事情:
So you could try something like this:
library(tidyverse)
df1 %>%
ggplot(aes(reorder(Tissue, LogFold), LogFold)) +
geom_col(aes(fill = LogFold)) +
scale_fill_gradient2(low = "white",
high = "blue",
midpoint = median(df1$LogFold)) +
coord_flip() +
labs(x = "Tissue")
但是我不知道颜色渐变在解释信息方面确实增加了很多东西.因此,如果没有结果,这就是您的判断力:
But I don't know that the color gradient really adds anything much in terms of interpreting the information. So here's the result without it, you be the judge:
df1 %>%
ggplot(aes(reorder(Tissue, LogFold), LogFold)) +
geom_col() +
coord_flip() +
labs(x = "Tissue") +
theme_classic()
这篇关于带有渐变颜色填充的ggplot2水平条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!