#### Use read_csv() or another function

#### Make sure your data is converted into a tibble. 
library(pacman)
p_load(tidyverse, readxl, bruceR, scipub,htmltools, htmlTable)
source("https://raw.githubusercontent.com/ChungPingCheng/R4BS2/main/R4BS_setup.R")
## Loading required package: ggmirt
## Loading required package: brolgar
## 
## Attaching package: 'brolgar'
## The following object is masked from 'package:insight':
## 
##     n_obs
dta_lydia <- read_excel("/Users/huangyuanzi/Desktop/Coursera/datavisualizationwithR/S5/111/dta_all_and_SCScalculated.xlsx")
dta_lydia$refl<-round(rowSums(dta_lydia[, 215:219]),0)
dta_lydia$p_sc <- dta_lydia$SCS_pos
dta_lydia$n_sc <- dta_lydia$SCS_neg
dta_lydia$resilience <- rowSums(dta_lydia[, 168:192])
dta_lydia$SCS_ttl <- dta_lydia$SCS_ttl
dta_lydia <- dta_lydia %>% mutate(Gender = case_when(gender == "生理男" ~ as.double(1),gender == "生理女" ~ as.double(0)))
dta1<-dta_lydia|>select(refl,age,p_sc,n_sc,resilience,SCS_ttl,Gender)
dta1 <- mutate(dta1, 性別 = recode(Gender,
                                    `0` = "女性",
                                    `1` = "男性"))
dta<- dta1%>% 
  mutate(age = substr(age, 1, 2))
refl_score <-aggregate(refl~性別,data=dta,FUN=mean)
ggplot(refl_score, aes(x = 性別, y = refl, fill = 性別))+
  geom_bar(stat = "identity")+
  geom_errorbar(aes(x=性別, ymin=refl-sd(dta$refl), ymax=refl+sd(dta$refl)), width=0.2, colour="black", alpha=1, size=0.3)+
  labs(x = "Gender", y = "Reflective ruminaiton", title = "Reflective ruminaiton by Gender")
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
This warning is displayed once every 8 hours.
Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
generated.

2.

p_sc_score <-aggregate(p_sc~性別,data=dta,FUN=mean)
ggplot(p_sc_score, aes(x = 性別, y = p_sc, fill = 性別)) +
  geom_bar(stat = "identity")+
  labs(x = "Gender", y = "positive self-compassion", title = "positive self-compassion by Gender")+
  geom_errorbar(aes(x=性別, ymin=p_sc-sd(dta$p_sc), ymax=p_sc+sd(dta$p_sc)), width=0.2, colour="black", alpha=1, size=0.3)

library(gganimate)
library(gifski)

###PUT YOUR ggplot figure with the appropriate gganimate functions here. It will compile in the html R Markdown report.
library(dplyr)
# Make a ggplot, but add frame=year: one image per year
ggplot(dta, aes(x=resilience,fill=性別)) + 
  geom_histogram(binwidth = 0.5, alpha = 0.7)+
  # gganimate specific bits:
  transition_states(Gender) +
  ease_aes('cubic-out')+
  enter_fade() +
  exit_fade() +theme_grey()

ggplot(data = dta, 
             aes(x=refl, y=p_sc))+
        geom_point(alpha=.2)+
        stat_smooth(method='lm',#計算回歸 
                       formula = y ~ x,#直線
                       se=TRUE, #不加standard error
                       linewidth = 0.5)+ 
        aes(color=性別)+
      labs(y='positive self-compassion',
       x='reflective ruminaiton',
       title='The scatter plot for positive self-compassion and reflective ruminaiton in different gender')

5.

ggplot(data = dta, 
             aes(x=refl, y=resilience))+
        geom_point(alpha=.2)+
        stat_smooth(method='lm',#計算回歸 
                       formula = y ~ x,#直線
                       se=TRUE, #不加standard error
                       linewidth = 0.5)+ 
        aes(color=性別)+
      labs(y='resilience',
       x='Reflective ruminaiton',
       title='The scatter plot for resilience and Reflective ruminaiton in different gender')

6.

ggplot(data = dta, 
             aes(x=p_sc, y=resilience))+
        geom_point(alpha=.2)+
        stat_smooth(method='lm',#計算回歸 
                       formula = y ~ x,#直線
                       se=TRUE, #不加standard error
                       linewidth = 0.5)+ 
        aes(color=性別)+
  labs(y='resilience',
       x='positive self-compassion',
       title='The scatter plot for resilience and positive self-compassion in different gender')

7

ggplot(data=dta, 
     aes(x=age, y=refl, color=性別)) +
  stat_summary(fun.data = "mean_cl_boot", 
               position=position_dodge(.2))+
  
  stat_summary(aes(group=性別), fun = mean, 
               geom="line",
               position=position_dodge(.2))+
  labs(y='score of reflective rumination',
       x='age',
       title='The relationships between reflective rumination and age in different gender')+
  theme(legend.position='top')
Warning: Removed 6 rows containing missing values or values outside the scale range
(`geom_segment()`).

8

ggplot(data=dta, 
     aes(x=age, y=resilience, color=性別)) +
  stat_summary(fun.data = "mean_cl_boot", 
               position=position_dodge(.2))+
  
  stat_summary(aes(group=性別), fun = mean, 
               geom="line",
               position=position_dodge(.2))+
  labs(y='score of resilience',
       x='age',
       title='The relationships between resilience and age in different gender')+
  theme(legend.position='top')
Warning: Removed 6 rows containing missing values or values outside the scale range
(`geom_segment()`).

9

library(ggplot2)
ggplot(dta, aes(x = 性別, y = resilience)) +
  geom_boxplot()+
  labs(title="the box plot for gender and resilience ")