I'm trying to have ggplot2 plot percentage instead of frequency on the y axis but it just doesn't work! I have added scale_y_continuous(labels = percent_format())
to my plot but it still displays frequencies. Here's my code:
ggplot(items) + geom_bar( aes(x = type, fill = category), position = "dodge") + scale_y_continuous(labels = percent_format())
Here is a sample of my dataset
head(items)
item type category
[1] PA100 1 A
[2] PB101 2 A
[3] UR360 2 A
[4] PX977 3 B
[5] GA008 3 B
[6] GR446 3 A
What I want to do is for each category A and B I want to plot the percentage of type 1, type 2, and type 3 it has; hence my code. But no matter what it keeps plotting the frequencies of type 1, 2, and 3 in each of the categories instead of the percentages :|
Try:
items = structure(list(item = structure(c(3L, 4L, 6L, 5L, 1L, 2L), .Label = c("GA008",
"GR446", "PA100", "PB101", "PX977", "UR360"), class = "factor"),
type = c(1L, 2L, 2L, 3L, 3L, 3L), category = structure(c(1L,
1L, 1L, 2L, 2L, 1L), .Label = c("A", "B"), class = "factor")), .Names = c("item",
"type", "category"), class = "data.frame", row.names = c(NA,
-6L))
items
item type category
1 PA100 1 A
2 PB101 2 A
3 UR360 2 A
4 PX977 3 B
5 GA008 3 B
6 GR446 3 A
tt = with(items, table(type, category))
tt
category
type A B
1 1 0
2 2 0
3 1 2
dd = data.frame(prop.table(tt, 2))
dd
type category Freq
1 1 A 0.25
2 2 A 0.50
3 3 A 0.25
4 1 B 0.00
5 2 B 0.00
6 3 B 1.00
ggplot(dd)+geom_bar(aes(x=type, y=Freq, fill=category), stat='identity', position='dodge')
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments