当前位置:网站首页>R language drawing | drawing mixed density function diagram and adding quantile line
R language drawing | drawing mixed density function diagram and adding quantile line
2022-04-21 07:08:00 【Zhuang Shanshan's R language manual】
brief introduction
There's a need recently , The following figures need to be drawn in the statistics class :

I mainly use ggridges In bag stat_density_ridges(). Introduction to this bag , Xiaobian has done an issue of content before , so : . Readers need to read this courseware further post , As well as some Case study .
Load package
library(ggplot2)
library(ggridges)
Generating data sets
Suppose the data comes from a mixed distribution .
item <- 10000
inds <- rbinom(1, item, 0.5)
x <- c(rnorm(inds, 1, 1), rnorm(item - inds, 8, 1))
data <- data.frame("value" = x, "class" = rep(1, length(x)))
Plot density function and add quantile lines
# mapping
p1 <- ggplot(data, aes(x = value, y = class, fill = factor(stat(quantile)))) +
stat_density_ridges(
geom = "density_ridges_gradient",
calc_ecdf = TRUE,
quantiles = c(0.025, 0.975)
) +
scale_fill_manual(
name = "Probability", values = c("#E2EAF6", "#436FB0", "#E2EAF6")
) +
theme_bw() +
theme(legend.position = "none", panel.grid = element_blank()) +
labs(x = "x", y = "Density")
p1

p2 <- ggplot(data, aes(x = value, y = class, fill = factor(stat(quantile)))) +
stat_density_ridges(
geom = "density_ridges_gradient",
calc_ecdf = TRUE,
quantiles = c(0.005, 0.495, 0.51, 0.99)
) +
scale_fill_manual(
name = "Probability", values = c("#E2EAF6", "#436FB0", "#E2EAF6", "#436FB0", "#E2EAF6"),
) +
theme_bw() +
theme(legend.position = "none", panel.grid = element_blank()) +
labs(x = "x", y = "Density")
p2

Merge two graphs
Use cowplot package , Merge the two figures . Xiaobian has made several issues of introduction to the package , so : . There are other ways to merge :.
library(cowplot)
# pdf("plot_cow.pdf", width = 8, height = 4)
plot_grid(p1, p2, ncol = 1, nrow = 2)
# dev.off()

recommend : You can save the following photos , stay b The station scans the QR code , perhaps b Station search 【 Zhuang Shanshan 】 watch Rmarkdown Series of video tutorials .Rmarkdown Two new videos ( Write wheel eye slide making ) Need documents in video , Reply in official account 【rmarkdown】
Visual tweet recommendation
R Visible | Spatial geographic data visualization (1)
R Visible | use R Tell the person you want
R Visible | Lollipop chart
R Visible | Merge multiple drawings
R Visible | Contour map
R Visible | Bubble chart
版权声明
本文为[Zhuang Shanshan's R language manual]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210605537366.html
边栏推荐
- Qt关于QMap容器释放内存笔记
- Text classification with Bert on colab
- First meet tensorflow 2 0 -- fashion MNIST identification
- I2C protocol (I): I2C bus spec
- Realization of character relationship visualization and question answering system in a dream of Red Mansions based on Knowledge Map
- ESP32 LVGL8.1 ——checkbox 复选框 (checkbox 23)
- Fundamentals of digital electronic technology 3.4 other types of MOS integrated circuits
- Simulation system of DC motor drive control based on STM32F103 chip
- 2、 5 [FPGA] simple combinational logic - Design decoder
- Lab server installation
猜你喜欢
随机推荐
Remember some commonly used r packages
Excel截取文本
CISSP认证每日知识点(2022年4月19日)
MySQL数据库备份命令--mysqldump
C语言模拟进栈出栈,先进先出先进后出共用内存
初识tensorflow2.0--Fashion MNIST识别
ESP32驱动编码器--SIQ-02FVS3 (Vscode + IDF)
matlab 数据归一化函数
Qt TableWidget插入QComboBox下拉框
每日CISSP认证常错题(2022年4月13日)
3、 5 switch level modeling of basic knowledge of Verilog HDL
基于 stm32f103 芯片的直流电机驱动控制仿真系统
每日CISSP认证常错题(2022年4月12日)
《数字电子技术基础》4.3 若干常用的组合逻辑电路 学习笔记
CSV document to TSV document - CSV to TSV
八位二进制乘法器VHDL
每日CISSP认证常错题(2022年4月14日)
Neo4j records in use
CISSP认证每日知识点(2022年4月12日)
CANopen开启PDO定时发送后心跳帧时间错误,PDO迟迟不发送,CANopen时间轴错乱









