19.5 Bar plots
- Customize:
- scale_x_discrete is used to handle x-axis title and labels
- coord_flip swaps the x and y axis
# Save the plot in the object "p"
pbar <- ggplot(data=df2, mapping=aes(x=grouping, fill=grouping)) +
geom_bar()
# Change x axis label with scale_x_discrete and change order of the bars:
p2 <- pbar + scale_x_discrete(name="counts of yes / no", limits=c("yes", "no"))
# Swapping x and y axis with coord_flip():
p3 <- p2 + coord_flip()
# Change fill
p4 <- p3 + scale_fill_manual(values=c("yellow", "cyan"))
# Show intermediary and final plots
pbar