美文网首页
R绘图_ggplot2图形参数之分面[10]

R绘图_ggplot2图形参数之分面[10]

作者: 谢俊飞 | 来源:发表于2020-03-22 13:24 被阅读0次
火狐截图_2020-02-11T08-36-22.554Z.png

Faceting: split a plot into a matrix of panels

ggplot2 分面:将图分成面板矩阵

本文教程描述了如何使用ggplot2软件包拆分图。分面方法将图分成面板矩阵。 每个面板显示数据的不同子集
根据代码运行如下:

rm(list = ls())
# Convert dose from numeric to factor variables
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
df <- ToothGrowth
head(df)

library(ggplot2)
bp <- ggplot(df, aes(x=dose, y=len, group=dose)) + 
  geom_boxplot(aes(fill=dose))
bp

# Split in vertical direction
bp + facet_grid(supp ~ .)
# Split in horizontal direction
bp + facet_grid(. ~ supp)

# Facet by two variables: dose and supp.
# Rows are dose and columns are supp
bp + facet_grid(dose ~ supp)
# Facet by two variables: reverse the order of the 2 variables
# Rows are supp and columns are dose
bp + facet_grid(supp ~ dose)
bp + facet_grid(dose ~ supp, margins=TRUE)

bp + facet_grid(dose ~ supp, scales='free')
bp + facet_grid(dose ~ supp, labeller=label_both)

# Change facet text font. Possible values for the font style:
#'plain', 'italic', 'bold', 'bold.italic'.
bp + facet_grid(dose ~ supp)+
  theme(strip.text.x = element_text(size=12, color="red",
                                    face="bold.italic"),
        strip.text.y = element_text(size=12, color="red",
                                    face="bold.italic"))
# Change the apperance of the rectangle around facet label
bp + facet_grid(dose ~ supp)+
  theme(strip.background = element_rect(colour="black", fill="white", 
                                        size=1.5, linetype="solid"))

# 可以使用facet_wrap()函数将小平面并排放置,如下所示:
bp + facet_wrap(~ dose)
bp + facet_wrap(~ dose, ncol=2)

相关文章

网友评论

      本文标题:R绘图_ggplot2图形参数之分面[10]

      本文链接:https://www.haomeiwen.com/subject/mkbmyhtx.html