作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想用 scale_colour_brewer(palette = "RdYlGn").
绘制可变数量的列如果我有五列,我得到的最高和最低因素的颜色是深绿色和深红色。
n <- 5 #number of variables
x <- 1:10
y <- runif(10*n)
cond <- rep(1:n, each = 10)
df1 <- data.frame(x,y)
ggplot(df1, aes(x=x, y=y)) +
geom_line(aes(colour=factor(cond), group=factor(cond)),size=2) +
scale_colour_brewer(palette = "RdYlGn") + theme_bw()
但是,如果我只有两列 ( n <- 2
),我会得到范围颜色的中间值。无论我的数据中有多少列,如何获得最高和最低因子的深红色和深绿色?
n <- 2 #number of variables
x <- 1:10
y <- runif(10*n)
cond <- rep(1:n, each = 10)
df1 <- data.frame(x,y)
ggplot(df1, aes(x=x, y=y)) +
geom_line(aes(colour=factor(cond), group=factor(cond)),size=2) +
scale_colour_brewer(palette = "RdYlGn") + theme_bw()
编辑最后,我修改了@paqmo 的答案并使用了 seq
.默认情况下,seq
选择极值并填充中间值。
n <- 2#number of variables
x <- 1:10
y <- runif(10*n)
cond <- rep(1:n, each = 10)
df1 <- data.frame(x,y)
ggplot(df1, aes(x=x, y=y)) +
geom_line(aes(colour=factor(cond), group=factor(cond)),size=2) +
scale_colour_manual(values = brewer.pal(11,"RdYlGn")[round(seq(from = 1, to = 11,length.out = n))] ) +
theme_bw()
最佳答案
改用 scale_colour_manual
从您想要的调色板中选择哪些值。您必须确保在执行此操作时加载 Rcolorbrewer
,因为 ggplot2
仅在您使用 scale_colour_brewer
时调用 Rcolorbrewer
> 争论。 colorRampPalette
从 brewer 调色板中获取颜色。
set.seed(1034)
n <- 2 #number of variables
x <- 1:10
y <- runif(10*n)
cond <- rep(1:n, each = 10)
df1 <- data.frame(x,y)
ggplot(df1, aes(x=x, y=y)) +
geom_line(aes(colour=factor(cond), group=factor(cond)),size=2) +
scale_colour_manual(values = colorRampPalette(brewer.pal(11,"RdYlGn"), bias = 2)(n)) +
theme_bw()
set.seed(1034)
n <- 3 #number of variables
x <- 1:10
y <- runif(10*n)
cond <- rep(1:n, each = 10)
df1 <- data.frame(x,y)
ggplot(df1, aes(x=x, y=y)) +
geom_line(aes(colour=factor(cond), group=factor(cond)),size=2) +
scale_colour_manual(values = colorRampPalette(brewer.pal(11,"RdYlGn"), bias = 2)(n)) +
theme_bw()
set.seed(1034)
n <- 4 #number of variables
x <- 1:10
y <- runif(10*n)
cond <- rep(1:n, each = 10)
df1 <- data.frame(x,y)
ggplot(df1, aes(x=x, y=y)) +
geom_line(aes(colour=factor(cond), group=factor(cond)),size=2) +
scale_colour_manual(values = colorRampPalette(brewer.pal(11,"RdYlGn"), bias = 2)(n)) +
theme_bw()
关于r - 使用 scale_colour_brewer 调色板的极值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42492009/
我是一名优秀的程序员,十分优秀!