分类变量的频率密度条形图 [英] Frequency density barplot of categorical variable

查看:166
本文介绍了分类变量的频率密度条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to plot a barplot of frequency density of the following ordered categorical data:

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

如果我绘图:

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

我得到:

但是我想将所有值除以总和以获得频率密度: IE. plot(ACC[EA$TYPE=="A"]/sum(as.numeric(ACC[EA$TYPE=="A"]))),但这不起作用.有提示吗?

But I'd like to divide all the values by the total to get a frequency density: Ie. plot(ACC[EA$TYPE=="A"]/sum(as.numeric(ACC[EA$TYPE=="A"]))) but that doesn't work. Any tips?

干杯

推荐答案

factor的默认绘图功能是barplot.因此,如果您要使用其他图形,则直接使用此功能可能会更容易:(带有随机因子示例)

The default plotting function for a factor is barplot. So if you want a different graph, it may be easier to directly use this function: (with a random factor example)

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

获得ggplot2相同的结果比较棘手,由于某种原因,我需要将数据放在data.frame中才能起作用:

Getting the same result with ggplot2 is trickier, and for some reason, I needed to put the data in a data.frame for it to work:

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

这篇关于分类变量的频率密度条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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