ggplot2绘制直方图

作者: R语言数据分析指南 | 来源:发表于2021-05-09 10:40 被阅读0次

本文档主要回复群中小伙伴的一个问题,喜欢的小伙伴请细细品味,使用企鹅的数据集进行可视化

加载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)

相关文章

  • R语言可视化(四):频率直方图绘制

    04.直方图绘制 清除当前环境中的变量 设置工作目录 hist函数绘制频率直方图 ggplot2包绘制直方图 gg...

  • ggplot2绘制直方图

    本文档主要回复群中小伙伴的一个问题,喜欢的小伙伴请细细品味,使用企鹅的数据集进行可视化 加载R包 定义字体 自定义...

  • ggplot2之常见观察变量绘图geom

    一、直方图 之前在初学ggplot2包时,初步了解了ggplot_histgram()是针对一组连续型变量绘制频率...

  • 第4章 R作图

    直方图教程ggplot2作图hist

  • python--seaborn直方图

    seaborn是专门用于统计数据可视化的包,可媲美R语言中的ggplot2包。本文介绍用seaborn绘制直方图。...

  • bar

    matlab中函数bar绘制直方图中的应用函数bar(x)可以绘制直方图

  • 绘制直方图

    练习:绘制直方图

  • ggplot2绘制分裂小提琴图

    本节来介绍如何使用ggplot2绘制分裂小提琴图 加载R包 ggplot2绘制分裂小提琴图 ggplot2绘制云雨图

  • 50. 彩色直方图源码

    彩色直方图绘制步骤: 读取图片信息 各通道值计数与归一化 设置横纵坐标 绘制蓝绿红直方图 显示所有直方图 彩色直方...

  • 直方图

    一、 绘制直方图 1.1 代码 1.2 效果 二、全局直方图均衡 2.1 代码 1.2 效果 三、直方图匹配 3....

网友评论

    本文标题:ggplot2绘制直方图

    本文链接:https://www.haomeiwen.com/subject/epymdltx.html