带有“负”的直方图对数刻度在R中 [英] Histogram with "negative" logarithmic scale in R

查看:470
本文介绍了带有“负”的直方图对数刻度在R中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些异常值的数据集,如下所示:

  x < -  rnorm(1000,0,20 )
x< - c(x,500,-500)

在这个线性的x轴上,我们看到

 直方图(x)

p>

我使用这个有用的线程制定了一个很好的方式将它放在日志规模上:
关于stats.stackexchange.com的这个问题,以获得讨论,详细信息和其他选项。

如果这是可接受的方法,您可以为ggplot创建一个自定义比例。以下代码演示了如何创建和使用自定义比例(使用自定义休息)以及asinh()转换的可视化。

  library(ggplot2)
库(比例)

限制< - 100
step < - 0.005
demo < - data.frame(x = seq(from = -1 * limits,to = limits,by = step))

asinh_trans< ;函数(){
trans_new(name ='asinh',transform = function(x)asinh(x),
inverse = function(x)sinh(x))
}

ggplot(demo,aes(x,x))+ geom_point(size = 2)+
scale_y_continuous(trans ='asinh',breaks = c(-100,-50,-10 ,-1,0,1,10,50,100))+
theme_bw()

  ggplot(demo,aes(x,x))+ geom_point(size = 2)+ 
scale_x_continuous(trans ='asinh',breaks = c(0,1,10,50,100))+
scale_y_log10(breaks = c(0,1,10,50,100))+#zero不会绘制
xlab(asinh()scale)+ ylab(log10 scale)+
theme_bw()


I have a dataset with some outliers, such as the following

x <- rnorm(1000,0,20)
x <- c(x, 500, -500)

If we plot this on a linear x axis scale at this we see

histogram(x)

I worked out a nice way to put it on a log scale using this useful thread: how to use a log scale for y-axis of histogram in R? :

mat <- data.frame(x)
ggplot(ee, aes(x = xx)) + geom_histogram(colour="darkblue", size=1, fill="blue") + scale_x_log10()

However, I would like the x axis labels from this 2nd example to match that of the first example, except with a kind of "negative log" - i.e. first tick (moving from the centre to the left) could be -1, then the next could be -10, the next -100, but all equidistant. Does that make sense?

解决方案

I am not sure I understand your goal, but when you want a log-like transformation yet have zeroes or negative values, the inverse hyperbolic sine transformation asinh() is often a good option. It is log-like for large values and is defined for all real values. See Rob Hyndman's blog and this question on stats.stackexchange.com for discussion, details, and other options.

If this is an acceptable approach, you can create a custom scale for ggplot. The code below demonstrates how to create and use a custom scale (with custom breaks), along with a visualization of the asinh() transformation.

library(ggplot2)
library(scales)

limits <- 100
step <- 0.005
demo <- data.frame(x=seq(from=-1*limits,to=limits,by=step))

asinh_trans <- function(){
  trans_new(name = 'asinh', transform = function(x) asinh(x), 
            inverse = function(x) sinh(x))
}

ggplot(demo,aes(x,x))+geom_point(size=2)+
     scale_y_continuous(trans = 'asinh',breaks=c(-100,-50,-10,-1,0,1,10,50,100))+
     theme_bw()

ggplot(demo,aes(x,x))+geom_point(size=2)+
     scale_x_continuous(trans = 'asinh',breaks=c(0,1,10,50,100))+
     scale_y_log10(breaks=c(0,1,10,50,100))+ # zero won't plot
     xlab("asinh() scale")+ylab("log10 scale")+
     theme_bw()

这篇关于带有“负”的直方图对数刻度在R中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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