变换色彩比例,但使用ggplot2保留一个不错的图例 [英] Transform color scale, but keep a nice legend with ggplot2

查看:144
本文介绍了变换色彩比例,但使用ggplot2保留一个不错的图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到类似的问题,但我想直接问我的具体问题:我有一个散点图z变量编码为色标:

  library(ggplot2)
myData< - data.frame( x = rnorm(1000),
y = rnorm(1000))
myData $ z < - with(myData,x * y)

badVersion< - ggplot(myData ,
aes(x = x,y = y,color = z))
badVersion < - badVersion + geom_point()
print(badVersion)

这产生了这个结果:



正如您所看到的,由于z变量是正态分布的,因此只有极少数点用极端的分布颜色。这是应该的,但我有兴趣强调差异。这样做的一种方法是使用:

  betterVersion<  -  ggplot(myData,
aes(x = x,y = y,color = rank(z)))
betterVersion < - betterVersion + geom_point()
print(betterVersion)

其中产生这个:



通过将rank()应用于z变量,我更加强调了z变量内的微小差异。人们可以想象在这里使用任何转换,而不是等级,但你明白了。



我的问题实质上是最直接的方式,或者最真正的ggplot2的方式,获得原始单位的传奇(z的单位,而不是z的等级),同时保持彩色点的转换版本?



<我有一种感觉,这种方式使用rescaler(),但我不清楚如何使用rescaler()进行任意转换等等。一般来说,更清晰的例子会很有用。



预先感谢您的时间。

解决方案

查看包 scale
特别是
?trans



我认为在给定获得价值的可能性或更极端的情况下映射颜色的转换应该是合理的(基本上是 pnorm(z)

我认为 scale_colour_continuous(trans = pro bability_trans(distribution ='norm')应该可以工作,但会抛出警告。所以我定义了一个新的转换(参见?trans_new



我必须定义一个转换和一个相反的

<$ (函数)函数(x)pnorm(x),函数(x)qnorm(x)pnorm(x),函数(x)qnorm (x))
}

badVersion + geom_point()+ scale_colour_continuous(trans ='norm'))



使用提供的 probability_trans 会引发警告并且看起来不起作用

 #这会抛出警告
badVersion + geom_point +
scale_colour_continuous(trans = probability_trans(distribution ='norm'))

##警告信息:
##在qfun(x,...)中:NaNs产生


I have seen somewhat similar questions to this, but I'd like to ask my specific question as directly as I can:

I have a scatter plot with a "z" variable encoded into a color scale:

library(ggplot2)
myData <- data.frame(x = rnorm(1000),
                     y = rnorm(1000))
myData$z <- with(myData, x * y)

badVersion <- ggplot(myData,
              aes(x = x, y = y, colour = z))
badVersion <- badVersion + geom_point()
print(badVersion)

Which produces this:

As you can see, since the "z" variable is normally distributed, very few of the points are colored with the "extreme" colors of the distribution. This is as it should be, but I am interested in emphasizing difference. One way to do this would be to use:

betterVersion <- ggplot(myData,
                        aes(x = x, y = y, colour = rank(z)))
betterVersion <- betterVersion + geom_point()
print(betterVersion)

Which produces this:

By applying rank() to the "z" variable, I get a much greater emphasis on minor differences within the "z" variable. One could imagine using any transformation here, instead of rank, but you get the idea.

My question is, essentially, what is the most straightforward way, or the most "true ggplot2" way, of getting a legend in the original units (units of z, as opposed to the rank of z), while maintaining the transformed version of the colored points?

I have a feeling this uses rescaler() somehow, but it is not clear to me how to use rescaler() with arbitrary transformations, etc. In general, more clear examples would be useful.

Thanks in advance for your time.

解决方案

Have a look at the package scales especially ?trans

I think that a transformation that maps the colour given the probability of getting the value or more extreme should be reasonable (basically pnorm(z))

I think that scale_colour_continuous(trans = probability_trans(distribution = 'norm') should work, but it throws warnings.

So I defined a new transformation (see ?trans_new)

I have to define a transformation and an inverse

library(scales)
norm_trans <- function(){
  trans_new('norm', function(x) pnorm(x), function(x) qnorm(x))
}

badVersion + geom_point() + scale_colour_continuous(trans = 'norm'))

Using the supplied probability_trans throws a warning and doesn't seem to work

# this throws a warning
badVersion + geom_point+
  scale_colour_continuous(trans = probability_trans(distribution = 'norm'))

## Warning message:
## In qfun(x, ...) : NaNs produced

这篇关于变换色彩比例,但使用ggplot2保留一个不错的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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