Rounded corner bar plot in R, we’ll show you how to use the ggchicklet package in the R programming language to make a ggplot2 bar chart with rounded bars.
The ggchicklet Package: An Overview
Bob Rudis’ ggchicklet package includes utilities for creating rounded rectangle segmented column charts (often known as “chicklets”).
Let’s dive into the topic.
Example 1: Barplot with Round Corners in R
We’ll teach you how to make rounded-corner barplots in R using the ggplot2 and ggchicklet package.
We must first create some data for this example.
df <- data.frame(value = 1:5, group = LETTERS[1:5]) df
value group 1 1 A 2 2 B 3 3 C 4 4 D 5 5 E
We constructed a data frame with a numeric and a character column by running the prior code.
The ggplot2 package must then be loaded.
We can now use the geom col method to create a conventional ggplot2 barplot, as illustrated below.
ggplot(data1, aes(group, value)) + geom_col()
The barchart depicted in Figure 1 was constructed after executing the previous R programming code.
The corners of the bars are not yet rounded, as you can see.
We must also install and load the ggchicklet package in order to produce a bar graph with round corners.
install.packages("ggchicklet", repos = "https://cinc.rud.is") library("ggchicklet")
We can now construct a barplot with round corners using the ggchicklet package’s geom chicklet function.
The radius argument in the geom chicklet function can be used to indicate how round the corners should be.
ggplot(data1, aes(group, value)) + geom_chicklet(radius = grid::unit(3, "mm"))
As you can see, we’ve made another ggplot2 barplot, but this time we’ve rounded the edges of the bars.
Example 2: Stacked ggplot2 Barplot with Round Corners
When developing stacked barplots, we think rounded corners seem particularly petty.
To demonstrate this, we must first construct a new data set.
df2 <- data.frame(value = 1:16, group = rep(LETTERS[1:4], each = 4), sub = letters[1:4]) df2
value group sub 1 1 A a 2 2 A b 3 3 A c 4 4 A d 5 5 B a 6 6 B b
The outcome of the previous code is shown above: A data frame with group and subgroup columns, as well as a values column.
The geom chicklet function can then be used to create a stacked barplot of these data. Note that the fill parameter is also specified within the aesthetics of our plot.
ggplot(df2, aes(group, value, fill = sub)) + geom_chicklet(radius = grid::unit(3, "mm"))