scale_y_log10不能正确填充geom_density [英] geom_density doesn't fill correctly with scale_y_log10

查看:236
本文介绍了scale_y_log10不能正确填充geom_density的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

  require(ggplot2)
set.seed(0)
xvar< - rnorm(100)
ggplot(data.frame(xvar),aes(xvar))+ geom_density(fill =lightblue)+ scale_y_log10()

该图如下所示:



如何在密度估算的右侧(即下图)制作图表阴影?

问题在于,默认情况下, stat_density 填充密度和 y = 0 已转换数据的一行。所以改变y = 0线的转换将成为这类问题的牺牲品。我个人认为这是 ggplot2 中的一个错误,尽管由于图形语法专家可能认为y转换后的密度没有意义,所以这个错误可能没有引起足够的重视。 p>

一个非常方便的解决方法是手动将偏移量添加到 .. density .. ,您将不得不显式调用,然后改变休息时间,使它看起来像你没有做任何奇怪的事情。

  require(ggplot2)
需要(比例)
set.seed(0)
xvar < - rnorm(100000)
quartz(height = 4,width = 6)
ggplot(data.frame (xvar),aes(x = xvar,y = log10(.. density ..)+ 4))+
geom_density(fill ='lightblue')+
scale_y_continuous(breaks = c(0, 1,2,3,4),
labels = c('0.0001','0.001','0.01','0.1','1'),限制= c(0,4),
'name ='density')
quartz.save('![StackOverflow_29111741_v2] [1] .png')

该代码生成此图:


Code:

require(ggplot2)
set.seed(0)
xvar <- rnorm(100)
ggplot(data.frame(xvar), aes(xvar)) + geom_density(fill="lightblue") + scale_y_log10()

The graph is something like this:

How can I make the graph shade on the right side of (viz. below) the density estimate?

解决方案

The problem is that stat_density by default fills between the density and the y=0 line of the transformed data. So transformations that alter the y=0 line will fall victim to problems of this sort. I personally think this is a bug in ggplot2, although since graphical grammar experts probably argue that y-transformed densities are meaningless, the bug may not get a lot of attention.

A very kludgy workaround is to manually add an offset to ..density.., which you will have to explicitly invoke, and then change the breaks to make it look like you didn't do anything weird.

require(ggplot2)
require(scales)
set.seed(0)
xvar <- rnorm(100000)
quartz(height=4,width=6)
ggplot(data.frame(xvar), aes(x=xvar, y=log10(..density..)+4)) + 
    geom_density(fill='lightblue') +
    scale_y_continuous(breaks=c(0,1,2,3,4), 
        labels=c('0.0001', '0.001', '0.01', '0.1','1'), limits=c(0,4),
        name='density')
quartz.save('![StackOverflow_29111741_v2][1].png')

That code produces this graph:

这篇关于scale_y_log10不能正确填充geom_density的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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