ggplot画图

1. 主题设置

1.1 一套常用主题

mytheme <- theme(panel.grid.major = element_line(colour = "grey85", size = 0.25), 
                 panel.grid.minor = element_line(colour = "grey85", size = 0.25),
                 panel.background = element_blank(),
                 panel.border = element_rect(fill=NA,color = "black"),
                 plot.title = element_text(hjust = 0.5),
                 plot.subtitle = element_text(hjust = 0.5),
                 plot.caption = element_text(),
                 legend.position = "bottom",
                 legend.key = element_blank(),
                 strip.background = element_rect(color = "black"))
复制代码

1.2 画板(Panel)

# 网格线(grid)
panel.grid.major = element_line(colour = "grey85", size = 0.25) # 主要网格线
panel.grid.minor = element_line(colour = "grey85", size = 0.25) # 次要网格线
# 无网格线
panel.grid = element_blank()

# 背景:建议用空白
panel.background = element_blank()

# 边界:建议用黑色
panel.border = element_rect(fill=NA,color = "black")
复制代码

1.3 条带(Strip)

# 边界颜色(color)建议用黑色
# 填充色(fill)建议用默认的灰色,或设为白色
strip.background = element_rect(color = "black")
strip.background.x = element_rect(color = "black",fill = "white")
strip.background.y = element_rect(color = "black")

# 位置:建议用inside(默认,不用特别设置)
strip.placement = "outside"
复制代码

1.4 图例(Legend)

# 位置:建议放底部
legend.position = "bottom"

# 图标:建议设为空白
legend.key = element_blank()

# 背景:建议不设(默认),或边界用黑色
legend.background = element_rect(color = "black")
复制代码

1.5 字体字号

# 字体设置
windowsFonts(Times=windowsFont("Times New Roman")) # 将Windows系统中的某字体赋值给Times

# 统一设置:使用text(),各元素也可在各个地方分别设置
text = element_text(family="Times", face="bold", size=12)

# 标题
plot.title = element_text(hjust = 0.5) # 标题居中
plot.subtitle = element_text(hjust = 0.5) # 副标题
plot.caption = element_text() # 说明文字

# 各元素
axis.title.x = element_text(size = 10)
strip.text.y = element_text(size = 10)
复制代码