本文介绍了使用stat_summary_hex以离散色阶显示最常见的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个10k行和3列的数据框:xpos,ypos和cluster(cluster是一个从0到9的数字):



我想根据最频繁的颜色显示六边形的六角形图



到目前为止,我得到了:

  library(ggplot2)
library(hexbin)
ggplot(clusters,aes(x = xpos,y = ypos,z = cluster))+ stat_summary_hex(fun.x = mode)

我想给我的是我想要的东西(即填充每个六边形的颜色从0到9),但色彩比例连续出现,我无法弄清楚如何使用离散色彩。



对于额外的上下文,这里是数据的底层混乱视图,我试图平滑通过使用六边形:

  qplot(data = clusters,xpos,ypos,color = factor(cluster))
stat_summary_hex(fun.x = mode)在做什么,但我是很确定这不是你的想法( mode 给出对象的存储模式,而不是统计模式, fun.x 不匹配 stat_summary_hex 的任何正式参数)。尝试这个。它列出了每个箱中的观察值,并且拉出了最大值的标签。

  ggplot(clusters,aes(x = xpos,y = ypos,z = cluster))+ stat_summary_hex(fun = function(x){
tab< - table(x)
names(tab)[which.max(tab)]
})

I have a data frame with 10k rows and 3 columns: xpos, ypos and cluster (cluster is a number from 0 to 9) here: http://pastebin.com/NyQw29tb

I would like to show a hex plot with each hexagon colored according to the most-frequent cluster within that hexagon.

So far I've got:

 library(ggplot2)
 library(hexbin)
 ggplot(clusters, aes(x=xpos, y=ypos, z=cluster)) + stat_summary_hex(fun.x=mode)

Which I think is giving me what I want (i.e. is filling in every hexagon with a color from 0 to 9), but the color scale appears continuous, and I can't figure out how to make it use a discrete one.

For extra context, here's the underlying, messier view of the data, which I'm trying to smooth out by using hexagons:

 qplot(data=clusters, xpos, ypos, color=factor(cluster))
解决方案

I don't knw what your stat_summary_hex(fun.x=mode) is doing, but I'm pretty sure it's not what you think (mode gives the storage mode of an object, not the statistical mode, and fun.x doesn't match any formal argument of stat_summary_hex). Try this. It tabulates the observations in each bin, and pulls out the label of the maximum count.

ggplot(clusters, aes(x=xpos, y=ypos, z=cluster)) + stat_summary_hex(fun = function(x) {
    tab <- table(x)
    names(tab)[which.max(tab)]
})

这篇关于使用stat_summary_hex以离散色阶显示最常见的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 14:16