• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

Coloring Barplots with ggplot2 in R

datavizpyr · January 6, 2020 ·

In this tutorial, we will see examples of making barplots and coloring the barplots in a few simple ways. We will see how to color barplots manually by specifying a color of interest and how to color a barplot by another variable in the dataset.

Let us load tidyverse suite of packages and set the theme with base size of the labels.

library(tidyverse)
theme_set(theme_bw(base_size=16))

In this tutorial to color barplots, we will use data set from 2019 Stack Overflow Developer survey. Here we use salary data for 5 educational group with a few more variables including gender and coding experience.

stackoverflow_file <- "https://raw.githubusercontent.com/datavizpyr/data/master/SO_data_2019/StackOverflow_survey_filtered_subsampled_2019.csv"

survey_results <- read_csv(stackoverflow_file)
## Parsed with column specification:
## cols(
##   CompTotal = col_double(),
##   Gender = col_character(),
##   Manager = col_character(),
##   YearsCode = col_character(),
##   Age1stCode = col_character(),
##   YearsCodePro = col_character(),
##   Education = col_character()
## )

In this post we will compare the effect of education on median salary. Let us compute median salary for each educational group.

median_salary <- survey_results %>%
  filter(!is.na(Education))%>%
  group_by(Education) %>%
  summarize(Salary =median(CompTotal))

We have five educational groups with their median salary.

median_salary
## # A tibble: 5 x 2
##   Education            Salary
##   <chr>                 <dbl>
## 1 Bachelor's           103000
## 2 Less than bachelor's 100000
## 3 Master's             120000
## 4 PhD                  134000
## 5 Professional         105000

Let us make a simple barplot with education on x-axis and median salary on y-axis. Let us reorder the bars by median salary and we can see an increase in salary with educational level.

median_salary %>%
  ggplot(aes(x=fct_reorder(Education, Salary), y=Salary)) +
  geom_col()+ 
  theme(axis.text.x = element_text(angle = 45, hjust = 1))+
  labs(x= "Education",
       y="Median Salary",
       subtitle="Effect of College Education\non Salary in US",
       caption="Data Source: StackOverflow Survey Results 2019")

Since the names of the education groups are longer, we place the x-axis labels at 45 degree to make it easy to read the labels.

Barplots with ggplot2 in R
Barplots with ggplot2 in R

By default, ggplot2 fills the bars in barplot in grey color. To change the fill color, we can manually specify the color of interest as argument to geom_col() function.

median_salary %>%
  ggplot(aes(x=fct_reorder(Education, Salary,), y=Salary)) +
  geom_col(fill="skyblue")+ 
  theme(axis.text.x = element_text(angle = 45, hjust = 1))+
  labs(x= "Education",
       y="Median Salary",
       subtitle="Effect of College Education\non Salary in US",
       caption="Data Source: StackOverflow Survey Results 2019")

In this example, we are filling the bars with skyblue color using fill=”skyblue”.

Manually coloring barplots with ggplot2
Manually coloring barplots with ggplot2

Sometime you may want to color the bars of a barplot by a variable. Let us color the barplot by Education level. Since we are filling the bars with color, we specify the fill argument with the variable of interest. In this case we use fill=Education within the aesthetics function of ggplot().

median_salary %>%
  ggplot(aes(x=fct_reorder(Education, Salary), y=Salary,
            fill=Education)) +
  geom_col()+ 
  theme(axis.text.x = element_text(angle = 45, hjust = 1))+
  labs(x= "Education",
       y="Median Salary",
       subtitle="Effect of College Education\non Salary in US",
       caption="Data Source: StackOverflow Survey Results 2019")

And this will color each bar in different color with a legend connecting the color to variable.

Coloring Barplots by a Variable with ggplot2
Coloring Barplots by a Variable with ggplot2

Related posts:

Stacked Barplots Side By Side with ggplot2 in RHow to Make Horizontal Stacked Barplots with ggplot2 in R? How to Make Barplots with Error bars in R?How To Make Barplots with Error bars in ggplot2? How To Highlight a Bar in Barplot in R?How To Highlight a Bar in Barplot in R? Customizing Labels on Bars in Side by side Stacked BarplotHow To Add Labels to Grouped Barplot with Bars Side-By-Side in R?

Filed Under: barplot ggplot2, color barplots in R, R Tagged With: barplot, ggplot2, R

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version