-
Book Overview & Buying
-
Table Of Contents
R Graph Essentials
Bar charts are useful for comparing the numbers of elements within subgroups of a population. However, they can be used for other purposes, such as comparing the means of a continuous variable across the levels of a categorical variable. You can create bar charts in ggplot using geom_bar(). As an exercise, create a bar chart of numbers of patients by ethnicity by turning the variable ETH into a factor by using factor(). The syntax is as follows:
W <- ggplot(T, aes(factor(ETH))) + geom_bar() W
The height of each bar gives the number of patients within each ethnicity. As an exercise, you can create a horizontal bar chart by adding the layer coord_flip(). The coord_flip() layer also works for other types of graph, including scatterplots and bar charts.
Now we insert our choice of color and border color using fill and color. Let's have an ivory color for the bars, along with dark green borders. The syntax is as follows:
W + geom_bar(fill="ivory", color="darkgreen")...
Change the font size
Change margin width
Change background colour