O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating bar charts with ggplot2

First, we will visualize the total number of bicycle rentals per station by means of a bar chart. For this, we need to make a small transformation to the data with the dplyr package and then we will use the ggplot2 package to generate the plot:

  1. Create a data frame to summarize and transform the station, workingday, and cnt variables with dplyr:
> bike.sum =bike %>% group_by(season, workingday) %>% summarize(rental = sum(cnt))
  1. Plot the bar graph for each station:
> ggplot(bike.sum, aes(x= season, y= rental)) + geom_bar(show.legend = TRUE, stat = "identity") + labs(title = "Rentals for Season and Day")

The following diagram is the ggplot:

  1. Now plot a bar graph by each station segmented by the workingday ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required