[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;那么这个类别所对应的输出节点的输出值应…

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");…

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

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

算法竞赛基础:树状数组

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

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…

CSS常见布局方式

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

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…

苹果群控软件开发必不可少的代码!

在开发苹果群控软件时&#xff0c;编写高质量的代码是至关重要的&#xff0c;这些代码不仅需要实现功能&#xff0c;还需要保证软件的稳定性、安全性和可扩展性&#xff0c;本文将分享四段在苹果群控软件开发中必不可少的源代码&#xff0c;并解释每段代码的作用和实现原理。 一…

HM2019创建分析模型

步骤一&#xff1a;查看单元类类型&#xff08;通过card edit&#xff09;&#xff0c;然后展开模型查看模型信息&#xff1b;步骤二&#xff1a;为材料集里添加新的材料 材料:Al 弹性模量E:70000 泊松比NU:0.33 其中&#xff1a;MAT1表示各向同性材料&#xff0c;E表示弹…

【漏洞复现】ShopXO任意文件读取漏洞

Nx01 产品简介 ShopXO是一套开源的企业级开源电子商务系统&#xff0c;包含PC、H5、微信小程序、支付宝小程序、百度小程序等多个终端&#xff0c;遵循Apache2开源协议发布&#xff0c;基于ThinkPHP5.1框架研发。该系统具有求实进取、创新专注、自主研发、国内领先企业级B2C电商…

three.js可以对3D模型做什么操作和交互,这里告诉你。

Three.js 提供了多种交互功能&#xff0c;可以对 3D 模型进行各种操作和交互。以下是一些常见的交互功能&#xff1a; 鼠标交互 通过鼠标事件&#xff0c;可以实现模型的拖拽、旋转、缩放等操作。例如&#xff0c;可以通过鼠标拖拽来改变模型的位置或角度。 触摸交互 对于支…

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

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