使用 R 和 ggraph 自定义树状图

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

使用 R 和 ggraph 自定义树状图

树状图是一种常用的数据可视化方法,它可以以层次结构展示数据之间的关系。在 R 语言中,可以使用 ggraph 包来创建高度可定制的树状图。本文将介绍如何使用 R 和 ggraph 包来自定义树状图,并提供相应的源代码示例。

首先,我们需要安装和加载 ggraph 包。可以使用以下代码在 R 中安装 ggraph 包:

install.packages("ggraph")

加载 ggraph 包:

library(ggraph)

接下来,我们需要准备一个数据集来创建树状图。假设我们有一个表示组织结构的数据集,其中包含部门和员工之间的关系。以下是一个简单的示例数据集:

# 创建示例数据集
data <- data.frame(
  员工 = c("张三", "李四", "王五", "赵六", "钱七"),
  部门 = c("销售部", "销售部", "财务部", "人力资源部", "人力资源部"),
  上级 = c(NA, "张三", "张三", "李四", "李四")
)

在这个数据集中,“员工” 列包含员工的姓名,“部门” 列包含员工所属的部门,“上级” 列表示员工的直接上级。我们将使用这个数据集来创建树状图。

接下来,我们使用 ggraph 包中的 ggraph() 函数创建一个图形对象,并使用

R 数据可视化 —— ggraph 边与节点 将网络图结构与我们平常绘制的数据图进行对比,我们可以发现,图的布局就相当于每个数据点的坐标,而节点就相当于数据点,边就是连接数据点的几何图形,可以是直线、曲线或带箭头的线等。所以,不同的节点形状和不同的边形状进行组合,就绘制出了上节我们展示的各式各样的图形。下面,我们要介绍节点与边的相关操作,图结构如下。 阅读详情

相关推荐

R 数据可视化进阶 —— ggraph 布局算法全解析与实战应用

本文深入解析R语言中ggraph包的布局算法,涵盖力导向、层次化、蜂巢图等五大核心布局技术,并提供生物网络、社交网络等领域的实战应用案例。通过详细的代码示例和性能优化技巧,帮助数据科学家掌握高级数据可视化方法,提升复杂网络的分析效率与展示效果。

weixin_29074295的博客 244

ggraph绘制基因互作环形网络图:从STRING数据到可视化实战

本文详细介绍了如何使用R语言的ggraph包,从STRING数据库的蛋白互作数据出发,绘制专业美观的基因互作环形网络图。教程涵盖数据预处理、图对象构建、核心绘图步骤及进阶技巧,重点演示了如何利用ggnewscale包在同一图中映射节点填充色(通路分组)和边框色(上下调状态),实现发表级可视化效果。

hadoop5ranger的博客 757

R 数据可视化 —— igraph 创建图

接下去的部分,我们要开始介绍层次关系与网络关系图片的绘制。层次关系型图标主要用于表达个体之间的层次关系,通常可以用来展示分类的多级关系,例如层次聚类的树状图、节点连接图、冰柱图、旭日图、圆填充图、矩形树状图等不同于层次关系结构,网络关系结构并不是自底向上或自顶向下的层次结构,展示了不同元素之间的更加自由和复杂的相互关系。如桑葚图、和弦图、弧长链接图、蜂箱图、边绑定图等。在图论中,图是由若干个节点或顶点(node或vertex)和连接两个节点的边(edge。

dxs18459111694的博客 3360

使用R语言绘制网络图的优雅方式:ggraph包

在R语言中,ggraph包是一个强大且优雅的工具,可用于创建漂亮的网络图。在这个示例中,我们使用的是force-directed布局,它会根据节点之间的连接关系自动调整节点的位置。假设我们有一个包含节点和边信息的数据框,节点信息存储在一个名为"nodes"的数据框中,边信息存储在一个名为"edges"的数据框中。通过简单的几行代码,我们可以创建具有吸引力和可读性的网络图。通过上述步骤,我们可以使用ggraph包创建一个简单的网络图。函数创建一个图形对象,并使用布局对象来设置节点和边的位置。

TechPulseZ的博客 1357

从零开始:如何在Windows上搭建Airsim仿真环境(避坑指南)

本文是一份详细的Windows平台AirSim仿真环境搭建指南。文章从硬件要求、Visual Studio与虚幻引擎安装开始,逐步讲解AirSim插件编译、UE项目配置,并重点介绍了通过WSL2连接PX4飞控软件实现软件在环仿真的完整流程。内容涵盖常见问题排查与性能优化,旨在帮助开发者避开常见陷阱,高效构建可用于无人机算法开发的虚拟测试平台。

earth的博客 311

TwinCAT3新手必看:5分钟搞定EtherCAT从站PDO动态配置(附电流变量添加技巧)

本文为TwinCAT3新手提供了EtherCAT从站PDO动态配置的快速入门指南。文章详细解析了Process Data Object(PDO)在实时数据交换中的核心作用,并分步演示了在TwinCAT3工程中扫描从站、动态添加与映射PDO(包括接收PDO和发送PDO)的完整操作流程。重点分享了如何灵活添加并监控如电机实际电流等关键工艺变量的高级技巧,并针对不同品牌驱动器的兼容性问题提供了实用的调试心法和解决方案,帮助工程师快速上手并优化工业自动化系统。

xgboost6farmer的博客 460

WPF基础系列六:MVVM框架

MVVM框架 文章目录MVVM框架前言一、MVVM框架简介1. 什么是MVVM?2. MVVM的优势3.MVVM的应用场景二、MVVM使用及示例 前言 在WPF开发中,经典的编程模式是MVVM,是为WPF量身定做的模式,该模式充分利用了WPF的数据绑定机制,最大限度地降低了Xmal文件和CS文件的耦合度,也就是UI显示和逻辑代码的耦合度,如需要更换界面时,逻辑代码修改很少,甚至不用修改。 与WinForm开发相比,我们一般在后置代码中会使用控件的名字来操作控件的属性来更新UI,而在WPF中通常是通过

iceberg7012的博客 2万+

用友u8服务器优化,用友U8erp软件运行的性能优化方案图文教程

今天一定要给大家分享一下用友U8erp软件运行的性能优化方案图文教程,以下方案来自用友软件官方维护工程师反馈在在处理客户问题的时候,有一类问题较为挠头,就是诸如用友U8服务器效率低下的问题。比如用友U8运行慢,卡住等,我们暂先不提用友U8软件代码、脚本层面本身的性能提升问题,我们只是基于现有用友U8软件版本,在客户现有环境下排查用友U8软件运行的性能瓶颈,并采取一定的技术手段进行性能调校。本文将推...

weixin_39882870的博客 5311

CTP开发实战:如何高效处理预埋单与委托单的完整流程(附代码示例)

本文深入探讨了CTP开发中预埋单与委托单的完整处理流程,从订单生命周期理解、核心代码实现到异步事件处理与系统健壮性构建。通过详细的代码示例和状态机分析,为开发者提供了构建高可靠订单处理系统的实战指南,重点解决了状态同步、异常恢复等关键工程挑战。

weixin_29234239的博客 121

二、FM1288调试方案-工具软件使用

分析音频数据的软件需要满足以下几点:1、能打开原始pcm音频文件;2、基本的音频播放软件的操作;3、能查看分析音频数据的频率成分;4、能查看分析音频音量幅度;5、能完成基本音频处理:降噪、音量增幅限幅、滤波和均衡;

CSDNmianfeixiazai001的博客 1300

【DaVinci Developer工具实战】03 -导入xml文件

本文档描述了 DaVinci Developer 导入数据的的具体功能 ,它适用于导入AUTOSAR SWC 模板【AUTOSAR XML 文件】 文件。

1995

Oracle 11g安装教程(保姆级超详细安装教程)

Oracle详细安装步骤,每一步都配有图例引导 ORACLE是数据库(Oracle服务端)。是连接数据库的客户端(Oracle客户端)。是连接oracle的可视化客户端软件。

2302_81846649的博客 1万+

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

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

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

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

上一篇: R语言中使用colnames函数来指定矩阵的列名称
下一篇: R语言统计绘图:t检验的实现方法
普通网友
博客等级 码龄3年 247粉丝 610原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址