我正在努力尋找反轉圖例的正確解決方案,以便紅色位于底部,綠色位于頂部。這是一個簡單的例子。
library(ggplot2)
library(dplyr)
x = seq(0.01,1,0.01)
y = seq(0.01,1,0.01)
df <- expand.grid(x = x, y = y)
df <- df %>% mutate(z = x*y/(1 x))
ggplot(df, aes(x = x, y = y, z = z))
geom_contour_filled(bins = 10)
geom_contour(bins = 20, colour = "grey")
scale_fill_manual(values = rainbow(20))
uj5u.com熱心網友回復:
幾個問題:
- 您使用 20 種顏色來描述 10 個垃圾箱。
- 您將整個彩虹用于紅綠色漸變。
建議的解決方法是使用函式的endandrev引數rainbow()。
library(ggplot2)
library(dplyr)
x = seq(0.01,1,0.01)
y = seq(0.01,1,0.01)
df <- expand.grid(x = x, y = y)
df <- df %>% mutate(z = x*y/(1 x))
ggplot(df, aes(x = x, y = y, z = z))
geom_contour_filled(bins = 10)
geom_contour(bins = 20, colour = "grey")
scale_fill_manual(values = rainbow(10, end = 0.4, rev = TRUE))

由reprex 包于 2022-05-15 創建(v2.0.1)
除此之外,您可能需要考慮采用比彩虹具有更好視覺特性的調色板。例如,你真的能在視覺上區分第 2 到第 4 個綠色垃圾箱嗎?具有更好(但不完美)屬性的緊密調色板是viridisLite::turbo(10, begin = 0.5).
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/476661.html
上一篇:scale_linetype_manual不起作用或顯示在R中的圖例上,為什么?
下一篇:我可以根據顏色選擇填充嗎?
