gpt4 book ai didi

r - 分类变量的频率密度条形图

转载 作者:行者123 更新时间:2023-12-02 17:28:36 25 4
gpt4 key购买 nike

我想绘制以下有序分类数据的频率密度条形图:

summary(ACC[EA$TYPE=="A"])
NG SG LG MG HG
2 25 36 17 0

如果我绘制:

plot(ACC[EA$TYPE=="A"])

我得到:

enter image description here

但我想将所有值除以总数以获得频率密度:IE。plot(ACC[EA$TYPE=="A"]/sum(as.numeric(ACC[EA$TYPE=="A"]))) 但这不起作用。有什么建议吗?

干杯,

最佳答案

因子 的默认绘图函数是barplot。所以如果你想要一个不同的图,直接使用这个函数可能会更容易:(以随机因子为例)

f <- factor(sample(letters[1:5], 100, r=T))
h <- table(f) / length(f)
barplot(h)

enter image description here

获取 same result with ggplot2比较棘手,出于某种原因,我需要将数据放在 data.frame 中才能正常工作:

dat <- data.frame(f = f)
library(ggplot2)
ggplot(dat, aes(x=f, y=..count.. / sum(..count..), fill=f)) + geom_bar()

enter image description here

关于r - 分类变量的频率密度条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36495155/

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