Bar charts

Author

Trung Dang

A bar chart is a common and effective way to visualize categorical data. It represents each category as a bar, with the length or height of the bar proportional to the value it represents. Bar charts are particularly useful for comparing quantities across different groups, highlighting trends, or spotting outliers.

Horizontal bar charts, where bars extend sideways — often used when category names are long

Bar charts are easy to read and interpret, making them ideal for communicating data clearly to a wide audience. They are widely used in fields like business, public health, education, and social sciences

#Preparation

Loading packages

Setting up fonts

Checking fonts

# A tibble: 5 × 6
  path             file        family            face        version     ps_name
  <chr>            <chr>       <chr>             <chr>       <chr>       <chr>  
1 C:/Windows/Fonts pala.ttf    Palatino Linotype Regular     Version 5.… Palati…
2 C:/Windows/Fonts palab.ttf   Palatino Linotype Bold        Version 5.… Palati…
3 C:/Windows/Fonts palabi.ttf  Palatino Linotype Bold Italic Version 5.… Palati…
4 C:/Windows/Fonts palai.ttf   Palatino Linotype Italic      Version 5.… Palati…
5 C:/Windows/Fonts PALSCRI.TTF Palace Script MT  Regular     Version 1.… Palace…
# A tibble: 0 × 6
# ℹ 6 variables: path <chr>, file <chr>, family <chr>, face <chr>,
#   version <chr>, ps_name <chr>

First example of chart

Second example of chart

pic1 <- ggplot(data = hepc, 
       aes(x= reorder(province, rate), 
           y = rate,
           fill = color)) +
  geom_col(width =  0.8) +
  scale_fill_identity(guide = "none") +
  labs(title = "Rate of hep C in Ontario just above the national average (31.7)\nFour provinces below the national average",
       subtitle = "Rates of reported hepatitis C in Canada, 2017",
       y = "",
       caption = "Note: Rate per 100,000 people \nNunavut did not report on hepatitis C in 2017\nSource: Canadian Notifiable Diseases Surveillance System (CNDSS)") +
  scale_y_continuous(expand =  c(0,0),
                     limits = c(0,65)) +
  geom_text(aes(label = rate, vjust = 0.5, hjust = 1.25), 
            color = "#404040", size = 5,
            family = "Calibri") +
  
  geom_text(data = hepc %>% 
              filter(province %in% c("Nova Scotia", "Ontario","Prince Edward Island",  "Northwest Territories", "Quebec")),
    aes(label = rate, vjust = 0.5, hjust = 1.25), 
    color = "#ffffff",size = 5,family = "Calibri") +
  coord_flip(clip= "off") +
  theme(
    plot.title = element_text(size = 24, 
                              color = "black", family = "Calibri-Bold",
                              margin = margin(0,0,15,0)),
    plot.title.position = "plot",
    plot.subtitle = element_text(size = 16,color = "black",
                                 margin = margin(0,0,15,0)),
    plot.caption = element_text(color = "#6d6d6d", size = 10, 
                                hjust = 0, 
                                margin = margin(t = 25, r = 0, b = 0, l = 0)),
    plot.caption.position =  "plot",
    axis.title = element_blank(),
    axis.text.x = element_blank(),
    axis.text.y = element_text(size = 13.5, color = "#404040", hjust = 1),
    axis.line = element_blank(),
    axis.ticks.x = element_blank(),
    axis.ticks.y = element_blank(),
    panel.grid.major.x = element_blank(),
    panel.background = element_blank(),
    plot.margin = margin(t = 10, r = 0,  b = 10, l = 10))
pic1

ggsave(plot = pic1, "Second example of bar gragh.svg", height= 7, width = 10)

The following picture is from SVG

The following picture is set at dpi = 96