[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.mfbz.cn/a/434702.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【Mybatis】批量映射优化 分页插件PageHelper 逆向工程插件MybatisX

文章目录 一、Mapper批量映射优化二、插件和分页插件PageHelper2.1 插件机制和PageHelper插件介绍2.2 PageHelper插件使用 三、逆向工程和MybatisX插件3.1 ORM思维介绍3.2 逆向工程3.3 逆向工程插件MyBatisX使用 总结 一、Mapper批量映射优化 需求: Mapper 配置文件很多时&…

16:00面试,16:08就出来了,问的问题过于变态了。。。

从小厂出来&#xff0c;没想到在另一家公司又寄了。 到这家公司开始上班&#xff0c;加班是每天必不可少的&#xff0c;看在钱给的比较多的份上&#xff0c;就不太计较了。没想到2月一纸通知&#xff0c;所有人不准加班&#xff0c;加班费不仅没有了&#xff0c;薪资还要降40%…

Sparse A*算法的时间复杂度

Sparse A*(SAS)算法是A*算法的变型算法&#xff0c;下面将结合A*算法的流程分析SAS的时间复杂度。对于SAS算法而言&#xff0c;其航迹规划的时间 T T T主要由两部分组成&#xff1a; T s T_s Ts​&#xff1a;在当前结点扩展可行子结点的时间&#xff1b; T 0 T_0 T0​&#…

Qt QPainter的使用方法

重点&#xff1a; 1.QPainter在QWidget窗口的paintEvent中使用。 2.QPainter通常涉及到设置画笔、设置画刷、绘图&#xff08;QPen、QBrush、drawxx&#xff09;三个流程。 class Widget : public QWidget {Q_OBJECTprotected:void paintEvent(QPaintEvent *event) Q_DEC…

基于51单片机的四位并行数据主从机传输设计

基于51单片机的四位并行数据主从机传输设计[proteus仿真] 主从机通信系统这个题目算是课程设计和毕业设计中常见的题目了&#xff0c;本期是一个基于51单片机的四位并行数据主从机传输设计 需要的源文件和程序的小伙伴可以关注公众号【阿目分享嵌入式】&#xff0c;赞赏任意文…

机器学习---数据分割

之前的文章中写过&#xff0c;我们可以通过实验测试来对学习器的泛化误差进行评估并进而做出选择。 为此&#xff0c;需使用一个“测试集"(testing set)来测试学习器对新样本的判别能力&#xff0c;然后以测试集上的“测 试误差”(testing error)作为泛化误差的近似。通…

操作系统系列学习——内核级线程实现

文章目录 前言内核级线程实现 前言 一个本硕双非的小菜鸡&#xff0c;备战24年秋招&#xff0c;计划学习操作系统并完成6.0S81&#xff0c;加油&#xff01; 本文总结自B站【哈工大】操作系统 李治军&#xff08;全32讲&#xff09; 老师课程讲的非常好&#xff0c;感谢 【哈工…

Linux第71步_将linux中的多个文件编译成一个驱动模块

学习目的&#xff1a;采用旧字符设备测试linux系统点灯&#xff0c;进一步熟悉其设计原理。采用多文件参与编译&#xff0c;深度学习编写Makefile&#xff0c;有利于实现驱动模块化设计。 1、创建MyOldLED目录 输入“cd /home/zgq/linux/Linux_Drivers/回车” 切换到“/home…

softmax和sigmoid的区别

sigmoid 公式&#xff1a; s i g m o i d ( x ) 1 1 e − x sigmoid(x) \frac{1}{1 e^{-x}} sigmoid(x)1e−x1​ 函数曲线如下&#xff1a; 导数公式&#xff1a; f ( x ) ′ e − x ( 1 e − x ) 2 f ( x ) ( 1 − f ( x ) ) f(x)\prime \frac{ e^{-x}}{(1 e^{-x})…

备战蓝桥杯————二分搜索(一)

引言 一、二分查找 基本概念 代码框架 二、二分查找 题目描述 解题思路及代码 结果展示 三、寻找左侧边界的二分搜索 使用背景 基本代码 引言 在计算机科学的世界里&#xff0c;二分查找算法无疑是一种经典且强大的工具。它以其高效的性能&#xff0c;在有序数据集中…

95、评估使用多线程优化带来的性能提升

本节评估一下&#xff0c;通过对卷积的 co 维度进行多线程切分之后&#xff0c;对于模型的性能提升。 评估下性能 在进行多线程程序运行时&#xff0c;建议电脑中的 CPU 不要有其他繁重的任务执行。 在相同的环境下&#xff0c;分别运行 5th_codegen 和 6th_multi_thread 下的…

Pytorch 复习总结 6

Pytorch 复习总结&#xff0c;仅供笔者使用&#xff0c;参考教材&#xff1a; 《动手学深度学习》Stanford University: Practical Machine Learning 本文主要内容为&#xff1a;Pytorch 计算机视觉。 本文先介绍了计算机视觉中两种常见的改进模型泛化性能的方法&#xff1a…

香港投资+移民计划高峰论坛圆满落幕洞察趋势,探索未来财富之路

3月4日&#xff0c;由中国香港太阳联合资本有限公司牵头举办的「香港投资移民计划高峰论坛」在港交所圆满结束。吸引超260位高净值人士参加&#xff0c;已收到近600个投资移民意向。此次论坛汇聚了来自世界各地的投资移民专家、企业家、以及潜在投资者&#xff0c;共同探讨香港…

网络编程(3/4)

广播 ​ #include<myhead.h>int main(int argc, const char *argv[]) {//1、创建套接字int sfd socket(AF_INET, SOCK_DGRAM, 0);if(sfd -1){perror("socket error");return -1;}//2、将套接字设置成允许广播int broadcast 1;if(setsockopt(sfd, SOL_SOC…

docker部署前后端分离项目

docker部署前后端分离项目 前提&#xff0c;服务器环境是docker环境&#xff0c;如果服务器没有安装docker&#xff0c;可以先安装docker环境。 各个环境安装docker&#xff1a; Ubuntu上安装Docker&#xff1a; ubuntu离线安装docker: CentOS7离线安装Docker&#xff1a; Cen…

如何给Vue项目配置好一个nginx.conf文件?

如何给Vue项目配置好一个nginx.conf文件&#xff1f; 一般前端项目中&#xff0c;会有一个docker/nginx/nginx.conf文件&#xff0c;用于配置DockerFile配置等。 那么&#xff0c;如何给项目写好一个nginx.conf文件&#xff0c;以DockerFile为例&#xff1a; # 使用 Node.js …

【LeetCode】并查集OJ

目录 1.省份数量 2. 等式方程的可满足性 1.省份数量 题目地址&#xff1a; 547. 省份数量 - 力扣&#xff08;LeetCode&#xff09; 解题思路&#xff1a;对于该题我们直接使用并查集&#xff0c;将可以直接的城市都归类一个集合&#xff0c;最后统计数组中集合的总数就是…

Qt入门(一)Qt概述

Qt是什么&#xff1f; Qt是一个跨平台应用开发框架。 Qt既包括了一系列的Qt库&#xff0c;还包括诸多配套的开发工具如QtCreater&#xff0c;GUI Designer。Qt本身是由C开发的&#xff0c;但是也提供了其他编程语言的接口。 Qt的定位以及同类 学一种技术&#xff0c;最重要的是…

WordPress建站入门教程:忘记数据库名称、用户名和密码了怎么办?

有时候我们需要进入phpMyAdmin管理一些数据库&#xff0c;但是登录phpMyAdmin时却需要我们输入数据库的用户名和密码&#xff0c;但是我们不记得了应该怎么办呢&#xff1f; 其实&#xff0c;我们只需要进入WordPress网站根目录找到并打开wp-config.php文件&#xff0c;就可以…

FPGA- RGB_TFT显示屏原理及驱动逻辑

下图是TFT显示屏的显示效果 该显示屏共分为 2 个版本&#xff0c;4.3 寸版本的 TFT4.3’’_V3.0 和 5.0 寸版本的 TFT5.0’’_V3.0。 两者 PCB 背板电路完全相同&#xff0c;接口脚位定义完全相同&#xff0c;接口时序完全相同&#xff0c;仅使用的显示屏 模组尺寸不同。设计两…
最新文章