Creating bar charts

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.

Getting ready

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:

  • The 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.
  • In the variable name of the first argument, the dot (.) 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)

How to do it…

The standard code that produces a bar chart is as follows:

ggplot(data= bardata, aes(x=econ_status, y=meandisA)) + geom_bar(stat="identity")
How to do it…

How it works…

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.

There's more…

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:

There's more…

See also

To produce a bar chart for more than one numeric variable, follow the Creating multiple bar charts recipe; to produce a bar chart with error bars, refer to the Creating a bar chart with error bars recipe.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.118.180