• [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

     

    1. #tutorial 5 -script
    2. #Exercise 1
    3. #1.1#
    4. ggplot(smoking_and_drug_use_amongst_English_pupils,aes(x=AlAge2))+geom_histogram(fill="Blue",stat="count")
    5. #1.2
    6. 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")
    7. #Exercise 2
    8. #2.1
    9. ggplot(smoking_and_drug_use_amongst_English_pupils,aes(x=Books2))+geom_bar()
    10. #2.2
    11. ggplot(smoking_and_drug_use_amongst_English_pupils,aes(fct_infreq(Books2)))+geom_bar()
    12. #.2.3
    13. 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="")
    14. #Exercise 3
    15. #3.1
    16. 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")
    17. #3.2
    18. #the distribution is very close for both sex
    19. #3.3
    20. 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

  • 相关阅读:
    面试官:volatile如何保证可见性的,具体如何实现?
    maven使用时候出现,jdk1.5的情况的解决办法
    ARM 汇编指令 orreq 的使用
    谷歌浏览器自定义标签页 newtab
    PIC单片机4——定时器方波
    实验室储样瓶耐强酸强碱PFA材质试剂瓶适用新材料半导体
    LINUX 服务器中病毒了,后来追踪到的一个机器运行脚本,研究了一下对于初学者shell的人有很大的帮助
    Elasticsearch安装
    软考重点6 数据结构与算法
    Vim功能大纲
  • 原文地址:https://blog.csdn.net/m0_74331272/article/details/136517763