2

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,

Community
  • 1
  • 1
HCAI
  • 2,213
  • 8
  • 33
  • 65

2 Answers2

3

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")
Community
  • 1
  • 1
Vincent Bonhomme
  • 7,235
  • 2
  • 27
  • 38
  • Thank you for this, I appreciate your help. In particular thank you for putting the link to a reproducible example. I know I need to do this but never know how. – HCAI Apr 08 '16 at 09:02
3

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

Community
  • 1
  • 1
Vincent Guillemot
  • 3,394
  • 14
  • 21