Chapter 15 附录1 图可视化

15.1 条形图(Bar plots)

15.3 线图

## `summarise()` has grouped output by 'Class'. You can override using the `.groups` argument.

## `summarise()` has grouped output by 'Class'. You can override using the `.groups` argument.

图例编号

15.5 脑区图

## # dk cortical brain atlas
##   regions: 35 
##   hemispheres: left, right 
##   side views: lateral, medial 
##   palette: yes 
##   use: ggplot() + geom_brain() 
## ----
##    hemi  side    region                label                   roi  
##    <chr> <chr>   <chr>                 <chr>                   <chr>
##  1 left  lateral bankssts              lh_bankssts             0002 
##  2 left  lateral caudal middle frontal lh_caudalmiddlefrontal  0004 
##  3 left  lateral fusiform              lh_fusiform             0008 
##  4 left  lateral inferior parietal     lh_inferiorparietal     0009 
##  5 left  lateral inferior temporal     lh_inferiortemporal     0010 
##  6 left  lateral lateral occipital     lh_lateraloccipital     0012 
##  7 left  lateral lateral orbitofrontal lh_lateralorbitofrontal 0013 
##  8 left  lateral middle temporal       lh_middletemporal       0016 
##  9 left  lateral pars opercularis      lh_parsopercularis      0019 
## 10 left  lateral pars orbitalis        lh_parsorbitalis        0020 
## # ℹ 76 more rows

15.7 diagram

15.8 Gant plot

#dev.off()

#各种不同的图例
# legend("topright", 
#        #shape
#        pch=22,
#        # size of the text
#        cex = 0.9,
#        #text.width = 12,
#        # inset = c(0, 0.6),
#        # no box border
#        #bty = "n",
#        # size of the icon
#        pt.cex=2, 
#        pt.bg=gray(c(0.3, 0.9)),
#        #border="white", 
#        legend=c("Completed","In Progress"),
#        #title="MSc plan", 
#        bg="white")
# 
# ## get corrdinates
# coord <- par("usr")
# legend(x = coord[2] * 0.85, y = coord[4]*0.5,
#        xjust = 0,
#        yjust = 0,
#        #shape
#        pch=22,
#        # size of the text
#        cex = 0.7,
#        #text.width = 12,
#        inset = c(1, 0.5),
#        # no box border
#        #bty = "n",
#        # size of the icon
#        pt.cex=1, 
#        pt.bg=gray(c(0.3, 0.9)),
#        #border="white", 
#        legend=c("Completed","In Progress"),
#        #title="MSc plan", 
#        bg="white")
# 
# legend( x= "bottomright", y=100, 
#         legend=c("quantile","90st"), 
#         col=c("blue", "yellow", ),   
#         pch=c(".","."))
# 
# legend("topright", y = 1,
#        legend(pch=22,
#               pt.cex= 1, 
#               pt.bg=gray(c(0.3, 0.9)),
#               border="black", 
#               legend=c("Completed", "Not Yet Done"), 
#               bg="white"))
# 
# legend(x = data.frame(x = "Courses", y = 2017-09-1),
#        pch=22, 
#        pt.cex=2, 
#        pt.bg=gray(c(0.3, 0.9)),
#        horiz=TRUE,
#        border="black", 
#        legend=c("Completed", "Not Yet Done"), 
#        title="Juqiang's plan", bg="white")



# author's reply

# g <- new("gantt")
# g <- ganttAddTask(g, "Courses") # no times, so a heading
# g <- ganttAddTask(g, "Physical Oceanography (A)", "2016-09-03", "2016-12-05", done=100)
# g <- ganttAddTask(g, "Chemistry Oceanography (B)", "2016-09-03", "2016-12-05", done=100)
# g <- ganttAddTask(g, "Fluid Dynamics (A+)", "2016-09-03", "2016-12-05", done=100)
# g <- ganttAddTask(g, "Biological Oceanography", "2017-01-03", "2017-04-05")
# g <- ganttAddTask(g, "Geological Oceanography", "2017-01-03", "2017-04-05")
# g <- ganttAddTask(g, "Time-series Analysis", "2017-01-03", "2017-04-05")
# g <- ganttAddTask(g, "Research") # no times, so a heading
# g <- ganttAddTask(g, "Literature review", "2016-09-03", "2017-02-01", done=20)
# g <- ganttAddTask(g, "Develop analysis skills", "2016-09-03", "2017-08-01", done=30)
# g <- ganttAddTask(g, "Thesis work", "2016-10-01", "2018-07-15", done=30)
# g <- ganttAddTask(g, "Thesis proposal", "2017-05-01", "2017-06-01")
# g <- ganttAddTask(g, "Writing (papers & thesis)", "2017-03-01", "2018-07-15")
# g <- ganttAddTask(g, "Defend thesis", "2018-09-01", "2018-09-05")
# 
# plot(g, ylabel=list(font=ifelse(is.na(g[["start"]]), 2, 1)),
#      event.time="2018-08-13", event.label="Report Date")
# 
# par(lend="square") # default is round
# 
# par(xpd=NA)
# legend(x=0.8, y=1.05,
#        pch=22,
#        pt.cex= 1, 
#        pt.bg=gray(c(0.3, 0.9)),
#        border="black", 
#        legend=c("Completed", "Not Yet Done"), 
#        bg="white")


## ggplot version

library(lubridate)
library(scales)
library(Cairo)

# Alternatively you can put all this in a CSV file with the same columns and
# then load it with read_csv()
# tasks <- read_csv("path/to/the/file")

tasks <- tribble(
        ~Start,       ~End,         ~Project,          ~Task,
        "2015-11-15", "2015-11-20", "Data collection", "Use IssueCrawler to expand lists",
        "2015-11-21", "2015-11-25", "Data collection", "Complete INGO databases",
        "2015-11-16", "2015-12-15", "Data collection", "Find all INGO legislation",
        "2015-12-15", "2015-12-25", "Data collection", "Code INGO restrictions",
        "2015-11-15", "2015-11-25", "Data collection", "Develop general INGO survey",
        "2015-11-25", "2015-12-31", "Data collection", "Administer survey",
        "2015-12-25", "2015-12-31", "Data analysis",   "Model stability and restrictions",
        "2016-01-01", "2016-02-02", "Writing",         "Chapter on formal restrictions (H1)",
        "2016-01-15", "2016-02-15", "Writing",         "Paper or chapter on survey results (H3 and H4)",
        "2016-03-16", "2016-03-19", "Writing",         "ISA conference in Atlanta",
        "2016-02-01", "2016-03-01", "Data collection", "Historical INGO restrictions in China",
        "2016-02-01", "2016-03-01", "Data collection", "Historical INGO restrictions in Egypt",
        "2016-05-01", "2016-06-01", "Data collection", "Fieldwork in London and Beijing",
        "2016-03-01", "2016-04-01", "Data analysis",   "Analyze restrictions in China and Egypt",
        "2016-04-01", "2016-06-01", "Data analysis",   "Analyze INGO activities",
        "2016-04-01", "2016-06-15", "Writing",         "Chapter on application of restrictions (H2)",
        "2016-05-01", "2016-07-01", "Writing",         "Chapter on INGO ideal points (H3)",
        "2016-05-01", "2016-07-01", "Writing",         "Chapter on INGO flexibility (H4)",
        "2016-07-01", "2016-08-01", "Writing",         "Theory",
        "2016-08-01", "2016-09-15", "Writing",         "Conclusion",
        "2016-08-01", "2016-09-15", "Writing",         "Introduction"
)

# Convert data to long for ggplot
tasks.long <- tasks %>%
        mutate(Start = ymd(Start),
               End = ymd(End)) %>%
        gather(date.type, task.date, -c(Project, Task)) %>%
        arrange(date.type, task.date) %>%
        mutate(Task = factor(Task, levels=rev(unique(Task)), ordered=TRUE))

# Custom theme for making a clean Gantt chart
theme_gantt <- function(base_size=11 ) {
        ret <- theme_bw(base_size) %+replace%
                theme(panel.background = element_rect(fill="#ffffff", colour=NA),
                      axis.title.x=element_text(vjust=-0.2), 
                      axis.title.y=element_text(vjust=1.5),
                      title=element_text(vjust=1.2),
                      panel.border = element_blank(), axis.line=element_blank(),
                      panel.grid.minor=element_blank(),
                      panel.grid.major.y = element_blank(),
                      panel.grid.major.x = element_line(size=0.5, colour="grey80"),
                      axis.ticks=element_blank(),
                      legend.position="bottom", 
                      axis.title=element_text(size=rel(0.8) ),
                      strip.text=element_text(size=rel(1)),
                      strip.background=element_rect(fill="#ffffff", colour=NA),
                      panel.spacing.y=unit(1.5, "lines"),
                      legend.key = element_blank())
        
        ret
}

# Calculate where to put the dotted lines that show up every three entries
x.breaks <- seq(length(tasks$Task) + 0.5 - 3, 0, by=-3)

# Build plot
timeline <- ggplot(tasks.long, aes(x=Task, y=task.date, colour=Project)) + 
        geom_line(size=6) + 
        geom_vline(xintercept=x.breaks, colour="grey80", linetype="dotted") + 
        guides(colour=guide_legend(title=NULL)) +
        labs(x=NULL, y=NULL) + coord_flip() +
        scale_y_date(date_breaks="2 months", labels=date_format("%b ‘%y")) +
        theme_gantt() + theme(axis.text.x=element_text(angle=45, hjust=1))

timeline

15.9 图标Lengends

15.10 坐标轴

## Warning: Removed 2 rows containing missing values (`geom_col()`).

##    Word Frequency
## 1   the   6187266
## 2    of   2947667
## 3   and   2687866
## 4    to   2595604
## 5     a   2167570
## 6    in   1724982
## 7  that   1037961
## 8    is   1017505
## 9   was   1009930
## 10   it    989823
## 11  for    900003
## 12   on    892343
## 13 with    750221
## 14   as    692788
## 15   by    678463
## 16   at    670544
## 17 from    567532
## 18 this    559451
## 19   be    548621
## 20  are    541298

15.11 配色

15.12 注释标记