为因子变量制作频率直方图 [英] Make Frequency Histogram for Factor Variables

查看:79
本文介绍了为因子变量制作频率直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对R非常陌生,因此我对这个基本问题深表歉意。我花了一个小时搜索这个问题,但找不到解决方法。

I am very new to R, so I apologize for such a basic question. I spent an hour googling this issue, but couldn't find a solution.

说我的数据集中有一些关于常见宠物类型的分类数据。我将其输入为R中的字符向量,其中包含不同类型的动物的名称。我是这样创建的:

Say I have some categorical data in my data set about common pet types. I input it as a character vector in R that contains the names of different types of animals. I created it like this:

animals <- c("cat", "dog",  "dog", "dog", "dog", "dog", "dog", "dog", "cat", "cat", "bird")

我将其变成与数据框中其他向量一起使用的一个因素:

I turn it into a factor for use with other vectors in my data frame:

animalFactor <- as.factor(animals)

我现在想创建一个直方图,以显示y轴上的每个变量,x轴上每个因子的名称,并且每个因子包含一个条。我尝试使用此代码:

I now want to create a histogram that shows the frequency of each variable on the y-axis, the name of each factor on the x-axis, and contains one bar for each factor. I attempt this code:

hist(table(animalFactor), freq=TRUE, xlab = levels(animalFactor), ylab = "Frequencies")

输出绝对不像我期望的那样。除了标注问题外,我似乎无法弄清楚如何按类别创建简单的频率直方图。

The output is absolutely nothing like I'd expect. Labeling problems aside, I can't seem to figure out how to create a simple frequency histogram by category.

推荐答案

似乎您想要 barplot(prop.table(table(animals)))

但是,这不是直方图。

这篇关于为因子变量制作频率直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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