HCAI
HCAI

Reputation: 2263

Frequency density barplot of categorical variable

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 

If I plot:

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

I get:

enter image description here

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?

Cheers,

Upvotes: 2

Views: 7302

Answers (2)

Vincent Guillemot
Vincent Guillemot

Reputation: 3429

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)

enter image description here

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()

enter image description here

Upvotes: 3

Vincent Bonhomme
Vincent Bonhomme

Reputation: 7453

It would be easier to fix it with a reproducible example, so I created one for you. The following works like a charm:

# creates the vector
x <- c(2, 25, 36, 17,  0)
names(x) <-  c("NG", "SG", "LG", "MG", "HG") 

# raw x = counts
barplot(x, ylab="Count")
# when divided by the total count, we obtain frequencies and we barplot them
barplot(x/sum(x), ylab="Frequency")

I see no reason why this could not work as long as ACC[EA$TYPE=="A"] is a numeric:

barplot(ACC[EA$TYPE=="A"]/sum(ACC[EA$TYPE=="A"]), ylab="Frequency")

Upvotes: 3

Related Questions