离散化ggplot2色阶的连续比例的最简单方法是什么? [英] easiest way to discretize continuous scales for ggplot2 color scales?

查看:289
本文介绍了离散化ggplot2色阶的连续比例的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这样的情节:

$ g $ p $ ggplot(iris)+ geom_point(aes(x = Sepal.Width,y = Sepal.Length,color = Sepal.Length))+ scale_colour_gradient()



离散化的正确方法是什么色彩比例,如下图所示的接受答案(渐变突破ggplot stat_bin2d图)?

ggplot可以正确识别离散值并为这些离散值使用离散比例,但是我的问题是如果您有连续的数据,并且想要离散的颜色条(每个正方形对应到一个值,并且方块仍然以渐变方式着色),那么最好的方法是什么?离散化/分块应该发生在ggplot之外,并作为单独的离散值列放入数据框中,还是有办法在ggplot中执行?我寻找的一个例子是类似于这里显示的比例:
geom_tile / heatmap之外。



b
$ b

谢谢。

解决方案

解决方案有点复杂,因为您需要一个离散的规模。否则,您可以简单地使用 round

  library(ggplot2)$ (x),n = nclass.Sturges(x),b(b),b(b),b(b) min.n = 1)

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

binned< - cut(x,breaks(x))
$ b $ res< - colfunc(length(unique(binned)))[as.integer(binned)]
names(res)< - as.character(binned)$ b $ (名称(bincol(iris $ Sepal.Length,blue,yellow,red)))
中断< - unique(bincol(iris $ Sepal.Length,blue,yellow,red))
breaks < - breaks [order(labels,decrease = TRUE)]
labels < - 标签[order(labels,decrease = TRUE)]


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


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 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? 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? 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.

thanks.

解决方案

The solution is slightly complicated, because you want a discrete scale. Otherwise you could probably simply use 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")

这篇关于离散化ggplot2色阶的连续比例的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆