R语言绘制密度图实战:使用ggplot2包和geom_density函数

R语言 专栏收录该内容
¥59.90 ¥99.00
90篇文章

R语言绘制密度图实战:使用ggplot2包和geom_density函数

在数据可视化中,密度图是一种常用的图表类型,用于展示连续变量的分布情况。R语言中的ggplot2包提供了丰富的绘图功能,其中的geom_density函数可以帮助我们轻松地绘制出美观的密度图。本文将介绍如何使用ggplot2包中的geom_density函数来绘制密度图,并通过实例演示其应用。

首先,我们需要安装和加载ggplot2包,这可以通过以下代码实现:

install.packages("ggplot2")  # 安装ggplot2包
library(ggplot2)  # 加载ggplot2包

接下来,我们准备一组示例数据,用于绘制密度图。假设我们有一个数据框df,其中包含了某种药物的治疗效果评分数据:

# 创建示例数据
set.seed(123)
df <- data.frame(score = rnorm(100, mean = 70, sd = 10))

以上代码使用了rnorm函数生成了一个平均值为70,标准差为10的符合正态分布的随机样本,共100个观测值。现在我们可以开始绘制密度图了。

# 绘制密度图
ggplot(df, aes(x = score)) + 
  geom_density()

运行以上代码后,我们将得到一个简单的密度图,横轴表示得分,纵轴表示密度值。通过这张图

ggplot2之Layer—— geoms(二) 上次谈到了layer中跟geoms_有关的参考线、条形图、2d bin计数的热图、空图、盒式图。这次谈一些其他的图形及参数。1. geom_contour 、stat_contour —— 等高线2. geom_count、 stat_sum—— 计数重叠点3. geom_density_2d 、stat_density_—— 2d二维密度估计的等高线4. geom_density 、stat_de 阅读详情

相关推荐

ggplot2参数简介

ggplot(faithfuld, aes(waiting, eruptions, fill = density)) + geom_tile() + scale_fill_gradient(low = "blue", high = "red") + guides(fill = guide_colorbar(title = "密度", barwidth = 20))scale_shape() 和 scale_shape_manual():用于设置点的形状,可以选择圆形、三角形、正方形等多种形状。

weixin_44619692的博客 3080

R语言使用ggplot2包使用geom_density()函数绘制基础密度图实战(density plot)

R语言使用ggplot2包使用geom_density()函数绘制基础密度图实战(density plot) 目录 R语言使用ggplot2包使用geom_density()函数绘制基础密度图实战(density plot) #密度图 #仿真数据 #geom_density()函数绘制基础密度图 #密度图 与直方图(histogram)相关的一种图表类型是密度图,它是通过计算“可能会产生观测数据的连续概率分布的估计”而产生。一般的过程是将该分布近似为一组核(即诸如正态(高斯)分布之

data+scenario+science+insight 2305

R语言使用ggplot2包使用geom_density()函数绘制分组密度图(线条色彩、添加均值线)实战(density plot)

R语言使用ggplot2包使用geom_density()函数绘制分组密度图(线条色彩、添加均值线)实战(density plot) 目录 R语言使用ggplot2包使用geom_density()函数绘制分组密度图(线条色彩、添加均值线)实战(density plot) #密度图 #仿真数据 #geom_density()函数绘制分组密度图(线条色彩、添加均值线) #密度图 与直方图(histogram)相关的一种图表类型是密度图,它是通过计算“可能会产生观测数据的连续概率分布的估计

data+scenario+science+insight 2032

R语言使用ggplot2包使用geom_density()函数绘制密度图(连续色彩、离散色彩、梯度色彩)实战(density plot)

R语言使用ggplot2包使用geom_density()函数绘制密度图(连续色彩、离散色彩、梯度色彩)实战(density plot) 目录 R语言使用ggplot2包使用geom_density()函数绘制密度图(连续色彩、离散色彩、梯度色彩)实战(density plot) #密度图 #仿真数据 #geom_density()函数绘制密度图(连续色彩、离散色彩、梯度色彩) #密度图 与直方图(histogram)相关的一种图表类型是密度图,它是通过计算“可能会产生观测数据的连续概

data+scenario+science+insight 1526

绘制分组密度图的R语言教程使用ggplot2包和geom_density函数

在上述示例中,我们创建了一个名为"data"的数据框,其中包含了一个名为"group"的分组变量和一个名为"value"的数值变量。通过aes函数,我们将"value"变量设置为x轴,并使用"group"变量来给不同组别的密度曲线着色。希望通过本教程,你能够掌握使用ggplot2包和geom_density函数绘制分组密度图的基本方法,并能够根据自己的数据定制和美化图表。然后,我们使用labs函数设置图表的标题为"分组密度图",x轴标签为"数值变量",y轴标签为"密度"。

DevSavantX的博客 404

R语言使用ggplot2包使用geom_density()函数绘制密度图(填充色、线性设置)实战(density plot)

R语言使用ggplot2包使用geom_density()函数绘制密度图(填充色、线性设置)实战(density plot) 目录 R语言使用ggplot2包使用geom_density()函数绘制密度图(填充色、线性设置)实战(density plot) #密度图 #仿真数据 #geom_density()函数绘制密度图(填充色、线性设置) #密度图 与直方图(histogram)相关的一种图表类型是密度图,它是通过计算“可能会产生观测数据的连续概率分布的估计”而产生。一般的过程是将

data+scenario+science+insight 1698

R语言使用ggplot2包使用geom_density()函数绘制分组密度图(分组拆分画板基于facet)实战(density plot)

R语言使用ggplot2包使用geom_density()函数绘制分组密度图(分组拆分画板基于facet)实战(density plot) 目录 R语言使用ggplot2包使用geom_density()函数绘制分组密度图(分组拆分画板基于facet)实战(density plot) #密度图 #仿真数据 #geom_density()函数绘制分组密度图(分组拆分画板基于facet) #密度图 与直方图(histogram)相关的一种图表类型是密度图,它是通过计算“可能会产生观测数据的

data+scenario+science+insight 1468

R语言使用ggplot2包使用geom_density()函数绘制分组密度图(改变图例位置、移除图例)实战(density plot)

R语言使用ggplot2包使用geom_density()函数绘制分组密度图(改变图例位置、移除图例)实战(density plot) 目录 R语言使用ggplot2包使用geom_density()函数绘制分组密度图(改变图例位置、移除图例)实战(density plot) #密度图 #仿真数据 #geom_density()函数绘制分组密度图(改变图例位置、移除图例) #密度图 与直方图(histogram)相关的一种图表类型是密度图,它是通过计算“可能会产生观测数据的连续概率分布

data+scenario+science+insight 1358

R语言使用ggplot2包使用geom_density()函数绘制密度图(自定义颜色填充、线条色彩、分组、均值线)实战(density plot)

R语言使用ggplot2包使用geom_density()函数绘制密度图(自定义颜色填充、线条色彩、分组、均值线)实战(density plot) 目录 R语言使用ggplot2包使用geom_density()函数绘制密度图(自定义颜色填充、线条色彩、分组、均值线)实战(density plot) #密度图 #仿真数据 #geom_density()函数绘制密度图(自定义颜色填充、线条色彩、分组、均值线) #密度图 与直方图(histogram)相关的一种图表类型是密度图,它是通过计

data+scenario+science+insight 1521

R语言使用ggplot2包使用geom_density()函数绘制分组密度图(添加直方图、分组颜色配置)实战(density plot)

R语言使用ggplot2包使用geom_density()函数绘制分组密度图(添加直方图、分组颜色配置)实战(density plot) 目录 R语言使用ggplot2包使用geom_density()函数绘制分组密度图(添加直方图、分组颜色配置)实战(density plot) #密度图 #仿真数据 #geom_density()函数绘制分组密度图(添加直方图、分组颜色配置) #密度图 与直方图(histogram)相关的一种图表类型是密度图,它是通过计算“可能会产生观测数据的连续概

data+scenario+science+insight 1281

R语言使用ggplot2包使用geom_density()函数绘制分组密度图(自定义调色板、brewer、灰度比例)实战(density plot)

R语言使用ggplot2包使用geom_density()函数绘制分组密度图(自定义调色板、brewer、灰度比例)实战(density plot) 目录 R语言使用ggplot2包使用geom_density()函数绘制分组密度图(自定义调色板、brewer、灰度比例)实战(density plot) #密度图 #仿真数据 #geom_density()函数绘制分组密度图(自定义调色板、brewer、灰度比例) #密度图 与直方图(histogram)相关的一种图表类型是密度图,它是

data+scenario+science+insight 1117

R语言使用ggplot2包使用geom_density()函数绘制分组密度图(填充色配置、半透明填充色、添加均值线)实战(density plot)

R语言使用ggplot2包使用geom_density()函数绘制分组密度图(填充色配置、半透明填充色、添加均值线)实战(density plot) 目录 R语言使用ggplot2包使用geom_density()函数绘制分组密度图(填充色配置、半透明填充色、添加均值线)实战(density plot) #密度图 #仿真数据 #geom_density()函数绘制分组密度图(填充色配置、半透明填充色、添加均值线) #密度图 与直方图(histogram)相关的一种图表类型是密度图,它是

data+scenario+science+insight 1484

R语言使用ggplot2包使用geom_density()函数绘制密度图(自定义颜色填充、线条色彩、分组、均值线)实战

R语言使用ggplot2包使用geom_density()函数绘制密度图(自定义颜色填充、线条色彩、分组、均值线)实战

statistics+insight+vista+power 525

R语言使用ggplot2包使用geom_density()函数绘制密度图(连续色彩、离散色彩、梯度色彩)实战

R语言使用ggplot2包使用geom_density()函数绘制密度图(连续色彩、离散色彩、梯度色彩)实战

statistics+insight+vista+power 486

北京联通IPTV组播实战:OpenWRT软路由配置全解析

本文详细解析了在北京联通IPTV环境下,利用OpenWRT软路由和udpxy工具实现组播转单播的实战配置。内容涵盖网络接口设置、防火墙规则、udpxy安装配置、直播源M3U文件处理以及全平台播放指南,帮助用户无需机顶盒即可在家庭局域网内任何设备上流畅观看电视直播。

weixin_29313547的博客 1437

深入解析Panfrost驱动:Mali-GPU开源驱动的架构与实现

本文深入解析了Panfrost驱动作为Mali-GPU开源驱动的架构与实现。从诞生背景到硬件架构演进,详细介绍了Panfrost如何突破ARM闭源策略,实现全栈支持。文章还探讨了驱动设计中的关键技术挑战与性能优化策略,为开发者提供了实用的编译部署指南和性能调优方法。

weixin_30790841的博客 557

TVS 和压敏电阻联合使用进行浪涌保护时,压敏电阻的压敏电压要低于TVS 的钳位电压 VC。

通过先让MOV接管,可以确保在大多数情况下,电压不会迅速上升到TVS二极管的钳位电压,从而保护TVS二极管免受持续高能量冲击的损害。总结来说,将MOV的压敏电压设置得低于TVS二极管的钳位电压,可以提供一种分级的保护机制,优化浪涌保护性能,同时保护敏感的TVS二极管不被过度的能量冲击损坏,并延长其使用寿命。MOV吸收的能量(Wmov):假设浪涌电流为10kA,MOV导通后将电压限制在300V,那么Wmov = Ipp * Vmov = 10kA * 300V = 3MW。例如,Vmov可以设定为275V。

weixin_66642980的博客 2255

三. zemax光学设计流程+案例

文章目录zemax 基本操作:1. 建立光学基本模型2. 像质评价3. 优化(重点)4. 公差分析简单镜头设计 zemax 基本操作: 1. 建立光学基本模型 2. 像质评价 3. 优化(重点) 对系统设定的约束条件或目标值 统称为操作数。 操作数: 光学特性参数(焦距EFFL,近轴放大率PMAG,入瞳位置EMPP) 像差参数(球差SPHA,彗差COMA,像散ASTI) 边界条件(中心厚度值CTVA,边缘厚度值ETAV) 优化的主要步骤: 设置评价函数和优化操作数 一般小像差系统采用波像差 构建

Taiiiii的博客 8070

dify工作流的原理

摘要: Dify 工作流是一个基于有向无环图(DAG)的智能任务编排系统,支持多节点(如LLM调用、API工具、条件分支等)的可视化拖拽与流程编排。其核心原理包括执行引擎(解析DAG并管理节点依赖)、上下文变量传递(类Jinja模板的变量引用机制)和状态持久化(存储执行日志与中间结果)。工作流通过事件驱动执行,支持异步任务调度,并与RAG知识库、LLM服务等模块深度集成,最终可发布为API或对话应用。技术栈基于Python(FastAPI+Celery)和PostgreSQL,实现低代码化的AI应用开发。

nbspzs的专栏 2196

保姆级教程:用MediaPipe和BlazePose在Python里实时追踪你的健身动作(附完整代码)

本文提供了一份详细的Python教程,教你如何使用MediaPipe和BlazePose实现实时健身动作追踪。从环境配置、模型选型到实时视频流处理架构设计,再到健身动作量化分析和可视化反馈系统优化,教程涵盖了完整的实现步骤和代码示例,帮助开发者快速构建高效的姿态特征点检测系统。

weixin_42531779的博客 156

多线程开发的基本概念

在Android开发中,我们常常会遇到多线程开发的需求。由于Android中的,我们不能在主线程处理耗时逻辑,否则会导致卡顿,甚至出现ANR(应用无响应)等异常情况。因此,我们需要将网络请求等耗时操作放到中去执行,完成后再更新UI。多线程开发因此成为Android开发中的一个重要部分。随着Android开发技术的不断进步,从早期的使用Handler,到后来的RxJava,再到如今的Kotlin协程,切换线程以及并发编程的代码写起来是越来越简洁了。

喻志强的博客 1029

一分钟了解OpenSpec (规格)

OpenSpec是由Fission AI开源的轻量级规范驱动开发(SDD, Spec-Driven Development)CLI工具。它专为AI编程助手(如Cursor、Claude Code)设计,核心是**“先约定,后编码”:在AI写代码前,先通过结构化规范文档达成共识,让开发过程可预测、可审计、可协作**。轻量无侵入:单CLI、无依赖、不改造项目、1分钟接入存量友好:增量Spec、不重构旧文档、1→n王者完全可控:每步审查、AI按图施工、零返工协作利器。

h a 6 6 6 c k 为 你解决复杂难题! 2902

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

上一篇: 使用H2O包在R语言中构建深度学习模型
下一篇: 使用R语言为类型数据添加图像符号标签
普通网友
博客等级 码龄3年 200粉丝 630原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址