0

I would like to change the font type for my plot title and also the data label to make my plot a little less "dull". I have tried font_import() alongside family="Comic Sans MS" (amongst other failed efforts) but nothing seems to alter the font type. I have researched other similar question responses but yet to figure out where I am going wrong. Below is my code:

library(ggplot2)
library(tidyverse)
library(dplyr)
library(extrafont)

font_import()

mydf <- data.frame( Category = c("Metric 1", 
                                 "Metric 2"),
                    Dec_21=c(31455, 1465),
                    Jan_22=c(44480, 1827),
                    Feb_22=c(58929, 2174))

mydf %>%
  gather(Month, Total, -Category) %>%
  mutate(Month = reorder(Month, row_number())) %>%
  mutate(Category = reorder(Category, row_number())) %>%
  ggplot(aes(Month, Total, fill = Category, group = Category)) +
  geom_text(aes(label=Total), position=position_dodge(width=0.9), vjust=-0.25) +
  scale_fill_manual(values = c("dark green", "red")) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(x = "", y = "", title = "Title of Plot", subtitle = "Subtitle of Plot") +
  theme_bw() +
  theme(legend.position = "bottom",
        legend.title = element_blank(),
        legend.key.size = unit(0.5, 'cm'),
        legend.text = element_text(size=7),
        panel.grid.major.x = element_blank(),
        panel.border = element_blank(),
        plot.title = element_text(hjust = 0),
        plot.subtitle = element_text(size=8, hjust=0, face="italic", color="black"),
        axis.text.x = element_text(size = 10, face = "bold", color="black"))
Bren85
  • 63
  • 6
  • for example, if you want to change the x axis font, you should add ```family = "Comic Sans MS"```into ```element_text``` in ```axis.text.x```. It is same for y axis or ```legend.text```. It is also similar for title and subtitles. – patula Mar 23 '22 at 14:23
  • When I edit the code to `plot.title = element_text(family = "Comic Sans MS", hjust = 0)` nothing changes. – Bren85 Mar 23 '22 at 14:46

1 Answers1

0

You can run in the theme command, text = element_text(family = "Comic Sans MS") to change the font. You can run this code:

 mydf %>%
   gather(Month, Total, -Category) %>%
   mutate(Month = reorder(Month, row_number())) %>%
   mutate(Category = reorder(Category, row_number())) %>%
   ggplot(aes(Month, Total, fill = Category, group = Category)) +
   geom_text(aes(label=Total), position=position_dodge(width=0.9), vjust=-0.25) +
   scale_fill_manual(values = c("dark green", "red")) +
   geom_bar(stat = "identity", position = "dodge") +
   labs(x = "", y = "", title = "Title of Plot", subtitle = "Subtitle of Plot") +
   theme_bw() +
   theme(legend.position = "bottom",
         legend.title = element_blank(),
         legend.key.size = unit(0.5, 'cm'),
         legend.text = element_text(size=7),
         panel.grid.major.x = element_blank(),
         panel.border = element_blank(),
         plot.title = element_text(hjust = 0),
         plot.subtitle = element_text(size=8, hjust=0, face="italic", color="black"),
         axis.text.x = element_text(size = 10, face = "bold", color="black")) +
   theme(text = element_text(family = "Comic Sans MS"))

Output:

enter image description here

Quinten
  • 35,235
  • 5
  • 20
  • 53
  • Thank you! Initially the code addition didn't work but after a R reboot the fonts have changed. – Bren85 Mar 24 '22 at 09:45