了解ggplot2中的带宽平滑 [英] Understanding bandwidth smoothing in ggplot2

查看:127
本文介绍了了解ggplot2中的带宽平滑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

realdata = https://www.dropbox.com/s/pc5tp2lfhafgaiy/realdata .txt



simulation = https://www.dropbox.com/s/5ep95808xg7bon3/simulation.txt



使用带宽= 1.5的数据的密度图给出我有以下情节:

  prealdata = scan(realdata.txt)
simulation = scan(simulation。 (密度(log10(模拟),bw = 1.5),lty = 2)
图(密度(log10(realdata),bw = 1.5))
行c $ c>



但是使用ggplot2绘制相同的数据,带宽参数(adjust)似乎工作方式不同。为什么?

  vec1 = data.frame(x = log10(realdata))
vec2 = data.frame(x = log10(模拟))
require(ggplot2)
ggplot()+
geom_density(aes(x = x,linetype =real data),data = vec1,adjust = 1.5) +
geom_density(aes(x = x,linetype =simulation),data = vec2,adjust = 1.5)+
scale_linetype_manual(name =data,values = c(real data= solid,simulation=dashed))



关于如何更好地平滑这些数据的建议也是非常欢迎!

解决方案

adjust = 不是与 bw = 相同。 (密度(log10(realdata),bw = 1.5))
lines(density(log10) (模拟),bw = 1.5),lty = 2)

c $ c> ggplot




出于任何原因, ggplot 不允许您指定 bw = 参数。默认情况下, density 使用 bw.nrd0(),所以当您使用基本图形更改此图时,使用 ggplot 更改此值。但是使用的是 adjust * bw 。因此,由于我们知道如何计算默认 bw ,我们可以重新计算 adjust = 以使用相同的值。 / b>

  #helper函数
bw <-function(b,x){b / bw.nrd0(x)}

require(ggplot2)
ggplot()+
geom_density(aes(x = x,linetype =real data),data = vec1,adjust = bw(1.5,vec1 $ x))+
geom_density(aes(x = x,linetype =simulation),data = vec2,adjust = bw(1.5,vec2 $ x))+
scale_linetype_manual(name =data ,
values = c(real data=solid,simulation=dashed))

结果是


这与基本图形绘图相同。


realdata = https://www.dropbox.com/s/pc5tp2lfhafgaiy/realdata.txt

simulation = https://www.dropbox.com/s/5ep95808xg7bon3/simulation.txt

A density plot of this data using bandwidth=1.5 gives me the following plot:

prealdata = scan("realdata.txt")
simulation = scan("simulation.txt")
plot(density(log10(realdata), bw=1.5))
lines(density(log10(simulation), bw=1.5), lty=2)

But using ggplot2 to plot the same data, bandwidth argument (adjust) seems to be working differently. Why?

vec1 = data.frame(x=log10(realdata))
vec2 = data.frame(x=log10(simulation))
require(ggplot2)
ggplot() +
geom_density(aes(x=x, linetype="real data"), data=vec1, adjust=1.5) +
geom_density(aes(x=x, linetype="simulation"), data=vec2, adjust=1.5) +
scale_linetype_manual(name="data", values=c("real data"="solid", "simulation"="dashed"))

Suggestions on how to better smooth this data are also very welcome!

解决方案

adjust= is not the same as bw=. When you plot

plot(density(log10(realdata), bw=1.5))
lines(density(log10(simulation), bw=1.5), lty=2)

you get the same thing as ggplot

For whatever reason, ggplot does not allow you to specify a bw= parameter. By default, density uses bw.nrd0() so while you changed this for the plot using base graphics, you cannot change this value using ggplot. But what get's used is adjust*bw. So since we know how to calculate the default bw, we can recalculate adjust= to give use the same value.

#helper function
bw<-function(b, x) { b/bw.nrd0(x) }

require(ggplot2)
ggplot() +
geom_density(aes(x=x, linetype="real data"), data=vec1, adjust=bw(1.5, vec1$x)) +
geom_density(aes(x=x, linetype="simulation"), data=vec2, adjust=bw(1.5, vec2$x)) +
scale_linetype_manual(name="data", 
    values=c("real data"="solid", "simulation"="dashed"))

And that results in

which is the same as the base graphics plot.

这篇关于了解ggplot2中的带宽平滑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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