Book Image

R Graph Essentials

Book Image

R Graph Essentials

Overview of this book

This book is targeted at R programmers who want to learn the graphing capabilities of R. This book will presume that you have working knowledge of R.
Table of Contents (6 chapters)
5
Index

Creating bar charts


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")...