gpt4 book ai didi

R中的相对频率直方图,ggplot

转载 作者:行者123 更新时间:2023-12-04 11:51:14 28 4
gpt4 key购买 nike

我可以使用 lattice 在 R 中绘制相对频率直方图包裹:

a <- runif(100)
library(lattice)
histogram(a)

我想在 ggplot 中获得相同的图形.我试过
dt <- data.frame(a)
ggplot(dt, aes(x = a)) +
geom_bar(aes(y = ..prop..))+
scale_y_continuous(labels=percent)

但它不是那样工作的。我应该在代码中更改什么?在图表之前计算相对频率对我来说不是一个选择。

最佳答案

您需要直方图,而不是条形图,因此:

ggplot(dt, aes(x = a)) + 
geom_histogram(aes(y = stat(count) / sum(count)), bins = 8) +
scale_y_continuous(labels = scales::percent)
lattice :

enter image description here
ggplot2 :

enter image description here

您可以看到分箱算法对两个包的工作方式略有不同。

关于R中的相对频率直方图,ggplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57430885/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com