Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用比例\颜色\布鲁尔调色板的极值_R_Ggplot2 - Fatal编程技术网

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()