Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Frequency Histogram for Factor Variables

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.

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) 

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.

like image 621
OnlyDean Avatar asked Feb 07 '14 23:02

OnlyDean


People also ask

Can you use a histogram for categorical variables?

A histogram can be used to show either continuous or categorical data in a bar graph.

How do you find the frequency distribution of a categorical variable?

use the table() function to find the distribution of categorical values.


2 Answers

It seems like you want barplot(prop.table(table(animals))):

enter image description here

However, this is not a histogram.

like image 131
Roland Avatar answered Sep 21 '22 19:09

Roland


If you'd like to do this in ggplot, an API change was made to geom_histogram() that leads to an error: https://github.com/hadley/ggplot2/issues/1465

To get around this, use geom_bar():

animals <- c("cat", "dog",  "dog", "dog", "dog", "dog", "dog", "dog", "cat", "cat", "bird")  library(ggplot2) # counts ggplot(data.frame(animals), aes(x=animals)) +   geom_bar() 

enter image description here

like image 32
Megatron Avatar answered Sep 21 '22 19:09

Megatron