本文档主要回复群中小伙伴的一个问题,喜欢的小伙伴请细细品味,使用企鹅的数据集进行可视化
加载R包
library(palmerpenguins)
library(tidyverse)
library(patchwork)
library(ggtext)
library(showtext)
定义字体
font_add_google("Montserrat", "Montserrat")
showtext_auto()
自定义主题
theme_niwot <- function(){
theme_classic() +
theme(plot.title = element_text(size = 14, face = "bold", colour = "#2F4858", hjust = 0.5),
axis.title.x = element_blank(),
text = element_text(family = "Montserrat"),
axis.text.y = element_text(size = 12,colour = "#2F4858"),
axis.title.y = element_text(size = 14,face = "bold", colour = "#2F4858"),
panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank())
}
数据可视化
a <- ggplot() +
geom_histogram(data = filter(penguins, species == "Adelie"),
mapping = aes(y = bill_length_mm, x = ..count..),
binwidth = 1.5, colour = "white", fill = "#967AA1") +
geom_histogram(data = filter(penguins, species == "Gentoo"),
mapping = aes(y = bill_length_mm, x = -..count..),
binwidth = 1.5, colour = "white", fill = "#26407D") +
labs(y = "millimetres", title = "Bill Length")+
theme_niwot()+ geom_vline(xintercept = 0)
b <- ggplot() +
geom_histogram(data = filter(penguins, species == "Adelie"),
mapping = aes(y = body_mass_g, x = ..count..),
binwidth = 200, colour = "white", fill = "#967AA1") +
geom_histogram(data = filter(penguins, species == "Gentoo"),
mapping = aes(y = body_mass_g, x = -..count..),
binwidth = 200, colour = "white", fill = "#26407D") +
labs(y = "grams", title = "Body Mass")+
theme_niwot()+ geom_vline(xintercept = 0)
c <- ggplot() +
geom_histogram(data = filter(penguins, species == "Adelie"),
mapping = aes(y = flipper_length_mm, x = ..count..),
binwidth = 3, colour = "white", fill = "#967AA1") +
geom_histogram(data = filter(penguins, species == "Gentoo"),
mapping = aes(y = flipper_length_mm, x = -..count..),
binwidth = 3, colour = "white", fill = "#26407D") +
labs(y = "millimetres", title = "Flipper Length")+
theme_niwot() + geom_vline(xintercept = 0)
d <- ggplot() +
geom_histogram(data = filter(penguins, species == "Adelie"),
mapping = aes(y = bill_depth_mm, x = ..count..),
binwidth = 0.5, colour = "white", fill = "#967AA1") +
geom_histogram(data = filter(penguins, species == "Gentoo"),
mapping = aes(y = bill_depth_mm, x = -..count..),
binwidth = 0.5, colour = "white", fill = "#26407D") +
labs(y = "millimetres", title = "Bill Depth")+
theme_niwot()+ geom_vline(xintercept = 0)
(a + d)/(c + b)

网友评论