R 基于ggplot2中x和y方向的两个不同变量指定不同的连续颜色渐变

R 基于ggplot2中x和y方向的两个不同变量指定不同的连续颜色渐变,r,ggplot2,R,Ggplot2,我是R(和堆栈溢出)新手,非常感谢您的帮助!我无法从问题中找到解决方案。我正试图: 基于不同的变量,在x和y方向上指定两个不同的连续颜色渐变。(下面代码中的图1) 为同一数据帧的不同变量的不同绘图保留这些颜色指定。(下面代码中的图2) 我只能根据如下所示的一个变量来管理分配颜色 例如: ####set up data#### #using ggplot2 package library(ggplot2) df <- data.frame(c(1, 1, 1, 1, 2, 2, 2, 2,

我是R(和堆栈溢出)新手,非常感谢您的帮助!我无法从问题中找到解决方案。我正试图:

  • 基于不同的变量,在x和y方向上指定两个不同的连续颜色渐变。(下面代码中的图1)
  • 为同一数据帧的不同变量的不同绘图保留这些颜色指定。(下面代码中的图2)
  • 我只能根据如下所示的一个变量来管理分配颜色

    例如:

    ####set up data####
    #using ggplot2 package
    library(ggplot2)
    
    df <- data.frame(c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4), 
                     c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4),
                     c(256, 4, 196, 16, 144, 36, 100, 64, 81, 49, 121, 25, 169, 9, 225, 1),
                     c(0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610))
    colnames(df) <- c("X", "Y", "V1", "V2")
    
    ####plot1: Simple plot of V1 vs V2#####
     #I would like the color to go towards blue as V1 increases on x axis (as it is now)
     #I would also like the color to go towards red as V2 increases on y axis
     #Ideally, when both x and y are large, it would be some purple color
    
    plot1 <- ggplot(df, aes(x=V1, y = V2, color= V1)) + geom_point()+ 
      scale_color_continuous(low="black", high="light blue")+ 
      labs(y= "V2: I want this to go from black --> red", x = "V1: black--> blue")
    plot1
    
    ####plot2: plot of x vs y but with color assignment same as plot1####
    #The position based on X and Y is correct but,
    #   I'd like to have the color assigned based on V1 and V2 (same as plot1)
    plot2 <- ggplot(df, aes(x=X, y=Y, color=V1)) + geom_point()+
      scale_color_continuous(low="black", high="light blue")
    plot2
    
    #####设置数据####
    #使用ggplot2包
    图书馆(GG2)
    
    df我试过修改scoa的,也许这会奏效。不过,您可能需要手动构建图例,因为这种方法会使默认图例失去吸引力

    根据以上数据:

    df <- data.frame(X = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4), 
                     Y = c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4),
                     V1 = c(256, 4, 196, 16, 144, 36, 100, 64, 81, 49, 121, 25, 169, 9, 225, 1),
                     V2 = c(0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610))
    
    现在,使用
    rgb()
    和缩放值创建命名颜色向量:

    col <- rgb(ycol, 0, xcol)
    names(col) <- as.character(df$V1)
    

    对于第二个绘图,保持颜色参数相同:

    ggplot(df, aes(x=X, y=Y, color=as.factor(V1))) +
      geom_point(size = 5) +
      scale_color_manual(values = col) +
      theme_classic() +
      theme(legend.position = "none") 
    

    谢谢,这很好用。对于将来引用这个的人,我能够将它应用到更大的数据集。出于某种原因,我不得不将指定颜色的列切换到我的V2等价物。。。可能是因为它的规模比我的数据集中的V1要大得多。
    ggplot(df, aes(x = V1, y = V2, color = as.factor(V1))) +
      geom_point(size = 5) +
      scale_color_manual(values = col) +
      theme_classic() +
      theme(legend.position = "none") 
    
    ggplot(df, aes(x=X, y=Y, color=as.factor(V1))) +
      geom_point(size = 5) +
      scale_color_manual(values = col) +
      theme_classic() +
      theme(legend.position = "none")