简体   繁体   English

将ggplot2色标连续缩放的最简单方法是什么?

[英]easiest way to discretize continuous scales for ggplot2 color scales?

Suppose I have this plot: 假设我有这个情节:

ggplot(iris) + geom_point(aes(x=Sepal.Width, y=Sepal.Length, colour=Sepal.Length)) + scale_colour_gradient()

what is the correct way to discretize the color scale, like the plot shown below the accepted answer here ( gradient breaks in a ggplot stat_bin2d plot )? 什么是离散色标的正确方法,如下面接受的答案下面显示的图( ggplot stat_bin2d图中的渐变断点 )?

ggplot correctly recognizes discrete values and uses discrete scales for these, but my question is if you have continuous data and you want a discrete colour bar for it (with each square corresponding to a value, and squares colored in a gradient still), what is the best way to do it? ggplot正确识别离散值,并为这些使用离散标度,但我的问题是,如果你有连续数据,你想要一个离散的颜色条(每个方格对应一个值,方块仍然以渐变着色),什么是最好的方法吗? Should the discretizing/binning happen outside of ggplot and get put in the dataframe as a separate discrete-valued column, or is there a way to do it within ggplot? 离散/分箱是否应该发生在ggplot之外并作为单独的离散值列放入数据帧中,或者是否有办法在ggplot中进行? an example of what I'm looking for is similar to the scale shown here: 我正在寻找的一个例子类似于此处显示的比例: 在此输入图像描述

except I'm plotting a scatter plot and not something like geom_tile /heatmap. 除了我正在绘制一个散点图而不是像geom_tile / geom_tile这样的东西。

thanks. 谢谢。

The solution is slightly complicated, because you want a discrete scale. 解决方案有点复杂,因为您需要一个离散的比例。 Otherwise you could probably simply use round . 否则你可能只是简单地使用round

library(ggplot2)

bincol <- function(x,low,medium,high) {
  breaks <- function(x) pretty(range(x), n = nclass.Sturges(x), min.n = 1)

  colfunc <- colorRampPalette(c(low, medium, high))

  binned <- cut(x,breaks(x))

  res <- colfunc(length(unique(binned)))[as.integer(binned)]
  names(res) <- as.character(binned)
  res
}

labels <- unique(names(bincol(iris$Sepal.Length,"blue","yellow","red")))
breaks <- unique(bincol(iris$Sepal.Length,"blue","yellow","red"))
breaks <- breaks[order(labels,decreasing = TRUE)]
labels <- labels[order(labels,decreasing = TRUE)]


ggplot(iris) + 
  geom_point(aes(x=Sepal.Width, y=Sepal.Length,
                 colour=bincol(Sepal.Length,"blue","yellow","red")), size=4) +
  scale_color_identity("Sepal.Length", labels=labels, 
                       breaks=breaks, guide="legend")

在此输入图像描述

You could try the following, I have your example code modified appropriately below: 您可以尝试以下操作,我在下面适当地修改了您的示例代码:

#I am not so great at R, so I'll just make a data frame this way
#I am convinced there are better ways. Oh well.
df<-data.frame()
for(x in 1:10){
  for(y in 1:10){
    newrow<-c(x,y,sample(1:1000,1))
    df<-rbind(df,newrow)
  }
}
colnames(df)<-c('X','Y','Val')


#This is the bit you want
p<- ggplot(df, aes(x=X,y=Y,fill=cut(Val, c(0,100,200,300,400,500,Inf))))
p<- p + geom_tile() + scale_fill_brewer(type="seq",palette = "YlGn")
p<- p + guides(fill=guide_legend(title="Legend!"))

#Tight borders
p<- p + scale_x_continuous(expand=c(0,0)) + scale_y_continuous(expand=c(0,0))
p

Note the strategic use of cut to discretize the data followed by the use of color brewer to make things pretty. 注意切割的战略用途是使数据离散化,然后使用颜色酿造器使事情变得美观。

The result looks as follows. 结果如下。

具有离散色的2D热图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM