ggplot2:Stat_function与行为规模的不当行为 [英] ggplot2: Stat_function misbehaviour with log scales
问题描述
我正在绘制一个点对数直方图(一个直方图,显示了一个点而不是条形的值),它是对数比例的。结果应该如下所示:
MWE:
让我们模拟一些数据:
set.seed(123)
d < - data.frame(x = rnorm(1000))
要得到点直方图,我需要首先计算直方图数据(hdata)
hdata < - hist(d $ x,plot = FALSE)
tmp< - data.frame(mids = hdata $ mids,
density = hdata $ density,
counts = hdata $ counts)
我们可以这样绘图
p < - ggplot(tmp,aes(x = mids,y = density))+ geom_point()+
stat_function(fun = dnorm,col =red)
p
得到这张图:
理论上我们应该能够应用l og scale(并将y-limits设置为大于0),并且我们应该有一个与目标图形相似的图片。
但是,如果我应用它,以下图表:
p + scale_y_log10(限制= c(0.001,10))
stat_function清楚地显示非缩放值,而不是在第一张图片中产生更接近实线的数字。
任何想法?
奖金 编辑解决方法 一种可能的解决方案是计算ggplot之外的dnorm数据,然后将其作为一行插入。例如 这将返回如下图形。这基本上是图形,但它不能解决stat_function问题。 I am trying to plot a point histogram (a histogram that shows the values with a point instead of bars) that is log-scaled. The result should look like this:
Lets simulate some Data: To get the point histogram I need to calculate the histogram data (hdata) first which we can plot like this to get this graph:
In theory we should be able to apply the log scales (and set the y-limits to be above 0) and we should have a similar picture to the target graph. However, if I apply it I get the following graph: The stat_function clearly shows non-scaled values instead of producing a figure closer to the solid line in the first picture. Any ideas? Bonus
Are there any ways to graph the histogram with dots without using the hist(..., plot = FALSE) function? EDIT Workaround One possible solution is to calculate the dnorm-data outside of ggplot and then insert it as a line. For example This returns a graph like the following. This is basically the graph, but it doesn't resolve the stat_function issue.
这篇关于ggplot2:Stat_function与行为规模的不当行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
有没有什么方法可以在不使用hist(...,plot = FALSE)函数?
tmp2 < - data.frame(mids = seq(from = min(tmp $ mids))to = max (tmp $ mids),
by =(max(tmp $ mids) - min(tmp $ mids))/ 10000))
tmp2 $ dnorm < - dnorm(tmp2 $ mids)
#绘制
ggplot()+
geom_point(data = tmp,aes(x = mids,y = density))+
geom_line(data = tmp2,aes x = mids,y = dnorm),col =red)+
scale_y_log10()
library(ggplot2)
set.seed(123)
d< - data.frame(x = rnorm(1000))
ggplot(d,aes(x))+
stat_bin(geom =point,
aes(y = ..density ..),
#same break as function hist的默认值:
breaks = pretty(范围(d $ x),n = nclass.Sturges(d $ x),min.n = 1),
position =identity)+
stat_function(fun = dnorm,col =red)+
scale_y_log10(limits = c(0.001,10))
MWE:set.seed(123)
d <- data.frame(x = rnorm(1000))
hdata <- hist(d$x, plot = FALSE)
tmp <- data.frame(mids = hdata$mids,
density = hdata$density,
counts = hdata$counts)
p <- ggplot(tmp, aes(x = mids, y = density)) + geom_point() +
stat_function(fun = dnorm, col = "red")
p
p + scale_y_log10(limits = c(0.001, 10))
tmp2 <- data.frame(mids = seq(from = min(tmp$mids), to = max(tmp$mids),
by = (max(tmp$mids) - min(tmp$mids))/10000))
tmp2$dnorm <- dnorm(tmp2$mids)
# Plot it
ggplot() +
geom_point(data = tmp, aes(x = mids, y = density)) +
geom_line(data = tmp2, aes(x = mids, y = dnorm), col = "red") +
scale_y_log10()
library(ggplot2)
set.seed(123)
d <- data.frame(x = rnorm(1000))
ggplot(d, aes(x)) +
stat_bin(geom = "point",
aes(y = ..density..),
#same breaks as function hist's default:
breaks = pretty(range(d$x), n = nclass.Sturges(d$x), min.n = 1),
position = "identity") +
stat_function(fun = dnorm, col = "red") +
scale_y_log10(limits = c(0.001, 10))