I have an object that is a factor with a number of levels:
x <- as.factor(c(rep("A",20),rep("B",10),rep("C",15)))
In the shortest manner possible, I would like to use ggplot to create a bar graph of the % frequency of each factor.
I keep finding that there are a lot of little annoyances that get in between summarizing and plotting when I have a factor. Here are a few examples of what I mean by annoyances:
as.data.frame(summary(x))
You have to rename the columns and the 1st column values are now rownames in the last example. In the next, you have to cheat to use cast and then you have to relabel because it defaults to a colname of “(all)”.
as.data.frame(q1$com.preferred)
dat$value <- 1
colnames(dat) <- c("pref", "value")
cast(dat, pref ~.)
colnames(dat)[2] <- "value"
Here’s another example, somewhat better, but less than ideal.
data.frame(x=names(summary(x)),y=summary(x))
If there’s a quick way to do this within ggplot, I’d be more than interested to see it. So far, my biggest problem is changing counts to frequencies.
Following up on @dirk and @joran’s suggestions (@joran really gets credit. I thought
as.data.frame(), and not justdata.frame(), was necessary, but it turns out @joran’s right …edit: shortened slightly (incorporated @Chase’s
prop.tabletoo)