简体   繁体   English

反转默认比例渐变ggplot2

[英]Reversing default scale gradient ggplot2

I am newbie, I am trying to desing a heat map.我是新手,我正在尝试设计热图。 This is my code:这是我的代码:

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 = "(%)")

Which produces a perfect chart, my problem is low levels are dark blue, and higher values are light blue, which is not intuitive.这产生了一个完美的图表,我的问题是低级别是深蓝色,高值是浅蓝色,这不直观。 Most common way to do is use rev() .最常见的方法是使用rev() But in my case I dont know how to.但就我而言,我不知道该怎么做。 So, is it possible to reverse this default scale ?那么,是否可以反转这个默认比例 This is the legend这就是传说

Other question, is there a way to create a scale gradient only with one colour.另一个问题,有没有办法只用一种颜色创建缩放渐变。 I mean, scale_fill_gradient/scale_fill_gradientn need to set a low color and high color (low = "", high = "") and I want to change the blue by red.我的意思是, scale_fill_gradient/scale_fill_gradientn需要设置低色和高色(low = "", high = "") ,我想将蓝色改为红色。

Thanks so much for your support.十分感谢您的支持。

?scale_colour_gradient shows the default values of low = "#132B43" and high = "#56B1F7" . ?scale_colour_gradient显示low = "#132B43"high = "#56B1F7"的默认值。

Simply switch those around:只需切换那些:

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

在此处输入图片说明

Personally, I think this is less intuitive than the default.就个人而言,我认为这不如默认设置直观。


Alternatively, you can use a reverse scale, but this will also flip the legend to start at the top:或者,您可以使用反向比例,但这也会翻转图例以从顶部开始:

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

在此处输入图片说明

you can use the color palettes from RColorBrewer (link) library and assign the direction of the color gradient您可以使用RColorBrewer (链接)库中的调色板并指定颜色渐变的方向

library(RColorBrewer)
library(ggplot2)

ggplot(df, aes(x, y)) +
  geom_tile(aes(fill = z, width = w), colour = "grey50") +
  scale_fill_distiller(palette ="RdBu", direction = -1) # or direction=1


# data
  df <- data.frame( x = rep(c(2, 5, 7, 9, 12), 2),
                    y = rep(c(1, 2), each = 5),
                    z = rep(1:5, each = 2),
                    w = rep(diff(c(0, 4, 6, 8, 10, 14)), 2))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM