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

2024-03-07 02:28
文章标签 fill ggplot2 exercise

本文主要是介绍[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

这篇关于[R] ggplot2 - exercise (“fill =“)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/782137

相关文章

Android fill_parent、match_parent、wrap_content三者的作用及区别

这三个属性都是用来适应视图的水平或者垂直大小,以视图的内容或尺寸为基础的布局,比精确的指定视图的范围更加方便。 1、fill_parent 设置一个视图的布局为fill_parent将强制性的使视图扩展至它父元素的大小 2、match_parent 和fill_parent一样,从字面上的意思match_parent更贴切一些,于是从2.2开始,两个属性都可以使用,但2.3版本以后的建议使

Computer Exercise

每日一练 单选题 在计算机机箱前面板接口插针上(     C   )表示复位开关。 A.SPK    B.PWRLED    C.RESET    D.HDDLED每台PC机最多可接(     B   )块IDE硬盘。 A.2    B.4    C.6    D.8(     B   )拓扑结构由连接成封闭回路的网络结点组成的,每一结点与它左右相邻的结点连接。 A.总线型    B

ggplot2高效实用指南 (可视化脚本、工具、套路、配色)

作者:严涛 浙江大学作物遗传育种在读研究生(生物信息学方向)伪码农,R语言爱好者,爱开源 ggplot2学习笔记之图形排列 R包ggseqlogo |绘制序列分析图 编者按:数据可视化是解析、理解和展示数据不可缺少的一部分。炫或不炫看个人喜好和功底,能否达意是最基本的要求---最合适的图示和配色表达最直观的含义。长文多图预警,这是关于ggplot2使用的极详细教程(190+图),是入门和晋级

Fill the Square

中文题目解释详见我的博客:http://xiaoshig.sinaapp.com/?p=94   In this problem, you have to draw a square using uppercase English Alphabets. To be more precise, you will be given a square grid with some empty bl

R语言可视化入门——使用ggplot2将模拟结果可视化

目录 零、引言一、数据生成二、单参数的展示(不同方法和案例)三、双参数的展示(不同方法和案例)四、后期服务 零、引言 在做科研时候,往往会设计不同的案例,不同参数和不同方法来说明自己模型(方法)的可行性。但是如何将这些元素展现在一张图上就至关重要,本文主要介绍了不同案例不同方法参数下基于ggplot2系统的可视化。具体细节没有细致的讲解,感兴趣的可以自行学习。 一、数据生成

lower_bound与upper_bound还有fill的使用

STL一直很好用,今天使用了一下lower_bound和upper_bound函数,熟练使用可以减少写二分的时间。 lower_bound是二分查找出大于等于给出的数的第一个值。upper_bound是二分查找出大于给出的数的第一个值。 这两个函数都是返回的地址,所以使用还要减去首地址(如果数组里面保存的是int) 下面是使用lower_bound优化最长上升子序列。由于长度相同的上升

java Arrays.fill方法介绍

Arrays.fill 是 Java 标准库中的一个方法,用于将数组中的所有元素设置为指定的值。它可以用于一维数组以及多维数组的填充。这个方法非常有用,当你需要快速初始化或重置数组时。 方法签名 // 用于填充一维数组public static void fill(int[] a, int val);// 用于填充指定范围内的一维数组public static void fill(int[

C++ fill fill_n 函数

fill 函数和 fill_n 函数在c++ 被称为写入元素算法 在大多数情况下,fill 函数的效率通常比 for 循环更高,尤其是在处理大规模数据时。这是因为 fill 函数通常是用更底层的、高效的方式实现的,可以减少循环开销。 解决memset函数不能对 数组 进行 数字初始化 赋值 然而,具体的效率也取决于编程语言和实现细节。在一些语言中,编译器可能会优化 for 循环,使其效率接近

shiny + bootstrap + shinythemes + ggplot2 + sunburst 动态交互式可视化web框架搭建

利用shiny + bootstrap + shinythemes + ggplot2 + sunburst 搭建自己的交互式可视化web

Dock属性值为Fill时,直接撑满全部区域问题

解决步骤: 1.将Dock为Fill的控件设置为none,然后剪切出来 2.重新设置左上右下四个区域的控件的Dock属性 3.将Dock为Fill的控件粘贴上去,设置其Dock属性为Fill