[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.mzph.cn/news/724532.shtml

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

相关文章

微信小程序开发系列(八)·微信小程序页面的划分以及轮播图区域的绘制和图片的添加

目录 1. 划分页面结构 2. 轮播图区域绘制 3. 轮播图图片添加 1. 划分页面结构 最终我们想达到如下效果&#xff1a; 其页面分为四层结构&#xff0c;因此我们需要配置四块view&#xff0c;代码如下&#xff1a; <!-- view 小程序提供的容器组件&#xff0c;可以当成…

【学习】torch.nn.CrossEntropyLoss交叉熵损失函数

交叉熵损失函数torch.nn.CrossEntropyLoss 交叉熵主要是用来判定实际的输出与期望的输出的接近程度&#xff0c;为什么这么说呢&#xff0c;举个例子&#xff1a; 在做分类的训练的时候&#xff0c;如果一个样本属于第K类&#xff0c;那么这个类别所对应的输出节点的输出值应…

支持国密的 Web 服务器

目录 前言编译 Nginx-with-GmSSLv3安装编译依赖的包下载源码编译代码配置与运行数字证书的生成与配置Nginx配置文件修改运行 Nginx-with-GmSSLv3测试Nginx小结前言 在上一章ARM 架构下国密算法库中,介绍了在银河麒麟系统 ARM 架构下编译国密库 GmSSL,在本章中,我将介绍集成…

96、C++ 性能优化一览

在对 C++ 版本的 resnet50 经过大约 5 个版本的优化之后,性能也基本达到了预期。至少利用手写的 resnet50 在 CPU 上推理一张图片感觉不到卡顿了。 下面对这几个版本的性能优化做一个总结。 初始版本1 第一版本的 C++ 代码,并没有考虑性能问题,仅仅是想按照手写 resnet50 …

操作系统原理与实验——实验四短进程优先调度

实验指南 运行环境&#xff1a; Dev c 算法思想&#xff1a; 短进程优先 (SPF)调度算法则是从就绪队列中选出一个估计运行时间最短的进程&#xff0c;将处理机分配给它&#xff0c;使它立即执行并一直执行到完成 核心数据结构&#xff1a; typedef struct data{ int hour; int…

Docker镜像操作介绍

一、镜像操作 镜像的操作可分为&#xff1a; 拉取镜像&#xff1a;拉取远程仓库的镜像到本地 docker pull重命名镜像&#xff1a;使用docker tag 命令重命名镜像查看镜像&#xff1a;使用docker image ls 或者 docker images命令查看本地已经存在的镜像删除镜像&#xff1a;删…

蓝桥杯倒计时 38 天

整数二分模板&#xff1a;数的范围 二分的本质不是单调性&#xff0c;而是二分出能满足某种性质使得将整数分成两半。 思考&#xff1a;模板题&#xff0c;模板记熟就能做 #include<iostream> using namespace std; int n,q; const int N 1e510; int a[N]; int main…

Jenkins 将shell脚本启动方式修改为bash

platform"arm x86" if [[ "$platform" ~ "arm" ]] thenecho "arm" fi最近在调试Jenkins实现的一些功能&#xff0c;发现在本地可以运行的脚本内容到了Jenkins里面就没办法运行了&#xff0c;不是提示unexpected operator就是提示[[ : …

Python 系统学习总结(基础语法+函数+数据容器+文件+异常+包+面向对象)

&#x1f525;博客主页&#xff1a; A_SHOWY&#x1f3a5;系列专栏&#xff1a;力扣刷题总结录 数据结构 云计算 数字图像处理 力扣每日一题_ 六天时间系统学习Python基础总结&#xff0c;目前不包括可视化部分&#xff0c;其他部分基本齐全&#xff0c;总结记录&#xff0…

网络编程 24/3/6 作业

1、数据库的增删改 #include <myhead.h> int main(int argc, const char *argv[]) {//定义数据库句柄指针sqlite3 *kdbNULL;//打开数据库&#xff0c;不存在则创建if(sqlite3_open("./my.db",&kdb)!SQLITE_OK){printf("sqlite3_open error\n");…

Golang-如何优雅的关闭一个Channel?

如何优雅的关闭Channel 这部分主要参考自&#xff1a;https://qcrao91.gitbook.io/go/channel/ru-he-you-ya-di-guan-bi-channel 直接关闭存在的问题 主要就是上述“向已关闭的Channel收发&#xff0c;会如何&#xff1f;”中所提到的情况&#xff1a; 1、向已关闭的channel…

全连接神经网络算法原理(激活函数、前向传播、梯度下降法、损失函数、反向传播)

文章目录 前言1、全连接神经网络的整体结构&#xff1a;全连接神经网络模型是由输入层、隐藏层、输出层所组成&#xff0c;全连接神经网络结构如下图所示&#xff1a;全连接神经网络的每一层都是由一个一个的神经元所组成的&#xff0c;因此只要搞清楚神经元的本质就可以搞清楚…

算法竞赛基础:树状数组

算法竞赛基础&#xff1a;树状数组 是什么&#xff1f; 树状数组虽然语义上是树状&#xff0c;但是实际上还是一个数组。 树状数组的功能就是单点和区间的修改和查询。 例如&#xff0c;如果想增加一个点的值&#xff0c;那么你需要让其上方所有能对齐的树状数组c全部增加相同…

java设计模式课后作业(待批改)

此文章仅记录学习&#xff0c;欢迎各位大佬探讨 实验&#xff08;一&#xff09; 面向对象设计 实验目的 ①使用类来封装对象的属性和功能&#xff1b; ②掌握类变量与实例变量&#xff0c;以及类方法与实例方法的区别&#xff1b; 知识回顾 详情见OOP课件 实验内容…

QChart柱状图

//柱状图// 创建柱状图数据QBarSet *set0 new QBarSet("");*set0 << 1601 << 974 << 655 << 362;QBarSeries *series new QBarSeries();series->append(set0);set0->setColor(QColor("#F5834B"));// 创建柱状图QChart *ch…

github双因子认证

最近换了个安卓手机&#xff0c;打算让之前的苹果手机退役了&#xff0c;所以需要重新搞GitHub的Two-factor authentication 步骤如下&#xff1a; 1. 访问安全中心 https://github.com/settings/security 2. 点击Authenticator app右侧按钮 3. 下载腾讯身份验证器&#xff…

MySQL作业题

一、取得每个部门最高薪水的人员名称 第一步&#xff1a;取得每个部门的最高薪水 select e.deptno, max(e.sal) as maxSal from emp e group by e.deptno; ----------------- | deptno | maxSal | ----------------- | 20 | 3000.00 | | 30 | 2850.00 | | 10 | 5…

CSS常见布局方式

一、静态布局&#xff08;Static Layout&#xff09; 既传统web设计 就是不管浏览器尺寸多少&#xff0c;网页布局就按当时写代码的布局来布置; 块级元素&#xff1a;每个块级元素会在上一个元素下面另起一行&#xff0c;他们会被设置好的margin分离。块级元素是垂直组织的。 …

进程和线程的创建过程

进程和线程的创建过程是不同的,具体如下: 进程的创建过程: 进程的创建过程涉及到写时复制(Copy-on-Write,COW)机制。写时复制是一种可以延迟甚至避免拷贝数据的内存映射方式。新进程的创建过程大致如下: 操作系统为新进程分配必要的资源,如进程控制块等。父进程的内存数据区…

python跨文件夹调用

如图所示&#xff0c;我们要在bin文件夹下的run_patchcore.py文件中调用src/patchcore文件夹下的backbone.py, common.py等文件。如第13行 方法1&#xff1a;将patchcore的上一层目录src添加到环境变量中 run_patchcore.py 文件中写入import sys sys.path.append(/home/rui/P…