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"))
CodePudding user response:
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: