[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博客https://blog.csdn.net/m0_74331272/article/details/136513694

R 复制代码
#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")
Exercise Description aes Function Effect Fill Effect Stat Effect Alpha Labs Effect
1.1 Histogram of the AlAge2 variable with blue bars x=AlAge2 Blue count None None
1.2 Histogram of the AlAge2 variable with blue bars, adjusted transparency, and axis labels x=AlAge2 Blue count 0.75 Title: "Age at which English pupils drunk alcohol for the first time"; x-axis label: "Age"
2.1 Bar plot of the Books2 variable x=Books2 None count None None
2.2 Bar plot of the Books2 variable with reordered levels by frequency x=fct_infreq(Books2) None count None None
2.3 Bar plot of the Books2 variable with reordered levels by frequency and filled by Books2 x=fct_infreq(Books2); fill=Books2 Books2 count 0.5 Title: "Nbr of books that english pupils have at home"; x-axis label: ""
3.1 Histogram of the AlAge2 variable with bars filled by Sex and adjusted transparency x=AlAge2; fill=Sex Sex count 0.75 Title: "Age at which English pupils experienced alcohol for the first time"; x-axis label: "Age"
3.3 Density plot of the CgAge2 variable with density curves filled by Sex x=CgAge2; fill=Sex Sex count 0.25 Title: "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:

R 复制代码
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

相关推荐
风中飘爻1 小时前
JavaScript:BOM编程
开发语言·javascript·ecmascript
kyle~1 小时前
ROS2---std_msgs基础消息包
开发语言·python·机器人·ros·机器人操作系统
满怀10151 小时前
【NumPy科学计算引擎:从基础操作到高性能实践】
开发语言·python·numpy
我命由我123452 小时前
35.Java线程池(线程池概述、线程池的架构、线程池的种类与创建、线程池的底层原理、线程池的工作流程、线程池的拒绝策略、自定义线程池)
java·服务器·开发语言·jvm·后端·架构·java-ee
&zzz2 小时前
Python生成exe
开发语言·python
Chandler242 小时前
Go:方法
开发语言·c++·golang
CopyLower3 小时前
分布式ID生成方案的深度解析与Java实现
java·开发语言·分布式
随便@_@4 小时前
基于MATLAB/simulink的信号调制仿真--AM调制
开发语言·matlab·simulink·移动仿真
爱代码的小黄人4 小时前
深入解析系统频率响应:通过MATLAB模拟积分器对信号的稳态响应
开发语言·算法·matlab
vsropy4 小时前
matlab安装python API 出现Invalid version: ‘R2022a‘,
开发语言·python