R 使用比例\颜色\布鲁尔调色板的极值
我想用R 使用比例\颜色\布鲁尔调色板的极值,r,ggplot2,R,Ggplot2,我想用scale\u color\u brewer(palete=“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(colo
scale\u color\u brewer(palete=“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()
使用
缩放颜色\u手册
从调色板中选择所需的值。执行此操作时,必须确保加载Rcolorbrewer
,因为使用scale\u color\u brewer
参数时,ggplot2
仅调用Rcolorbrewer
<代码>颜色调色板从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使用scale\u color\u manual
从调色板中选择所需的值。执行此操作时,必须确保加载Rcolorbrewer
,因为使用scale\u color\u brewer
参数时,ggplot2
仅调用Rcolorbrewer
<代码>颜色调色板
从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您可以通过设置少于限制的中断来破解它:
库(ggplot2)
种子(47)
df1您可以通过设置少于限制的中断来破解它:
库(ggplot2)
种子(47)
df1此解决方案不适用于n我没有注意到这一点,就我而言是草率的colorRampPalette
将有所帮助,使用bias
调整从调色板获取颜色的位置。最后,我修改了您的答案,使用seq
。默认情况下,seq
将使用极值并填充中间值<代码>缩放颜色手册(值=brewer.pal(11,“RdYlGn”)[四舍五入(seq(from=1,to=11,length.out=n)))
。此解决方案不适用于n遗漏了这一点,我的部分太马虎了colorRampPalette
将有所帮助,使用bias
调整从调色板获取颜色的位置。最后,我修改了您的答案,使用seq
。默认情况下,seq
将使用极值并填充中间值<代码>缩放颜色手册(值=brewer.pal(11,“RdYlGn”)[四舍五入(seq(from=1,to=11,length.out=n)))
。
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()