分面

回复:0 | 查看:412 | admin 发表于 2018-3-4 17:32:12 |阅读模式 |复制链接
We will use the tips dataset from the reshape2 package.
library(reshape2)# Look at first few rowshead(tips)#>   total_bill  tip    sex smoker day   time size#> 1      16.99 1.01 Female     No Sun Dinner    2#> 2      10.34 1.66   Male     No Sun Dinner    3#> 3      21.01 3.50   Male     No Sun Dinner    3#> 4      23.68 3.31   Male     No Sun Dinner    2#> 5      24.59 3.61 Female     No Sun Dinner    4#> 6      25.29 4.71   Male     No Sun Dinner    4
This is a scatterplot of the tip percentage by total bill size.
library(ggplot2)sp <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1)sp
facet_grid
The data can be split up by one or two variables that vary on the horizontal and/or vertical direction.
This is done by giving a formula to facet_grid(), of the form vertical ~ horizontal.
# Divide by levels of "sex", in the vertical directionsp + facet_grid(sex ~ .)
# Divide by levels of "sex", in the horizontal directionsp + facet_grid(. ~ sex)
# Divide with "sex" vertical, "day" horizontalsp + facet_grid(sex ~ day)# Divide by day, going horizontally and wrapping with 2 columnssp + facet_wrap( ~ day, ncol=2)
sp + facet_grid(sex ~ day) +    theme(strip.text.x = element_text(size=8, angle=75),          strip.text.y = element_text(size=12, face="bold"),          strip.background = element_rect(colour="red", fill="#CCCCFF"))

library(reshape2)# Look at first few rowshead(tips)#>   total_bill  tip    sex smoker day   time size#> 1      16.99 1.01 Female     No Sun Dinner    2#> 2      10.34 1.66   Male     No Sun Dinner    3#> 3      21.01 3.50   Male     No Sun Dinner    3#> 4      23.68 3.31   Male     No Sun Dinner    2#> 5      24.59 3.61 Female     No Sun Dinner    4#> 6      25.29 4.71   Male     No Sun Dinner    4
This is a scatterplot of the tip percentage by total bill size.
#> 2      10.34 1.66   Male     No Sun Dinner    3
#> 3      21.01 3.50   Male     No Sun Dinner    3
#> 4      23.68 3.31   Male     No Sun Dinner    2
#> 5      24.59 3.61 Female     No Sun Dinner    4
#> 6      25.29 4.71   Male     No Sun Dinner    4
#> 2      10.34 1.66   Male     No Sun Dinner    3
#> 3      21.01 3.50   Male     No Sun Dinner    3
#> 4      23.68 3.31   Male     No Sun Dinner    2
#> 5      24.59 3.61 Female     No Sun Dinner    4
#> 6      25.29 4.71   Male     No Sun Dinner    4

ibrary(reshape2)
# Look at first few rows
head(tips)
#>   total_bill  tip    sex smoker day   time size
#> 1      16.99 1.01 Female     No Sun Dinner    2
#> 2      10.34 1.66   Male     No Sun Dinner    3
#> 3      21.01 3.50   Male     No Sun Dinner    3
#> 4      23.68 3.31   Male     No Sun Dinner    2
#> 5      24.59 3.61 Female     No Sun Dinner    4
#> 6      25.29 4.71   Male     No Sun Dinner    4
This is a scatterplot of the tip percentage by total bill size.

# The mtcars dataset:
data=as.matrix(mtcars)
head(data)
# Default Heatmap (left)
heatmap(data)

# Use 'scale' to normalize (right)
heatmap(data, scale="column")

Instead of faceting with a variable in the horizontal or vertical direction, facets can be placed next to each other, wrapping with a certain number of columns or rows. The label for each plot will be at the top of the plot.

# Divide by day, going horizontally and wrapping with 2 columns
sp + facet_wrap( ~ day, ncol=2)
plot of chunk unnamed-chunk-7

Modifying facet label appearance
sp + facet_grid(sex ~ day) +
    theme(strip.text.x = element_text(size=8, angle=75),
          strip.text.y = element_text(size=12, face="bold"),
          strip.background = element_rect(colour="red", fill="#CCCCFF"))

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表