does anyone know if it is possible to exclude zero values from a barplot in ggplot?
I have a dataset that contains proportions as follows:
X5employf prop X5employff
1 increase 0.02272727
2 increase 0.59090909 1
3 increase 0.02272727 1 and 8
4 increase 0.02272727 2
5 increase 0.34090909 3
6 increase 0.00000000 4
7 increase 0.00000000 5
8 increase 0.00000000 6
9 increase 0.00000000 6 and 7
10 increase 0.00000000 6 and 7
11 increase 0.00000000 7
12 increase 0.00000000 8
13 decrease 0.00000000
14 decrease 0.00000000 1
15 decrease 0.00000000 1 and 8
16 decrease 0.00000000 2
17 decrease 0.00000000 3
18 decrease 0.10000000 4
19 decrease 0.50000000 5
20 decrease 0.20000000 6
21 decrease 0.00000000 6 and 7
22 decrease 0.00000000 6 and 7
23 decrease 0.10000000 7
24 decrease 0.10000000 8
25 same 0.00000000
26 same 0.00000000 1
27 same 0.00000000 1 and 8
28 same 0.00000000 2
29 same 0.00000000 3
30 same 0.21052632 4
31 same 0.31578947 5
32 same 0.26315789 6
33 same 0.15789474 6 and 7
34 same 0.00000000 6 and 7
35 same 0.05263158 7
36 same 0.00000000 8
as you can see in the 'prop' column there are a lot of zero values. I am producing a facetted bar plot with 'X5employf' column as the facet. But because of the zero values I end up with a lot of empty space on my plot(see below). Is there a way of forcing ggplot to not plot the zero values? Its not the case of dropping unused factors as these are not NA values but 0s. Any ideas??!
For your plot, simply use which
to specify that you only want to use the subset of the dataframe containing non-zero proportions. This way you don't have to modify your original dataframe. Then, specify "free_x" in your scales
argument within facet_grid
to get rid of your empty space in your faceted plot.
plot <- ggplot(df[which(df$prop>0),], aes(X5employff, prop)) +
geom_bar(aes(fill=X5employff, stat="identity")) +
facet_grid( ~ X5employf, scales="free_x") +
theme_bw()
plot
Note that I replaced the blank fields with "blank" for the sake of quick import into R from Excel.
I'm unsure whether or not there is a way to set ignored values in ggplot
. However you could consider simply recoding 0's to NA:
df[df$prop == 0] <- NA
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With