[R] Underline your idea with ggplot2

news2025/1/25 9:21:20

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.coloradmin.cn/o/1495042.html

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

相关文章

Golang各版本的GC详解

go v1.3的标记清除法 清除的第一步&#xff1a;stw将可达对象标记删除未被标记对象 go v1.5三色标记法 从根节点出发&#xff0c;将下一个节点遍历为灰色&#xff0c;放入灰色集合中遍历灰色节点集合&#xff0c;把灰色能到达的节点标记为灰色&#xff0c;把自身标记为黑色&a…

安全增强型 Linux

书接上篇 一查看selinux状态 SELinux的状态&#xff1a; enforcing&#xff1a;强制&#xff0c;每个受限的进程都必然受限 permissive&#xff1a;允许&#xff0c;每个受限的进程违规操作不会被禁止&#xff0c;但会被记录于审计日志 disabled&#xff1a;禁用 相关命令…

SystemVerilog Support

介绍 AMD Vivado™合成支持可以合成的SystemVerilog RTL的子集。这个以下部分介绍了这些数据类型。 针对特定文件的SystemVerilog 默认情况下&#xff0c;Vivado合成工具使用Verilog 2005语法编译*.v文件和*.sv文件使用SystemVerilog语法。要在Vivado IDE中将SystemVerilog作…

**蓝桥OJ 178全球变暖 DFS

蓝桥OJ 178全球变暖 思路: 将每一座岛屿用一个颜色scc代替, 用dx[]和dy[]判断他的上下左右是否需要标记颜色,如果已经标记过颜色或者是海洋就跳过.后面的淹没,实际上就是哪个块上下左右有陆地,那么就不会被淹没,我用一个tag标记,如果上下左右一旦有海洋,tag就变为false.如果tag…

开发者如何选择代码签名证书?

代码签名证书是一种由权威认证机构颁发的数字证书&#xff0c;它允许软件开发者对其代码进行数字签名。这种签名基于公钥基础设施&#xff08;PKI&#xff09;技术&#xff0c;使用一对密钥&#xff1a;一个私钥和一个公钥。私钥用于生成签名&#xff0c;而公钥则嵌入到代码签名…

Linux学习-二级指针的使用

目录 ###指针传参时要用二级指针 ###函数体内部想要修改函数外部指针变量值的时候需要使用二级指针(指针变量的地址) ###指针传参时要用二级指针 char *str[5]; int Fun(char **ppstr,int len); ###函数体内部想要修改函数外部指针变量值的时候需要使用二级指针(指针变量的…

GCN原理回顾

Cora_dataset description Cora数据集是一个常用的学术文献用网络数据集&#xff0c;用于研究学术文献分类和图网络分析等任务。 该数据集由机器学习领域的博士论文摘要组成&#xff0c;共计2708篇论文&#xff0c;涵盖了7个不同的学科领域。每篇论文都有一个唯一的ID&#xf…

桥接模式: 消息发送器设计

桥接模式是一种结构型设计模式&#xff0c;它将抽象部分与它的实现部分分离&#xff0c;使它们可以独立地变化。桥接模式通过将抽象和实现分离&#xff0c;可以让它们可以独立地变化&#xff0c;从而提高系统的灵活性和可扩展性。 在桥接模式中&#xff0c;有两个重要的概念&a…

JavaBoy假期如何学习项目?弯道块才是真的快!

至暗时刻 老话说的好&#xff0c;弯道快才是真的快&#xff0c;谁直线不会加油&#xff1f;每到假期都是在座的各位弯道超车的时候。转眼自己已经出来搬了快四年砖头了&#xff0c;偶尔访问下牛客发现行情真是一年不如一年。。。不由得回想起自己春招时候的经历。 回想起2020年…

数据分析-Pandas数据的直方图探查

数据分析-Pandas数据的直方图探查 数据分析和处理中&#xff0c;难免会遇到各种数据&#xff0c;那么数据呈现怎样的规律呢&#xff1f;不管金融数据&#xff0c;风控数据&#xff0c;营销数据等等&#xff0c;莫不如此。如何通过图示展示数据的规律&#xff1f; 数据表&…

【贪心算法】Leetcode 455.分发饼干 376. 摆动序列 53. 最大子数组和

【贪心算法】Leetcode 455 分发饼干 376. 摆动序列【规律很多】53. 最大子数组和 455 分发饼干局部最优推全局最优&#xff1a;尽量用大饼干去满足大胃口的小朋友 376. 摆动序列【规律很多】思想&#xff1a;注意考虑一个坡度留首尾两个点、平坡、首尾 53. 最大子数组和【好思想…

FreeRTOS学习笔记-基于stm32(1)任务基础知识

一、裸机与RTOS 我们使用的32板子是裸机&#xff0c;又称前后台系统。裸机有如下缺点&#xff1a; 1、实时性差。只能一步一步执行任务&#xff0c;比如在一个while循环中&#xff0c;要想执行上一个任务&#xff0c;就必须把下面的任务执行完&#xff0c;循环一遍后才能执行…

从0开始学习NEON(2)

1、前言 继上一个例子&#xff0c;本次继续来学习NEON&#xff0c;本次学习NEON中向量拼接的操作&#xff0c;主要应用在图像的padding中。 https://blog.csdn.net/weixin_42108183/article/details/136440707 2、案例 2.1 案例1 在某些情况下&#xff0c;需要取在每个向量…

轻松压缩照片大小:简单实用的方法

当您需要通过网络传输或共享照片时&#xff0c;较小的文件大小可以提高传输速度并减少带宽消耗。这适用于通过电子邮件、社交媒体、即时消息应用程序等发送照片的场景。为了解决这个问题&#xff0c;本文将介绍一些简单而有效的方法来压缩照片的大小&#xff0c;以便更方便地分…

python并发编程:IO模型

一 IO模型 二 network IO 再说一下IO发生时涉及的对象和步骤。对于一个network IO \(这里我们以read举例\)&#xff0c;它会涉及到两个系统对象&#xff0c;一个是调用这个IO的process \(or thread\)&#xff0c;另一个就是系统内核\(kernel\)。当一个read操作发生时&#xff…

面试经典150题——基本计算器

​A husband is a man of many miles. ——Unknown 1. 题目描述 2. 题目分析与解析 2.1 思路一——先算括号内的内容 这个题目其实就是编译原理中很小的一个模块了&#xff0c;基本思路还是通过栈来实现。题目的难点主要在&#xff1a; 其中括号优先级的处理&#xff0c;以…

Spring揭秘:ImportBeanDefinitionRegistrar应用场景及实现原理!

内容概念 ImportBeanDefinitionRegistrar接口提供了强大的动态注册Bean的能力&#xff0c;它允许开发者在Spring容器初始化时&#xff0c;灵活地根据特定条件或需求来添加或修改Bean定义&#xff0c;从而实现更为精细的控制和扩展性。这是构建可扩展框架、插件系统或处理复杂配…

请说说你对Vue模板编译的理解

Vue模板编译是Vue.js框架的核心之一&#xff0c;它负责将Vue模板转换成渲染函数&#xff0c;从而实现模板的解析和渲染。要深入了解Vue模板编译&#xff0c;我们需要从编译过程、作用、特点等方面进行详细解析。 1. Vue模板编译的作用 Vue模板编译的主要作用是将Vue模板字符串…

021—pandas 书单整理将同一种书整理在一起

前言 在办公自动化场景下&#xff0c;最常见的需求就是信息的整理&#xff0c;pandas 最擅长复杂数据逻辑的处理&#xff0c;能够让整理工作更加高效&#xff0c;同时不容易出错。今天的案例是将一个平铺的书单按品类进行整理&#xff0c;合并为一行。 需求: 将书按书名进行合…

【python高级编程教程】笔记(python教程、python进阶)第三节:(1)多态与鸭子类型(Polymorphism and Duck Typing)

参考文章1&#xff1a;【比刷剧还爽】清华大佬耗时128小时讲完的Python高级教程&#xff01;全套200集&#xff01;学不会退出IT界&#xff01; 参考文章2&#xff1a;清华教授大力打造的Python高级核心技术&#xff01;整整100集&#xff0c;强烈建议学习&#xff08;Python3…