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 2301

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 2027

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 1518

绘制分组密度图的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 1462

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 1513

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 1278

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 1114

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 1482

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

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

statistics+insight+vista+power 521

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

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

statistics+insight+vista+power 484

ENVI5.6 自动化流程:GF系列卫星影像批处理与质量优化

本文详细介绍了如何利用ENVI5.6构建针对GF-2、GF-6、GF-7系列卫星影像的自动化批处理流水线。通过配置Batch Processing工具,实现从正射校正到图像融合的全流程批量处理,并分享参数设置、质量监控与故障排查的实战经验,旨在显著提升海量遥感影像的处理效率与成果质量。

efc1234567的博客 603

经典脉冲激光测距原理&雷达方程

脉冲激光测距机的工作过程如下:首先用瞄准光学系统瞄准目标,然后接通激光电源,储能电容器充电,产生触发闪光灯的触发脉冲,闪光灯点亮,激光器受激辐射,从输出反射镜发射出一个前沿陡峭、峰值功率高的激光脉冲,通过发射光学系统压缩光束发射角后射向目标。通过计算激光发射功率经介质传输的衰减,目标表面截获和反射的光功率,到达接收视场的光功率,以及接收光学系统接收到的光功率,就可以得到到达光电探测器的光功率,即接收功率。⑤光東路程上的折射率变化;),测量光脉冲到达目标并由目标返回到接收机的时间,由此计算出目标的距离。

weixin_46153372的博客 2524

Marin说PCB之LPDDR5的布线注意事项--03

这个规则中是把每个字节中的WCK to RDQS 以及DM / DQ to WCK等长规则都加到了DM / DQ这个等长规则中去了,因为芯片手册上的设计要求中的DM / DQ这个group的等长误差是+-50MIL, WCK to RDQS的等长group的规则是62.5MIL,DM / DQ to WCK等长group的规则是相对松一些是150MIL。其实上面的等长规则还是建少了一些的,例如手册上要求的每个channel的WCK0:1,WCK2:3等长误差在+-488MIL,这个规则建议还是补上去吧。

weixin_45223454的博客 1229

保姆级教程:在S32K312上配置C40驱动,实现Flash读写(基于RTD 4.4.2)

本文详细介绍了在S32K312微控制器上配置C40驱动实现Flash读写的全流程,基于RTD 4.4.2库。内容涵盖环境搭建、外设配置、Flash操作实战代码及高级应用场景,特别针对LLD驱动和FLS存储操作中的常见问题提供了解决方案,帮助开发者快速掌握嵌入式Flash存储技术。

weixin_28684757的博客 255

MounRiver Studio实战:像管理个人项目一样整理沁恒MCU的EVT例程(附文件结构图)

本文详细介绍了如何使用MounRiver Studio重构沁恒MCU的EVT例程,实现工程化管理。通过分析官方EVT设计的局限性,提出标准化的目录结构和迁移步骤,帮助开发者提升代码可维护性和团队协作效率。文章还提供了MounRiver Studio的关键配置参数和常见问题排查指南,适用于沁恒MCU开发者。

weixin_33940469的博客 356

卸载重装Ubuntu22.04双系统

先说一下写此篇文章的背景吧,我在Windows下配置了三个cornerNet深度学习环境分别是cuda10.3(显卡版本太高不支持),cuda11.3(配置后偶然看到一篇说这个版本win下只能用CPU跑),最后是cuda11.1终究还是没能跑通代码,因为它的代码是在Liunx系统写的,就想着装个Ubuntu试试看,在网上看说一般软件不要装太高版本,我就下载了470版的显卡驱动,跟着步骤升级了系统内核,结果第二天开机Ubuntu就崩了,开机黑屏左上角只有一道横杠显示,只能按ctrl+shift+f5进入命令窗

编程小白的博客 8108

ArcGIS与地理加权回归GWR【一】

因此,在实际问题研究中我们经常发现回归参数在不同地理位置上往往表现为不同,也就是说回归参数随地理位置变化,这时如果仍然采用全局空间回归模型(全局假设β是不会变得),得到的回归参数估计将是回归参数在整个研究区域内的平均值,如上所说某一个因素在ABC等不同的地理位置对房价的影响是不同的,假如采用全局空间回归模型,便忽略了数据的空间位置属性,只反映平均意义下因变量与自变量的相关关系,而不能有效反映回归关系的空间非平稳性的特征。当自变量为1个时,是一元线性回归,自变量为2个及以上时,称为多元线性回归。

JData_Engineer的博客 1374

ardupilot 获取避障传感器数据

本节主要记录自己学ardupilot的避障代码过程,首先看下怎么注册避障传感器,然后怎么获取避障数据。

陌城烟雨 5524

深度学习工具包 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")

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

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

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值