gpt4 book ai didi

反转默认比例梯度ggplot2

转载 作者:行者123 更新时间:2023-12-02 00:11:24 32 4
gpt4 key购买 nike

我是新手,我正在尝试设计热图。这是我的代码:

ggplot(gd, aes(Qcountry, Q6_1_Q6d), order = TRUE) +
geom_tile(aes(fill = prob), colour = "white") +
theme_minimal() +
labs( y = "Main reason for mobility", x = "Country") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.3)) +
scale_fill_gradient(name = "(%)")

这会产生一个完美的图表,我的问题是低水平是深蓝色,较高的值是浅蓝色,这不直观。最常见的方法是使用 rev()。但就我而言,我不知道该怎么做。那么,是否可以逆转这个默认比例This is the legend

另一个问题,有没有办法只用一种颜色创建比例渐变。我的意思是,scale_fill_gradient/scale_fill_gradientn 需要设置低颜色和高颜色(low = "", high = ""),我想将蓝色改为红色。

非常感谢您的支持。

最佳答案

?scale_colour_gradient 显示 low = "#132B43"high = "#56B1F7" 的默认值。

只需切换它们:

ggplot(faithfuld, aes(waiting, eruptions)) +
geom_raster(aes(fill = density)) +
scale_fill_continuous(high = "#132B43", low = "#56B1F7")

enter image description here

就我个人而言,我认为这不如默认值直观。

<小时/>

或者,您可以使用反向比例,但这也会翻转图例以从顶部开始:

ggplot(faithfuld, aes(waiting, eruptions)) +
geom_raster(aes(fill = density)) +
scale_fill_continuous(trans = 'reverse')

enter image description here

关于反转默认比例梯度ggplot2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43515112/

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