Chapter 1 Notes for ggplot2
guides
:fill = guide_legend(title = NULL)
: remove legend titlefill = guide_legend(nrow=4, byrow=TRUE)
; put the legends into 4 rows
theme
:legend.position='none'
: hide legendlegend.position='bottom'
: put legend at the bottomaxis.title.x = element_blank()
: remove x axis name (or usexlab()
)axis.text.x = element_text(angle = 45)
: adjust x axis text angleaxis.text.x = element_blank()
: remove labels on x axisaxis.text.x = element_text(color = c("red"))
: change label colors on x axisplot.title = element_text(hjust = 0.5)
: center titletext=element_text(size=15)
: change font size- use
base_size = 15
intheme_xx()
- use
family = "xxx"
to change font family
- use
scale_color_manual
: modify legendsvalues
: change colorlabels
: change legends labelsname
: change legend title
scale_x_continuous
: change breaks and range of x axisbreak
: set breakslimits
: set range
stat_function
: add function curve
stat_function(color = 'blue', fun=dunif, args=list(0,1),size=1.2)
geom_segment
: segment plot
ggplot(d2,
aes(x = lower, #横向线段
xend = upper,
y = county,
yend = county))+
geom_segment(lwd=1, alpha=0.5) +
labs(x='mean', y="county", title='95% Credible Intervals') +
scale_color_brewer(palette='Set1') +
scale_y_reverse() +
theme_bw()
Remark: change segment line type, color and width:
-scale_linetype_manual(values=c("twodash", "dotted")) #("solid", "twodash", "dotted", "longdash", "dotdash", "dashed", "blank")
-scale_color_manual(values=c('#999999','#E69F00'))
-scale_size_manual(values=c(1, 1.5))
ggpubr::ggarrange(p1, p2, ..., ncol = )
: plot multiple ggplots in one figure- Or
gridExtra::grid.arrange(..., ncol = )
- Or
require(cowplot)
= vector("list", n)
myplots <- ggplot(...)
myplot[[i]]plot_grid(plotlist=myplots)
- Or
require(grid)
grid.newpage() #新建图表版面
pushViewport(viewport(layout = grid.layout(2,2))) #将版面分成2*2矩阵
<- function(x,y){viewport(layout.pos.row = x, layout.pos.col = y)}
vplayout print(chart3, vp = vplayout(1,1:2)) #将(1,1)和(1,2)的位置画图chart3
print(chart2, vp = vplayout(2,1)) #将(2,1)的位置画图chart2
print(chart1 , vp = vplayout(2,2)) #将(2,2)的位置画图chart1
scale_y_log10()
: tale log of y axisgeom_hline()
,geom_vline()
,geom_abline
geom_hline(yintercept=25, color="orange", size=1) +
geom_vline(xintercept=3, color="orange", size=1
geom_errorbar()
: boxplot or scatterplot add confidence interval
geom_errorbar(aes(ymin = mean_gain-1.96*sd_gain,
ymax = mean_gain+1.96*sd_gain, colour = Diet), data = ChickError)
forcats::fct_inorder
: Reorder factor levels by first appearance, frequency, or numeric order
= factor(rep(c("Roads", "Canals", "Early railways", "Railways"), each = 3))
a %>% forcats::fct_inorder() %>% levels() a
## [1] "Roads" "Canals" "Early railways" "Railways"