r - 如何使用ggplot在直方图上叠加任意参数分布?

标签 r data-visualization histogram ggplot2

如何使用ggplot在直方图上叠加任意参数分布?

我已经基于Quick-R example进行了尝试,但是我不知道缩放因子来自何处。这种方法合理吗?如何修改它以使用ggplot?

下面是使用此方法对正态分布和对数正态分布进行过度绘制的示例:

## Get a log-normalish data set: the number of characters per word in "Alice in Wonderland"
alice.raw <- readLines(con = "http://www.gutenberg.org/cache/epub/11/pg11.txt", 
                       n = -1L, ok = TRUE, warn = TRUE,
                       encoding = "UTF-8")

alice.long <- paste(alice.raw, collapse=" ")
alice.long.noboilerplate <- strsplit(alice.long, split="\\*\\*\\*")[[1]][3]
alice.words <- strsplit(alice.long.noboilerplate, "[[:space:]]+")[[1]]
alice.nchar <- nchar(alice.words)
alice.nchar <- alice.nchar[alice.nchar > 0]

# Now we want to plot both the histogram and then log-normal probability dist
require(MASS)
h <- hist(alice.nchar, breaks=1:50, xlab="Characters in word", main="Count")
xfit <- seq(1, 50, 0.1)

# Plot a normal curve
yfit<-dnorm(xfit,mean=mean(alice.nchar),sd=sd(alice.nchar))
yfit <- yfit * diff(h$mids[1:2]) * length(alice.nchar) 
lines(xfit, yfit, col="blue", lwd=2) 

# Now plot a log-normal curve
params <- fitdistr(alice.nchar, densfun="lognormal")
yfit <- dlnorm(xfit, meanlog=params$estimate[1], sdlog=params$estimate[1])
yfit <- yfit * diff(h$mids[1:2]) * length(alice.nchar)
lines(xfit, yfit, col="red", lwd=2)

这将产生以下图:

为了澄清,我想在y轴上计数,而不是密度估计。

最佳答案

看看stat_function()

alice.raw <- readLines(con = "http://www.gutenberg.org/cache/epub/11/pg11.txt", 
                       n = -1L, ok = TRUE, warn = TRUE,
                       encoding = "UTF-8")

alice.long <- paste(alice.raw, collapse=" ")
alice.long.noboilerplate <- strsplit(alice.long, split="\\*\\*\\*")[[1]][3]
alice.words <- strsplit(alice.long.noboilerplate, "[[:space:]]+")[[1]]
alice.nchar <- nchar(alice.words)
alice.nchar <- alice.nchar[alice.nchar > 0]
dataset <- data.frame(alice.nchar = alice.nchar)
library(ggplot2)
ggplot(dataset, aes(x = alice.nchar)) + geom_histogram(aes(y = ..density..)) +
  stat_function(fun = dnorm, 
    args = c(
      mean = mean(dataset$alice.nchar), 
      sd = sd(dataset$alice.nchar)), 
    colour = "red")

如果要像示例中那样在y轴上计数,则需要一个将密度转换为计数的函数:
dnorm.count <- function(x, mean = 0, sd = 1, log = FALSE, n = 1, binwidth = 1){
  n * binwidth * dnorm(x = x, mean = mean, sd = sd, log = log) 
}

ggplot(dataset, aes(x = alice.nchar)) + geom_histogram(binwidth=1.6) + 
  stat_function(fun = dnorm.count, 
                args = c(
                  mean = mean(dataset$alice.nchar), 
                  sd = sd(dataset$alice.nchar), 
                  n = nrow(dataset), binwidth=1.6), 
                colour = "red")

关于r - 如何使用ggplot在直方图上叠加任意参数分布?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11224247/

相关文章:

r - 如何将 R 变量传递给 sqldf?

javascript - 未捕获的 TypeError : . 过滤器不是函数

javascript - 显示带有标签的城市/兴趣点的 geoChoroplethChart map

r - ggplot 直方图显示额外的灰色条

r - 直接在 ggplot 中设置直方图的 bin 数量

c# - 以编程方式执行 R 脚本

r - R中的4向维恩图?

r - 黄土线绘制不正确

javascript - 利用 p5.js/processing 在 GeoTIFF 上进行数据可视化?如何在网络墨卡托中绘制经度/纬度坐标?

c - 数组直方图主