A bar chart is the simplest chart that displays the frequency of a categorical variable or the summary statistics of a numeric variable over the category of other variables. In this recipe, we will learn how we can produce a bar chart using the ggplot2
library.
Let's call the ggplotdata
dataset that was created in the preceding section. We intend to produce a bar chart that will represent the mean of a numeric variable on the y axis over the category of the economic status variable on the x-axis. So, we need to prepare the summarized dataset as follows:
library(plyr) bardata <- ddply(ggplotdata,.(econ_status),summarize,meandisA=mean(disA), meandisB=mean(disB),meandisC=mean(disC),meadisD=mean(disD))
The description of the most important components of the preceding function is as follows:
ddply()
function takes the data frame as the input and produces the output in the data frame as well. In the function name, the first d
signifies the input data frame and the second d
signifies the output data frame..
) is being used so that we do not need to write the variable name within quotes.Now, let's call the ggplot2
library using the following line of code:
library(ggplot2)
The standard code that produces a bar chart is as follows:
ggplot(data= bardata, aes(x=econ_status, y=meandisA)) + geom_bar(stat="identity")
Each of the code's arguments is described as follows:
data
: This argument inputs the desired dataset.aes
: This is to specify the aesthetic of the plot, such as the values of the x and y axis. Beside specifying only the axes we can use other aesthetics that we will use later can be specified here, such as shape or color).geom_bar
: This is the actual function that produces the bar chart. There are some other arguments within geom_bar()
, such as stats="identity
", that refer to one-to-one mapping of the data points, that is, the value of y-axis should be displayed as it is, without any transformation.We can change the width of the bar and also change the orientation of the bar using some of the arguments with the primary function. An example of this is shown in the following code:
# to change bar width we could use width= argument within geom_bar() ggplot(data= bardata, aes(x=econ_status, y=meandisA)) + geom_bar(stat="identity",width=0.5) # change the layout to horizontal use coord_flip() # with the main plot ggplot(data= bardata, aes(x=econ_status, y=meandisA)) + geom_bar(stat="identity",width=0.5)+coord_flip()
The plot will look like what's shown in the following figure:
18.217.15.45