【问题标题】:How to set fixed continuous colour values in ggplot2如何在ggplot2中设置固定的连续颜色值
【发布时间】:2014-02-03 21:07:12
【问题描述】:

我正在绘制很多图形,我希望它们都具有相同的色标,以便我可以相互比较。这是我的代码:

myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")))
print(ggplot(mydata, aes(x= X, y= Y, colour= Z)) + geom_point(alpha=.5,size = 6) + scale_colour_gradientn(colours = myPalette(100)) + ylim(.1,.4) + xlim(1.5,2) + ggtitle(title))

有没有办法设置这个色阶?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我理解正确吗?您有两个绘图,其中色标的值被映射到不同绘图上的不同颜色,因为这些绘图中没有相同的值。

    library("ggplot2")
    library("RColorBrewer")
    ggplot(subset(mtcars, am==0), aes(x=wt, y=mpg, colour=carb)) + 
      geom_point(size=6)
    

    ggplot(subset(mtcars, am==1), aes(x=wt, y=mpg, colour=carb)) + 
      geom_point(size=6)
    

    在顶部,深蓝色为 1,浅蓝色为 4,而在底部,深蓝色(仍然)为 1,但浅蓝色现在为 8。

    您可以通过将limits 参数传递给刻度来修复颜色条的末端;它应该涵盖数据在任何图中可以采用的整个范围。此外,您可以将此比例分配给一个变量并将其添加到所有绘图中(以减少冗余代码,以便定义仅在一个地方而不是在每个绘图中)。

    myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")))
    sc <- scale_colour_gradientn(colours = myPalette(100), limits=c(1, 8))
    
    ggplot(subset(mtcars, am==0), aes(x=wt, y=mpg, colour=carb)) + 
      geom_point(size=6) + sc
    

    ggplot(subset(mtcars, am==1), aes(x=wt, y=mpg, colour=carb)) + 
      geom_point(size=6) + sc
    

    【讨论】:

      【解决方案2】:

      可能有更好的方法来做到这一点,但我不知道。同时,您需要确保scale_colour_gradientnvalues 参数使得所有绘图的值都映射到正确的颜色。所以在这里,我用相同的映射在 0-100 之间绘制了两个图,但其中一个的值在 50-150 之间:

      mydata <- data.frame(X=runif(20), Y=runif(20), Z=runif(20, 0, 100))
      p1 <- ggplot(mydata, aes(x=X, y=Y, colour=Z)) + 
        geom_point(alpha=.5, size = 6) + 
        scale_colour_gradientn(colours = myPalette(100), values=seq(0, 100, length.out=100)/100) + 
        ggtitle("Z: 0 - 100")
      

      这是关键位:

      mydata2 <- data.frame(X=runif(20), Y=runif(20), Z=runif(20, 50, 150))
      nrm.range.2 <- (range(mydata$Z) - min(mydata2$Z)) / diff(range(mydata2$Z))
      nrm.vals <- seq(nrm.range.2[[1]], nrm.range.2[[2]], length.out=100)
      

      现在制作第二个情节。

      p2 <- ggplot(mydata2, aes(x=X, y=Y, colour=Z)) + 
        geom_point(alpha=.5, size = 6) + 
        scale_colour_gradientn(colours = myPalette(100), values=nrm.vals) + 
        ggtitle("Z: 50 - 150")
      

      我不知道强制在刻度上显示哪个值范围,但如果您有多个具有不重叠 Z 值范围的图,您可以创建第三个具有所有范围的虚拟图和用那个。在这里,我故意超出范围以显示重叠的值具有相同的颜色。

      【讨论】:

        【解决方案3】:

        好的,从上一个示例中获取数据集:

        library(ggplot2)
        library(RColorBrewer)
        library(gridExtra)
        library(gtablegridExtra)
        
        #Using the mtcars data set
        
        #Generate plot 1
        p1=ggplot(subset(mtcars, am==0), aes(x=wt, y=mpg, colour=carb)) + 
                  geom_point(size=2)+
                  labs(title="Graph 1")+
                  scale_color_gradientn(colours=rainbow(5))
        
        #Generate plot 2
        p2=ggplot(subset(mtcars, am==1), aes(x=wt, y=mpg, colour=carb)) + 
                  geom_point(size=2)+
                  labs(title="Graph 2")+
                  scale_color_gradientn(colours=rainbow(5)) 
        

        因此,如果我们使用 grid.arrange 将两个图绘制在一起,您应该会得到:

        grid.arrange(arrangeGrob(p1,
                                 p2,
                                 nrow = 1))
        

        Graphs without equivalent color scale

        因此,我们希望两个图表的范围相同,并且只绘制其中一个结肠囊。您需要做的是首先定义色标的范围。在这个例子中,让我们从:

        summary(mtcars$carb)
        
        >
        Min.   1st Qu.  Median  Mean   3rd Qu.   Max. 
        1.000   2.000   2.000   2.812   4.000   8.000 
        

        所以我们知道色阶应该是从 1 到 8。我们将这个范围定义为 col.range,然后我们用它来指定每个图形中的范围:

        #Define color range
        col.range=c(1,8)
        
        #Generate plot 1
        p1=ggplot(subset(mtcars, am==0), aes(x=wt, y=mpg, colour=carb)) + 
                  geom_point(size=2)+
                  labs(title="Graph 1")+
                  scale_color_gradientn(colours=rainbow(5),limits=col.range) #look here
        
        #Generate plot 2
        p2=ggplot(subset(mtcars, am==1), aes(x=wt, y=mpg, colour=carb)) + 
                  geom_point(size=2)+
                  labs(title="Graph 2")+
                  scale_color_gradientn(colours=rainbow(5),limits=col.range) #look here 
        
        
        #Plot both graphs together
         grid.arrange(arrangeGrob(p1,
                                  p2,
                                  nrow = 1))
        

        这将为您提供以下图表。现在,两个图表之间的颜色是可比较的。

        Graphs with same color scale

        但是重复的结肠秤是多余的,所以我们只想使用一个。

        所以为了得到漂亮的最终图,我们可以使用我们之前定义的相同的 p1 和 p2 图,我们只是在 grid.arrange 函数上指定为:

        #Create al element that will represent your color scale:
        color.legend=gtable_filter(ggplotGrob(p1),"guide-box")
        
        #We hide de color scale on each individual graph
        #Then we insert the color scale and we adjust the ratio of it with the graphs
        #For this we define the theme() as follows:
        
        grid.arrange(arrangeGrob(p1+theme(legend.position="none"),
                                 p2+theme(legend.position="none"),
                                 nrow = 1), #Here we have just remove the color scale
                     color.scale, #We inserted the color scale. 
                     nrow=1, #We put the color scale to the right of the graph
                     widths=c(20,1) #With this we make the color scale much narrower
        

        这样你就完成了,得到下图:

        Graphs with just one color scale

        希望有用!!!!!!

        请评价!!!!

        【讨论】:

          猜你喜欢
          • 2017-10-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-18
          • 1970-01-01
          • 1970-01-01
          • 2017-11-21
          • 2017-09-17
          • 1970-01-01
          相关资源
          最近更新 更多