[R] ggplot2 - exercise (“fill =“)

We have made the plots like:

 Let's practice with what we have learnt in: [R] How to communicate with your data? - ggplot2-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/m0_74331272/article/details/136513694

 

#tutorial 5 -script
#Exercise 1
#1.1#
ggplot(smoking_and_drug_use_amongst_English_pupils,aes(x=AlAge2))+geom_histogram(fill="Blue",stat="count")
#1.2
ggplot(smoking_and_drug_use_amongst_English_pupils,aes(x=AlAge2))+geom_histogram(fill="Blue",stat="count", alpha=0.75)+labs(title="Age at which English pupils drunk alcohol for the first time",x="Age")


#Exercise 2
#2.1
ggplot(smoking_and_drug_use_amongst_English_pupils,aes(x=Books2))+geom_bar()
#2.2 
ggplot(smoking_and_drug_use_amongst_English_pupils,aes(fct_infreq(Books2)))+geom_bar()

#.2.3
ggplot(smoking_and_drug_use_amongst_English_pupils,aes(fct_infreq(Books2),fill=Books2))+geom_bar(alpha=0.5)+scale_fill_brewer(palette="Greens")+labs(title="Nbr of books that english pupils have at home",x="")

#Exercise 3
#3.1
ggplot(smoking_and_drug_use_amongst_English_pupils,aes(x=AlAge2,fill=Sex))+geom_histogram(stat="count", alpha=0.75)+labs(title="Age at which English pupils experienced alcohol for the first time", x="Age")
#3.2
#the distribution is very close for both sex
#3.3
ggplot(smoking_and_drug_use_amongst_English_pupils,aes(x=CgAge2,fill=Sex))+geom_density(stat="count",alpha=0.25)+labs(title="Age at which English pupils smoke cigarette for the first time", x="Age")


ExerciseDescriptionaes Function EffectFill EffectStat EffectAlphaLabs Effect
1.1Histogram of the AlAge2 variable with blue barsx=AlAge2BluecountNoneNone
1.2Histogram of the AlAge2 variable with blue bars, adjusted transparency, and axis labelsx=AlAge2Bluecount0.75Title: "Age at which English pupils drunk alcohol for the first time"; x-axis label: "Age"
2.1Bar plot of the Books2 variablex=Books2NonecountNoneNone
2.2Bar plot of the Books2 variable with reordered levels by frequencyx=fct_infreq(Books2)NonecountNoneNone
2.3Bar plot of the Books2 variable with reordered levels by frequency and filled by Books2x=fct_infreq(Books2); fill=Books2Books2count0.5Title: "Nbr of books that english pupils have at home"; x-axis label: ""
3.1Histogram of the AlAge2 variable with bars filled by Sex and adjusted transparencyx=AlAge2; fill=SexSexcount0.75Title: "Age at which English pupils experienced alcohol for the first time"; x-axis label: "Age"
3.3Density plot of the CgAge2 variable with density curves filled by Sexx=CgAge2; fill=SexSexcount0.25Title: "Age at which English pupils smoke cigarette for the first time"; x-axis label: "Age"

1.1 

1.2

2.1

2.2

2.3

Why the former one don't need fill = Books2 ?

In ggplot2, when you use the fct_infreq() function to reorder a categorical variable like Books2 based on frequency, ggplot automatically creates bars for each level of the reordered variable. This means that each bar in the plot represents a level of the Books2 variable, and the bars are automatically filled based on the default color scheme.

In the second line of code, aes(fct_infreq(Books2), fill=Books2) is used to specify that the fill aesthetic should be mapped to the Books2 variable. This means that the bars in the bar plot will be filled based on the levels of the Books2 variable, and the fct_infreq(Books2) function is used to reorder the bars based on the frequency of each level.

In contrast, in the first line of code, aes(fct_infreq(Books2)) is used without specifying fill=Books2. This means that the bars in the bar plot will be filled with a default color, and the fct_infreq(Books2) function is still used to reorder the bars based on frequency. However, the fill aesthetic is not explicitly mapped to any variable, so the bars will not be filled based on the levels of the Books2 variable and they are the same.

If we delete the fill = part in the 2.3:

ggplot(smoking_and_drug_use_amongst_English_pupils,aes(fct_infreq(Books2)))+geom_bar(alpha=0.5)+scale_fill_brewer(palette="Greens")+labs(title="Nbr of books that english pupils have at home",x="")

The scale_fill_brewer will not work:

3.1

if we delete the fill = sex

3.2

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

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

相关文章

20 easy 70. 爬楼梯

//假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 // // 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢? // // // // 示例 1: // // //输入:n 2 //输出:2 //解释:有两种方法可以爬到楼顶。 /…

electron 架构

文章目录 Chromium 架构Electron 架构 Chromium 架构 主体架构:主进程 Browser,打开一个页面就会启动一个 Render 渲染进程,进程间通信就是 IPC 机制(Inter-Process Communication)。 主进程的 RenderProcessHost 和 R…

算法 - 【受限条件下可到达节点的数目】

受限条件下可到达节点的数目 题目示例1示例2 分析代码 题目 现有一棵由 n 个节点组成的无向树,节点编号从 0 到 n - 1 ,共有 n - 1 条边。给你一个二维整数数组 edges ,长度为 n - 1 ,其中 edges[i] [ai, bi] 表示树中节点 ai 和…

【通信原理笔记】【一】确定信号分析——1.4 信号的功率谱与能量谱

文章目录 前言一、功率谱与能量谱1. 从频域求能量说起2. 再聊聊密度3. 单边能量谱与带宽3.1 实信号的共轭对称性3.2 单边谱与带宽 二、信号的相关函数1. 说回到时域分析 总结 前言 上一篇我们学习了信号的功率与能量,现在我们继续深入,探讨一下信号的功…

Pytorch学习 day04(Totensor、Normalize、Resize、Compose)

Totensor 把一个PIL格式的图片,或者ndarray格式的图片转换为tensor格式使用方法,如下: from PIL import Image from torchvision import transforms from torch.utils.tensorboard import SummaryWriterimg Image.open("images/00130…

Redis 面试题

Redis 基础 什么是 Redis? Redis (Remote Dictionary Server) 本质上是一个 Key-Value 类型的内存数据库,很像 memcached,整个数据库统统加载在内存当中进行操作,定期通过异步操作把数据库数据 flush 到硬盘上进行保存。因为是纯…

【二】【SQL Server】如何运用SQL Server中查询设计器通关数据库期末查询大题

教学管理系统201703153 教学管理系统数据库展示 成绩表展示 课程表展示 学生表展示 院系表展示 一、基本操作 设置复合主键 设置其他表的主键 设置字段取值范围 二、简单操作 第一题 第二题 第三题 第四题 结尾 最后,感谢您阅读我的文章,希望这些内容能…

RF接口测试(1)

RF是做接口测试的一个非常方便的工具,我们只需要写好发送报文的脚本,就可以灵活的对接口进行测试。 做接口测试我们需要做如下工作: 1、拼接发送的报文 2、发送请求的方法 3、对结果进行判断 我们先按步骤实现,再进行RF操作的…

降低85%的gc发生率:ES的GC调优实践!

#大数据/ES #经验 #性能 问题背景 客户方面反馈的问题是ES入库速度变慢,延迟升高到几百毫秒,导致数据积压过多,影响了业务。 排查发现ES的服务日志出现不少的gc overhead现象,下面是一个示例的日志片段: [yyyy-MM-…

C++单例模式、工厂模式

一、单例模式 (一) 什么是单例模式 1. 是什么? 在系统的整个生命周期内,一个类只允许存在一个实例。 2. 为什么? 两个原因: 节省资源。方便控制,在操作公共资源的场景时,避免了多个对象引起的复杂操作…

腾讯云4核8G服务器轻量和CVM可用来干什么?

腾讯云4核8G服务器适合做什么?搭建网站博客、企业官网、小程序、小游戏后端服务器、电商应用、云盘和图床等均可以,腾讯云4核8G服务器可以选择轻量应用服务器4核8G12M或云服务器CVM,轻量服务器和标准型CVM服务器性能是差不多的,轻…

android基础学习

从上面的描述就可以知道,每一个Activity组件都有一个对应的ViewRoot对象、View对象以及WindowManager.LayoutParams对象。这三个对象的对应关系是由WindowManagerImpl类来维护的。具体来说,就是由WindowManagerImpl类的成员变量mRoots、mViews和mParams所…

Linux环境下使用轮询方式操作UART

目录 概述 1 Linux环境下UART设备 2 轮询方式操作UART功能实现 2.1 打开串口函数:usr_serial_open 2.2 关闭串口函数: usr_serial_close 2.3 发送数据函数: usr_serial_sendbytes 2.4 接收数据函数: usr_serial_readbytes …

【QT C++实践】Qt 项目中一个界面动态处理多张数据库中的表|附源码

一、前言 在之前那篇讲如何使用QT连接数据库时(QT C实践|超详细数据库的连接和增删改查操作|附源码),做了一个简单的对数据库进行增删改查的界面(如下)。 但是存在一个问题就是:这个界面只是对一张表进行操作,但是我…

探索HTTP/2

文章目录 http/1.1http/2疑惑 探索1. 连接前言2. 帧结构2.1 帧类型 Type 3. 帧详情3.1 SETTINGS 帧3.2 WINDOW_UPDATE 帧3.3 PRIORITY 帧3.4 HEADERS 帧3.5 DATA 帧3.6 PING3.7 GOAWAY 帧3.8 RST_STREAM 帧3.9 PUSH_PROMISE 帧3.10 CONTINUATION 帧 你对http2了解多少&#xff…

BUUCTF--极客大挑战php

文章目录 1.网站备份文件www.zip2.下载后发现class.phpindex.phpflag.php 3.分析php代码绕过__wakeup方法变量权限为私有或保护python方法url方法 1.网站备份文件www.zip 2.下载后发现 class.php <?php include flag.php; error_reporting(0);class Name{private $usernam…

MYSQL5.7报1205 - Lock wait timeout exceeded; try restarting transaction

简介 今天使用navicate操作添加时&#xff0c;mysql报错误&#xff0c;错误如下 原因 这个问题的原因是在mysql中产生了事务A&#xff0c;执行了修改的语句&#xff0c;比如&#xff1a; update t1 set aget18 where id1;此时事务并未进行提交&#xff0c;事务B开始运行&am…

【C++精简版回顾】19.异常处理

1.throw抛出问题 int print(int a,int b) {if (b 0)throw b;return a / b; } 2.try与catch解决问题 try {print(2, 0); } catch (int b) {cout << "竟然是&#xff1a;"<<b<<endl; } 结果&#xff1a; 补充1&#xff1a;可以抛出字符串等 1.throw…

pytorch配置环境

1.查看cuda版本 nvidia-smi cuda version:12.3 2.下载torch 然后根据版本去查找对应的 torch下载代码 可查看这里&#xff1a;Previous PyTorch Versions | PyTorch 然后执行 conda install pytorch1.10.0 torchvision0.11.0 torchaudio0.10.0 cudatoolkit11.3 -c pytorch…

Python的文件操作

归纳编程学习的感悟&#xff0c; 记录奋斗路上的点滴&#xff0c; 希望能帮到一样刻苦的你&#xff01; 如有不足欢迎指正&#xff01; 共同学习交流&#xff01; &#x1f30e;欢迎各位→点赞 &#x1f44d; 收藏⭐ 留言​&#x1f4dd; 路在脚下&#xff0c;勇往直前&#x…
最新文章