簡體   English   中英

反轉默認比例漸變ggplot2

[英]Reversing default scale gradient ggplot2

我是新手,我正在嘗試設計熱圖。 這是我的代碼:

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() 但就我而言,我不知道該怎么做。 那么,是否可以反轉這個默認比例 這就是傳說

另一個問題,有沒有辦法只用一種顏色創建縮放漸變。 我的意思是, 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")

在此處輸入圖片說明

就個人而言,我認為這不如默認設置直觀。


或者,您可以使用反向比例,但這也會翻轉圖例以從頂部開始:

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

在此處輸入圖片說明

您可以使用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