ggplot2-主题及实用技巧(6)

theme

#准备数据

library(ggplot2)
library(gridExtra)
#theme_bw(),适合科研的主题
##密度图

p <- ggplot(data = dexp) + geom_density(
  aes(Expression, color = Sample)
)


#局部设置

p + theme_bw()


#全局设置

theme_set(theme_bw())


#内置主题

theme_gray <- p + theme_gray() +
  labs(title = "ggplot_theme_gray")

theme_grey <- p + theme_grey() +
  labs(title = "ggplot_theme_grey")

theme_bw <- p + theme_bw() +
  labs(title = "ggplot_theme_bw")

theme_classic <- p + theme_classic() +
  labs(title = "ggplot_theme_classic")

theme_dark <- p + theme_dark() +
  labs(title = "ggplot_theme_dark")

theme_light <- p + theme_light() +
  labs(title = "ggplot_theme_light")

theme_linedraw <- p + theme_linedraw() +
  labs(title = "ggplot_theme_linedraw")

theme_minimal <- p + theme_minimal() +
  labs(title = "ggplot_theme_minimal")

theme_void <- p + theme_void() +
  labs(title = "ggplot_theme_void")


plots <- list(
  theme_gray,
  theme_grey,
  theme_bw,
  theme_classic,
  theme_dark,
  theme_light,
  theme_linedraw,
  theme_minimal,
  theme_void)

mp <- marrangeGrob(grobs = plots, ncol = 2, nrow = 2)
ggsave("themes.pdf", mp)


##美化图形
#三种基础元素:文本,直线,矩形

#准备数据
library(ggplot2)
library(gridExtra)
source("pre_data.R")

#密度图
p1 <- ggplot(data = dexp) + geom_density(
  aes(Expression, color = Sample)
) 

#修改标题
p1 + labs(title = "Density distribution") +
  theme(plot.title = element_text(size = 20, hjust = 0.5))

p1 + labs(title = "Density distribution") +
  
#family:字体样式;hjust:水平对齐;vjust:垂直对齐;
  theme(plot.title = element_text(size = 20, hjust = 0.5))#0,1分别代表左右端


#修改坐标轴

p1 + labs(x="Gene Expression") +
  scale_x_continuous(breaks = seq(0, 2000, 50)) +
  theme(axis.title.x = element_text(face = "italic"),
        axis.text.x = element_text(angle = 50, vjust = 0))
  
#修改图例(legend)
#可以top,“none”

p1 + theme(legend.position = c(0.95, 0.7),
           legend.background = element_rect(fill = "black"))

一页多图

library(ggplot2)
library(dplyr)
library(gridExtra)
source("pre_data.R")

p <- ggplot(data = dexp)

#密度图

p_density <- p + geom_density(
  aes(Expression, color = Sample)
  )

#箱线图

p_boxplot <- p + geom_boxplot(
  aes(x = Sample, y = Expression, color = Group)
)

data_boxplot <- ggplot_build(p_boxplot)$data[[1]]


#小提琴图

p_violin <- p + geom_violin(
  aes(x=Sample, y = Expression, fill = Group)
)

grid.arrange(p_boxplot, p_density, p_violin, ncol=2)

grid.arrange(p_boxplot, p_density, p_violin, ncol=2,
#top指添加标题
             top = "total")

plots <- list(p_density, p_boxplot, p_violin)

#复杂布局

lay1 = rbind(c(1, 2),
             c(3, 3))

grid.arrange(p_boxplot, p_density, p_violin, ncol=2,
             layout_matrix = lay1)

lay2 = rbind(c(1, 3),
             c(2, 3))


grid.arrange(grobs = plots, ncol=2,
             layout_matrix = lay2,
#c(1, 2)代表第一列和第二列
             widths=c(1, 2),
             height = c(1, 0.8))

#网状
#grobs用于添加列表
#arrangeGrob用于生成图片的特定参数
p1 <- arrangeGrob(grobs = plots,
                  layout_matrix = lay1)

p2 <- arrangeGrob(grobs = plots,
                  layout_matrix = lay2)

grid.arrange(p1, p2, ncol =2)

#生成多页

ml <- marrangeGrob(grobs = plots, nrow = 2, ncol = 1)

ggsave("multipage.pdf", ml)

你可能感兴趣的:(ggplot2-主题及实用技巧(6))