Reputation: 1
I am plotting a barplot for a summary data.frame consisting of ten records. Each record lists a size and a frequency for that size. One size class has a zero frequency.
However, when run, the zero-count class vanishes and instead of the full ten classes, only nine appear in the plot.
I've tried an as.numeric, but that renders as a decimal and doesn't reflect the factor value.
The ggpot2 code I am using is:
plt1 <- ggplot(szt, aes(x = as.factor(size, Freq))) +
geom_bar(stat = "identity")
plt1 + xlab("Debitage Size, 5 mm class") + ylab("Frequency") +
ggtitle("Debitage Size Distribution by 5 mm class")
The data is:
size Freq
1 1 196
2 2 261
3 3 77
4 4 26
5 5 14
6 6 9
7 7 4
8 8 0
9 9 1
10 10 2
> str(szt)
'data.frame': 10 obs. of 2 variables:
$ size: Factor w/ 10 levels "1","2","3","4",..: 1 2 3 4 5 6 7 10 8 9
$ Freq: num 196 261 77 26 14 9 4 0 1 2
As noted above, I wish to see all ten factors along the X-axis. However ggplot drops the zero-count element.
Upvotes: 0
Views: 205
Reputation: 2022
@john welcome to SO. Please see here on how to create a minimum reproducible example. Try the following,
library(ggplot2)
df2<- data.frame(size = as.factor( c(1:10)),
freq=c(196,200,77,26,14,9,4,0,1,2)
)
df2
R> df2
size freq
1 1 196
2 2 200
3 3 77
4 4 26
5 5 14
6 6 9
7 7 4
8 8 0
9 9 1
10 10 2
ggplot(df2, aes(x = size, y=freq)) +
geom_bar(stat = "identity")
Upvotes: 0