我正試圖在ggplot中為變數z的連續色階添加一個灰度色階。下面是一個樣本,但我還沒能弄清楚如何制作一個連續的灰度色階。
library(ggplot2)
x < - seq(0。 10,1)
y < - seq(11。 20,1)
z < - seq(21。 30,1)
df <- data. frame(cbind(x, y,z))?
ggplot(df,aes(x, y))
geom_point(aes(color = z)/span>。 尺寸= 3)
scale_color_continuous(type = "viridis">)
uj5u.com熱心網友回復:
一種輕松地將顏色改變為灰度的方法是使用scale_color_distiller。只要在你的腳本中把scale_color_continuous替換成下面這一行就可以了。
scale_color_distiller(type = "seq"/span>,
方向 = -1,
palette = "Greys")
uj5u.com熱心網友回復:
# First you set the border of the gray colors
gray_range < - colorRampPalette(c("#737373"/span>。 "#F0F0F0"))
x < - seq(0。 10,1)
y < - seq(11。 20,1)
z < - seq(21。 30,1)
# 然后你設定灰色的范圍
color_z < - grey_range(length(z))
df <- data. frame(cbind(x, y,z))
# 最后,你將顏色分配給因子Z。
ggplot(df,aes(x, y, color=as. factor(z))
geom_point(size = 3)
scale_color_manual(values=setNames(color_z, levels(df$z)))
theme_bw()
非常簡單的方法是這樣做的
ggplot(df, aes(x,y, color=as. factor(z))
geom_point(size = 3)
scale_colour_grey()
# 使用 start = 0.2, end = 0.8 選項,你可以調節灰色的強度和方向。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/330215.html
標籤:



