gpt4 book ai didi

r - ggplot2 带有渐变颜色填充的水平条形图

转载 作者:行者123 更新时间:2023-12-03 20:30:27 24 4
gpt4 key购买 nike

我正在尝试创建水平条形图,并希望根据它们的对数折叠值填充单个条形,白色表示最低值,最暗表示最高值。但是,我无法做到。
这是我的工作,有人可以帮我解决吗?

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

enter image description here

先感谢您!

最佳答案

以下是您需要考虑的事项:

  • 对数据框值进行排序对 ggplot 没有影响
  • 在您的代码中,您将填充颜色映射到 Tissue,而不是 LogFold
  • 较新的 geom_colgeom_bar(stat = ...) 打字少
  • 当您指定 Tissue
  • 的所有值时,对您的规模的限制是不必要的
  • 如果要填充渐变,请使用 scale_fill_gradient2()
  • fill = white 在 theme_classic 的白色背景上将不可见

  • 所以你可以尝试这样的事情:
    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")

    enter image description here

    但我不知道颜色渐变在解释信息方面真的增加了多少。所以这是没有它的结果,你是法官:
    df1 %>% 
    ggplot(aes(reorder(Tissue, LogFold), LogFold)) +
    geom_col() +
    coord_flip() +
    labs(x = "Tissue") +
    theme_classic()

    enter image description here

    关于r - ggplot2 带有渐变颜色填充的水平条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49396955/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com