[R] Underline your idea with ggplot2

Preview:

# 介绍:之前的教程中,我们学习了如何使条形图或直方图看起来更好

比如:

1. How to select a graph = calibrate the geom part
2. How to select variables = calibrate the aes part
3. How to add a title = calibrate the labs part
4. How to put the bar in a certain order = fct_infreq in the aes part
5. How to change the colour and how to fill the bars = the fill in aes part or geombar , or the option scale_fill
6. How to adjust transparency = alpha

# 今天我们将学习如何在图形中添加信息,编辑图例中的文本元素,并改变主题

# 添加图形中的信息使用geom_text()

# 示例:在条形图上添加每个条形的计数

ggplot(data = mpg, aes(x = class)) +geom_bar() +geom_text(stat = 'count', aes(label = ..count..), vjust = -0.5)

# 编辑图例中的文本元素并改变主题使用theme()

# 示例:改变坐标轴文本的大小和位置

ggplot(data = mpg, aes(x = class)) +geom_bar() +theme(axis.text.x = element_text(angle = 45, size = 10))

# 理解数据可视化的指导原则

# 例如,平衡、强调、运动、模式、重复、节奏和多样性

# 使用散点图进行两个连续变量的数据可视化

# 使用条形图进行两个分类数据的数据可视化,并学习新的自定义设置

# 使用一个连续变量和一个分类变量进行数据可视化

Main Content

Add info in the plots:

首先,让我们来看看如何在图形中添加信息。在R中,我们可以使用geom_text()函数来实现这一点。例如,如果我们想在条形图上显示每个条形的计数,我们可以这样做:

ggplot(data = mpg, aes(x = class)) +geom_bar() +geom_text(stat = 'count', aes(label = ..count..), vjust = -0.5)

 

  • ggplot(data = mpg, aes(x = class)): This sets up the basic plot using the mpg dataset and specifies that the class variable should be mapped to the x-axis.

  • geom_bar(): This adds a bar plot layer to the plot, creating a bar for each unique value of the class variable.

  • geom_text(stat = 'count', aes(label = ..count..), vjust = -0.5): This adds text labels to the plot. The stat = 'count' argument tells geom_text to calculate the count of observations for each class. The aes(label = ..count..) specifies that the count should be used as the label for each bar. The vjust = -0.5 argument adjusts the vertical position of the labels to place them above the bars.

  • if vjust = 0.5

接下来,让我们讨论如何编辑图例中的文本元素并改变图形的主题。在R中,我们可以使用theme()函数来实现这一点。例如,如果我们想改变坐标轴文本的大小和位置,我们可以这样做:

ggplot(data = mpg, aes(x = class)) +geom_bar() +theme(axis.text.x = element_text(angle = 45, size = 10))

 Changing the text size and position in the x or y axis 

 + theme(axis.text.x = element_text(angle = 45, size=10))
+ theme(axis.text.x = element_text(angle = 45,size=7))
  • family: Specifies the font family to be used for the axis text. For example, setting family = "Arial" would use the Arial font for the axis text.

  • face: Specifies the font style to be used for the axis text. This can be used to make the text bold, italic, or bold italic. For example, setting face = "bold" would make the axis text bold.

  • colour: Specifies the color of the axis text, ticks, and marks. For example, setting colour = "red" would make the axis text red.

  • size: Specifies the size of the axis text. For example, setting size = 12 would make the axis text 12 points in size.

  • angle: Specifies the angle at which the axis text is displayed. For example, setting angle = 45 would rotate the axis text 45 degrees clockwise.

remove axis ticks and labels

you can remove axis ticks and labels using element_blank() or size=0 in theme() in ggplot2. Here's how you can do it:

library(ggplot2)# Create a basic plot
p <- ggplot(data = mpg, aes(x = class)) +geom_bar() +geom_text(stat = 'count', aes(label = ..count..), vjust = -0.5)# Remove x-axis ticks and labels
p + theme(axis.text.x = element_blank(),axis.ticks.x = element_blank())# Remove y-axis ticks and labels
p + theme(axis.text.y = element_blank(),axis.ticks.y = element_blank())

 Add the headcount for each bar in a graph which indicate proportion

ggplot(CUHKSZ_employment_survey_1,aes(fct_infreq(Occupation),y=(..count..)/sum(..count..),fill=Occupation))+geom_bar()+geom_text(stat='count',aes(label=..count..),vjust=+1.5)

 

ggplot(CUHKSZ_employment_survey_1, aes(x = fct_infreq(Occupation), fill = Occupation)) +geom_bar(aes(y = (..count..)/sum(..count..))) +geom_text(stat = 'count', aes(label = ..count.., y = (..count..)/sum(..count..)), vjust = +1.5) +labs(title="Occupation of CUHK Shenzhen students after graduation",x=NULL, y="Proportion")

 

If you want to remove the x-axis label entirely, you can use x = "" instead.

ggplot(CUHKSZ_employment_survey_1, aes(x = fct_infreq(Occupation), fill = Occupation)) +geom_bar(aes(y = (..count..)/sum(..count..))) +geom_text(stat = 'count', aes(label = ..count.., y = (..count..)/sum(..count..)), vjust = +1.5) +labs(title="Occupation of CUHK Shenzhen students after graduation", x = "", y = "Proportion")

 If I want to underline that students are more likely to become “Professional an technician” or “Clerical personnel”, I might use the same color for those category

Scale_fill_manual(values=c(“color1”,”color2”….)
# Define custom colors
custom_colors <- c("Professional and technician" = "Red", "Clerical personnel" = "Red", "Other" = "grey")# Create the plot with custom colors
ggplot(CUHKSZ_employment_survey_1, aes(x = fct_infreq(Occupation), fill = Occupation)) +geom_bar(aes(y = (..count..)/sum(..count..))) +geom_text(stat = 'count', aes(label = ..count.., y = (..count..)/sum(..count..)), vjust = +1.5) +labs(title="Occupation of CUHK Shenzhen students after graduation", x = "", y = "Proportion") +scale_fill_manual(values = custom_colors)

If I want to underline that students more than 10% of the students become “Professional an technician” “Clerical personnel” or “managerial personnel”, colour should de different and I should add a horizontal line

+geom_hline(yintercept=0.1)

 

ggplot(CUHKSZ_employment_survey_1, aes(x = fct_infreq(Occupation), fill = Occupation)) +geom_bar(aes(y = (..count..)/sum(..count..))) +theme(axis.text.x =element_text(angle = 45,vjust = 0.6))+geom_text(stat = 'count', aes(label = ..count.., y = (..count..)/sum(..count..)), vjust = +1.5) +labs(title="Occupation of CUHK Shenzhen students after graduation", x = "", y = "Proportion") +scale_fill_manual(values = custom_colors) +geom_hline(yintercept=0.1)

 

Demonstrate that your data are normally distributed by over-ploting a Gaussian curve on your histogram

ggplot(CUHKSZ_employment_survey_1, aes(x = Monthly_salary_19, y = stat(density))) +geom_histogram(binwidth = 500, fill = "blue", colour = "black", alpha = 0.5, boundary = 8000) +geom_density(color = "red") +labs(title = "Histogram of Monthly Salary with Density Curve Overlay", x = "Monthly Salary", y = "Density")

Notice to use stat(density) here instead of ...density... , or it will report an Error

or more(

Warning message:
`stat(density)` was deprecated in ggplot2 3.4.0.
ℹ Please use `after_stat(density)` instead.
This warning is displayed once every 8 hours.
Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated. )

Underline the individuals who are overweigth in the BMI histogram = change the colour of the bar in an histogram

Decompose the histogram into two using the function subset

ggplot(SEE_students_data_2,aes(x=BMI))+geom_histogram(data=subset(SEE_students_data_2,BMI<25),fill="Blue", alpha=0.5,binwidth = 1,color="Black")+geom_histogram(data=subset(SEE_students_data_2,BMI>25),fill="Red", alpha=0.5,binwidth = 1,color="Black")

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/724972.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

指针乐园----上

大家好&#xff0c;我是Beilef&#xff0c;许久未见还请多多关照。 文章目录 目录 文章目录 前言 一、指针是什么 二、指针的运用 1.指针变量和地址 2.指针变量和解引⽤操作符&#xff08;*&#xff09; 解引用操作符 3.指针变量类型及意义 3.2指针的-整数 3.3 void* 指针 …

docker-compose Install Dockge

Dockge Dockge 是一个精美的、易于使用的、反应式的自托管 docker compose.yaml 面向堆栈的管理器。 主要特性: 通过Web页面管理compose.yaml文件。 创建/编辑/启动/停止/重新启动/删除容器。更新Docker镜像。交互式Web终端。响应式设计,实时更新进度(Pull/Up/Down)和Web…

Win11桌面出现的这个图标“了解此图片”怎么关闭?

&#x1f9d1;个人简介&#xff1a;大家好&#xff0c;我是尘觉&#xff0c;希望我的文章可以帮助到大家&#xff0c;您的满意是我的动力&#x1f609; 在csdn获奖荣誉: &#x1f3c6;csdn城市之星2名 ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ …

Java开发手册,java高并发高可用面试题

前言 今年我也33了&#xff0c;离传说中不好找工作的35岁又更近了。说没有焦虑是对自己撒谎&#xff0c;于是我采访了一些人&#xff0c;自己思考了下&#xff0c;写下了这篇文章&#xff0c;希望能有些共鸣。 先看看大家的态度&#xff1a; 色老力衰&#xff0c;不好忽悠&a…

rt-thread uart驱动

uart驱动描述基于GD32F470芯片。 rt-thread提供了一套I/O设备模型&#xff0c;如果想要使用操作系统的驱动去进行操作&#xff0c;就得将具体芯片的硬件驱动注册到设备驱动框架上。 关于rt-thread的I/O设备模型相关内容可以参考 rt-thread I/O设备模型-CSDN博客文章浏览阅读55…

vue命令行

书接上回&#xff0c;一直没能将vue-shop-master项目启动&#xff0c;还是停留在命令行&#xff0c;现参考链接如下&#xff0c; https://blog.csdn.net/jieyucx/article/details/127915699 止步不前了&#xff0c;晚上再摸索吧&#xff0c;搞电商去。。。。溜了溜了&#x…

MySQL进阶之(四)InnoDB数据存储结构之行格式

四、InnoDB数据存储结构之行格式 4.1 行格式的语法4.2 COMPACT 行格式4.2.1 记录的额外信息01、变长字段长度列表02、NULL 值列表03、记录头信息 4.2.2 记录的真实数据 4.3 Dynamic 和 Compressed 行格式4.3.1 字段的长度限制4.3.2 行溢出4.3.3 Dynamic 和 Compressed 行格式 4…

【Flutter 】get-cli init报错处理

报错内容 get init 命令跳出,报错内如下 Select which type of project you want to creat Synchronous waiting using dart:cli waitFor Unhandled exceotion . Dart WaitforEvent is deprecated and disabled by default. This feature will be fully removed in Dart 3.4 …

【定岗定编】某度假村酒店客房部定岗定编管理咨询项目纪实

该度假村酒店由于地域广阔&#xff0c;将客服部分为了四个不同区域&#xff0c;这样就导致了在不同的区域员工的接待量不均衡的状况&#xff0c;引起了员工的强烈不满。如何合理地配置客户部人员以及如何合理地鉴定员工的工作量成为该酒店所面临的两大难题。让我们来看看在人力…

vulhub中ThinkPHP5 SQL注入漏洞 敏感信息泄露

漏洞原理 传入的某参数在绑定编译指令的时候又没有安全处理&#xff0c;预编译的时候导致SQL异常报错。然而thinkphp5默认开启debug模式&#xff0c;在漏洞环境下构造错误的SQL语法会泄漏数据库账户和密码 启动后&#xff0c;访问http://your-ip/index.php?ids[]1&ids[]2…

即插即用篇 | YOLOv8 引入 NAM 注意力机制 | 《NAM: Normalization-based Attention Module》

论文名称:《NAM: Normalization-based Attention Module》 论文地址:https://arxiv.org/pdf/2111.12419.pdf 代码地址:https://github.com/Christian-lyc/NAM 文章目录 1 原理2 源代码3 添加方式4 模型 yaml 文件template-backbone.yamltemplate-small.yamltemplate-large…

C++之类型转换

C语言中的类型转换 在C语言中, 如果赋值运算符左右两侧类型不同, 或者形参与实参类型不匹配, 或者返回值类型与 接收返回值类型不一致时, 就需要发生类型转化, C语言中总共有两种形式的类型转换: 隐式类型转换和显式类型转换 1. 隐式类型转化是关联度很强, 意义相近的类型之间…

【CSP试题回顾】201512-2-消除类游戏

CSP-201512-2-消除类游戏 解题思路 输入棋盘大小和颜色: 首先&#xff0c;程序从标准输入读取两个整数n和m&#xff0c;分别代表棋盘的行数和列数。然后&#xff0c;程序读取接下来的n行输入&#xff0c;每行包含m个整数&#xff0c;代表棋盘上每个方格中的棋子颜色。 初始化…

[蓝桥杯 2017 省 A] 油漆面积 Java代码及一些个人理解

[蓝桥杯 2017 省 A] 油漆面积 题目描述 X 星球的一批考古机器人正在一片废墟上考古。 该区域的地面坚硬如石、平整如镜。 管理人员为方便&#xff0c;建立了标准的直角坐标系。 每个机器人都各有特长、身怀绝技。它们感兴趣的内容也不相同。 经过各种测量&#xff0c;每个…

【论文速读】 | AI驱动修复:漏洞自动化修复的未来

本次分享论文为&#xff1a;AI-powered patching: the future of automated vulnerability fixes 基本信息 原文作者&#xff1a;Jan Nowakowski, Jan Keller 作者单位&#xff1a;Google Security Engineering 关键词&#xff1a;AI, 安全性漏洞, 自动化修复, LLM, sanitiz…

Objective-C blocks 概要

1.block的使用 1.1什么是block&#xff1f; Blocks是C语言的扩充功能&#xff1a;带有自动变量&#xff08;局部变量&#xff09;的匿名函数。 “带有自动变量”在Blocks中表现为“截取自动变量" “匿名函数”就是“不带名称的函数” 块&#xff0c;封装了函数调用及调用…

全方位碾压chatGPT4的全球最强模型Claude 3发布!速通指南在此!保姆级教学拿脚都能学会!

&#x1f389;&#x1f389;欢迎光临&#xff0c;终于等到你啦&#x1f389;&#x1f389; &#x1f3c5;我是苏泽&#xff0c;一位对技术充满热情的探索者和分享者。&#x1f680;&#x1f680; &#x1f31f;持续更新的专栏《Spring 狂野之旅&#xff1a;从入门到入魔》 &a…

Android开发岗还不会这些问题,温故而知新

前言 前前后后经历过大项目、小项目&#xff0c;跨平台&#xff0c;小程序&#xff0c;Nodejs服务等等&#xff0c;目前在做的Rom开发&#xff0c;定制各种手机中的奇葩需求&#xff0c;从应用层到Framework层&#xff0c;再到C层&#xff0c;再到驱动&#xff0c;最终到Linux&…

MybatisPlus入门详解

一、MyBatisPlus 简介 1.1 创建新模块 <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.1</version></dependency> 由于mp并未被收录到idea的系统内置配置,无法…

Solidity Uniswap V2 Pair中添加流动性

添加流动性的功能的用户入口&#xff0c;UniswapV2在UniswapV2Router中实现&#xff0c;它用来计算新的流动性并发行LP-Token&#xff0c;流动性管理简单地视为LP-Token管理。当你为一个pair增加流动性时&#xff0c;合约会创造LP Token;当你移除流动性时&#xff0c;LP-Token就…