Add alt text, title, subtitle, and caption to the plot.
ggplot(candy) + aes(x = year, y = rating) + geom_col() + scale_x_continuous(breaks = 2019:2024) + scale_y_continuous(limits = c(0, 5), position = "right") + labs(alt = "Column chart of year and rating", title = "Candy ratings over the years", subtitle = "Ratings (1 - 5)", caption = "Source: The School of Data", x = NULL, y = NULL) + theme(plot.title.position = "plot")
Step 6: Apply a theme and format text
Use a minimal theme and customize text appearance.
ggplot(candy) + aes(x = year, y = rating) + geom_col() + scale_x_continuous(breaks = 2019:2024) + scale_y_continuous(limits = c(0, 5), position = "right") + labs(alt = "Column chart of year and rating", title = "Candy ratings over the years", subtitle = "Ratings (1 - 5)", caption = "Source: The School of Data", x = NULL, y = NULL) + theme_minimal() + theme(text = element_text(family = "PT Sans"), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), plot.title.position = "plot", plot.title = element_text(face = "bold", size = 16), plot.subtitle = element_text(face = "italic", 12), axis.text.y = element_text(size = 12))
Step 7: Add color
Customize the color of the columns and make final adjustments.
ggplot(candy) + aes(x = year, y = rating) + geom_col(fill = "rosybrown") + scale_x_continuous(breaks = 2019:2024) + scale_y_continuous(limits = c(0, 5), position = "right") + labs(alt = "Column chart of year and rating", title = "Candy ratings over the years", subtitle = "Ratings (1 - 5)", caption = "Source: The School of Data", x = NULL, y = NULL) + theme_minimal() + theme(text = element_text(family = "PT Sans"), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), plot.title.position = "plot", plot.caption.position = "plot", plot.title = element_text(face = "bold", size = 16), plot.subtitle = element_text(face = "italic", 12), axis.text = element_text(size = 12))
Getting started
We’ll use the candy dataset throughout this tutorial. Here’s how the dataset looks:
name
sales
price
rating
year
category
Jelly Beans
300
2.5
4.5
2019
Chewy
Gummy Bears
150
1.5
3.8
2020
Chewy
Lollipop
200
1
4
2021
Hard
Cotton Candy
100
2
4.2
2022
Soft
Jolly Ranchers
250
1.8
4.7
2023
Hard
Marshmallow
180
1.2
3.5
2024
Soft
To view the code to create the candy dataset, click the button below:
We’ll create a column chart that shows the rating of candy over the years.
Column chart of candy ratings over the years
Steps to create a column chart
Let’s go through the process of creating this column chart step by step.
Step 1: Start a ggplot and specify the data
ggplot(candy)
Step 2: Add aesthetics
ggplot(candy) + aes(x = year, y = rating)
Step 3: Add geometric objects
ggplot(candy) + aes(x = year, y = rating) + geom_col()
Exercise 3.1
Try running the code below to see a column chart with the ratings of candy over the years:
Exercise 3.2
Change the y-axis to sales to create a column chart showing sales over the years.
ggplot(candy) + aes(x = year, y = sales) + geom_col()
Step 4: Format axes
Now, let’s improve our column chart by formatting the axes, adding labels, and applying a theme.
We’ll use scale_x_continuous() and scale_y_continuous() to adjust the axes.
The breaks argument in scale_x_continuous() specifies the tick marks on the x-axis, while the limits argument in scale_y_continuous() sets the range of the y-axis.
The position argument in scale_y_continuous() moves the y-axis to the right side.
ggplot(candy) + aes(x = year, y = rating) + geom_col() + scale_x_continuous(breaks = 2019:2024) + scale_y_continuous(limits = c(0, 5), position = "right") + labs(alt = "Column chart of year and rating", title = "Candy ratings over the years", subtitle = "Ratings (1 - 5)", caption = "Source: The School of Data", x = NULL, y = NULL) + theme(plot.title.position = "plot")
Step 6: Apply a theme and format text
ggplot(candy) + aes(x = year, y = rating) + geom_col() + scale_x_continuous(breaks = 2019:2024) + scale_y_continuous(limits = c(0, 5), position = "right") + labs(alt = "Column chart of year and rating", title = "Candy ratings over the years", subtitle = "Ratings (1 - 5)", caption = "Source: The School of Data", x = NULL, y = NULL) + theme_minimal() + theme(text = element_text(family = "PT Sans"), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), plot.title.position = "plot", plot.title = element_text(face = "bold", size = 16), plot.subtitle = element_text(face = "italic", 12), axis.text.y = element_text(size = 12))
Step 7: Add color
Finally, let’s add color to our column chart.
ggplot(candy) + aes(x = year, y = rating) + geom_col(fill = "rosybrown") + scale_x_continuous(breaks = 2019:2024) + scale_y_continuous(limits = c(0, 5), position = "right") + labs(alt = "Column chart of year and rating", title = "Candy ratings over the years", subtitle = "Ratings (1 - 5)", caption = "Source: The School of Data", x = NULL, y = NULL) + theme_minimal() + theme(text = element_text(family = "PT Sans"), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), plot.title.position = "plot", plot.caption.position = "plot", plot.title = element_text(face = "bold", size = 16), plot.subtitle = element_text(face = "italic", 12), axis.text = element_text(size = 12))
Exercise 3.3
Let’s create a column chart showing the price of candy over the years. Use the code below as a starting point and make the following changes:
Change the y-axis to price
Update the title and subtitle to reflect the new data
Adjust the y-axis limits to fit the price data (hint: use limits = c(0, 3) )
Change the fill color to “skyblue”
ggplot(candy) + aes(x = year, y = price) + geom_col(fill = "skyblue") + scale_x_continuous(breaks = 2019:2024) + scale_y_continuous(limits = c(0, 3), position = "right") + labs(alt = "Column chart of year and price", title = "Candy prices over the years", subtitle = "Price in dollars", caption = "Source: The School of Data", x = NULL, y = NULL) + theme_minimal() + theme(text = element_text(family = "PT Sans"), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), plot.title.position = "plot", plot.caption.position = "plot", plot.title = element_text(face = "bold", size = 16), plot.subtitle = element_text(face = "italic", 12), axis.text = element_text(size = 12))
Quiz
Loading...
Loading...
Loading...
Review
In this tutorial, we learned how to create a column chart in R using the ggplot2 package. We covered the following steps:
Step 1: Start a ggplot and specify the data frame.
Step 2: Add aesthetics using the aes function.
Step 3: Add geometric objects using geom_col() .
Step 4: Format the axes using scale_x_continuous() and scale_y_continuous() .
Step 5: Add labels and titles using labs() .
Step 6: Apply a theme and format text using theme() .
Step 7: Add color to the chart using the fill argument in geom_col() .
In the next section, we’ll create a line graph in R. Let’s continue learning!