ggplot:如何使用facet_grid创建不同的x轴标题 [英] ggplot: How to create different x-axis titles with facet_grid

查看:113
本文介绍了ggplot:如何使用facet_grid创建不同的x轴标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个共享y轴但具有不同x轴的图.我使用facet_grid适当地将它们分开(见图),但是两个x轴需要具有不同的标题(而不是一个标题"Num Cell Lines.Tissue").我已经看到了类似的事情,首先创建2个ggplot对象,然后使用ggplotGrob之类的函数.有没有办法直接做到这一点?

I have two plots that share the y-axis but have different x-axes. I used facet_grid to separate them appropriately (see figure), but the two x-axes need to have different titles (instead of the one title "Num Cell Lines.Tissue"). I've seen something similar done by creating 2 ggplot objects first then use a function like ggplotGrob. Is there a way to do this directly?

以包含数据,代码

数据:

Variable  Condition  Num.CellLines.Tissue   Percent.Altered
V1        C1                 1              0.20149254
V1        C1                 2              0.03731343
V1        C1                 3              0
V1        C1                 4              0
V1        C1                 5              0
V2        C2                 1              0.74893617
V2        C2                 2              0.37446809
V2        C2                 3              0.16595745
V2        C2                 4              0.09787234
V2        C2                 5              0.06808511
V2        C2                 6              0.05531915
V2        C2                 7              0.02553191
V2        C2                 8              0.01702128
V2        C2                 9              0.01276596
V2        C2                10              0.00851064
V2        C3                 1              0.88554217
V2        C3                 2              0.68072289
V2        C3                 3              0.40361446
V2        C3                 4              0.22289157
V2        C3                 5              0.11445783
V2        C3                 6              0.06626506
V2        C3                 7              0.04819277
V2        C3                 8              0.01807229
V2        C3                 9              0.01807229
V2        C3                10              0.01204819
V2        C4                 1              0.87301587
V2        C4                 2              0.6984127
V2        C4                 3              0.52380952
V2        C4                 4              0.38095238
V2        C4                 5              0.25925926
V2        C4                 6              0.14285714
V2        C4                 7              0.07407407
V2        C4                 8              0.04232804
V2        C4                 9              0.03703704
V2        C4                10              0.03174603

代码:

ggplot(data, aes(y=Percent.Altered, x = Num.CellLines.Tissue, color= Condition )) + geom_line(size=1) + facet_grid(. ~ Variable, scales="free_x")

理想图的图像

默认情况下,标签V1和V2以我希望它们显示的方式显示,并且与它们下面的x轴标题不同.

The labels V1 and V2 show up the way I want them to by default, and they are different from what the titles of the x-axes below them should be.

推荐答案

我们可以使用 switch ='x'然后放置 strip.placement ="outside"

We can use switch = 'x' then place strip.placement = "outside"

library(tidyverse)

text = "Variable  Condition  Num.CellLines.Tissue   Percent.Altered
V1        C1                 1              0.20149254
V1        C1                 2              0.03731343
V1        C1                 3              0
V1        C1                 4              0
V1        C1                 5              0
V2        C2                 1              0.74893617
V2        C2                 2              0.37446809
V2        C2                 3              0.16595745
V2        C2                 4              0.09787234
V2        C2                 5              0.06808511
V2        C2                 6              0.05531915
V2        C2                 7              0.02553191
V2        C2                 8              0.01702128
V2        C2                 9              0.01276596
V2        C2                10              0.00851064
V2        C3                 1              0.88554217
V2        C3                 2              0.68072289
V2        C3                 3              0.40361446
V2        C3                 4              0.22289157
V2        C3                 5              0.11445783
V2        C3                 6              0.06626506
V2        C3                 7              0.04819277
V2        C3                 8              0.01807229
V2        C3                 9              0.01807229
V2        C3                10              0.01204819
V2        C4                 1              0.87301587
V2        C4                 2              0.6984127
V2        C4                 3              0.52380952
V2        C4                 4              0.38095238
V2        C4                 5              0.25925926
V2        C4                 6              0.14285714
V2        C4                 7              0.07407407
V2        C4                 8              0.04232804
V2        C4                 9              0.03703704
V2        C4                10              0.03174603"

data <- read.table(text = text, header = TRUE)
head(data)
#>   Variable Condition Num.CellLines.Tissue Percent.Altered
#> 1       V1        C1                    1      0.20149254
#> 2       V1        C1                    2      0.03731343
#> 3       V1        C1                    3      0.00000000
#> 4       V1        C1                    4      0.00000000
#> 5       V1        C1                    5      0.00000000
#> 6       V2        C2                    1      0.74893617

ggplot(data, aes(y = Percent.Altered, x = Num.CellLines.Tissue, color = Condition )) + 
  geom_line(size = 1) + facet_grid(. ~ Variable, scales = "free_x", switch = 'x') +
  theme_classic(base_size = 16) +
  theme(strip.placement = "outside") +
  theme(axis.title.x = element_blank(),
        strip.background = element_blank())

# Append the original x-axis label. Code taken from the ref below 
# http://ggplot2.tidyverse.org/reference/as_labeller.html
appender <- function(string, prefix = "Num.CellLines.Tissue: ") paste0(prefix, string)
ggplot(data, aes(y = Percent.Altered, x = Num.CellLines.Tissue, color = Condition )) + 
  geom_line(size = 1) + 
  facet_grid(. ~ Variable, 
             labeller = as_labeller(appender),
             scales = "free_x", switch = 'x') +
  theme_classic(base_size = 16) +
  theme(strip.placement = "outside") +
  theme(axis.title.x = element_blank(),
        strip.background = element_blank())  

根据OP的新图片进行编辑,我们需要绘制 V1 & V2 分开,然后使用 cowplot :: plot_grid 函数合并在一起:

Edit based on OP's new picture, we need to plot V1 & V2 separately then merge together using cowplot::plot_grid function:

# V1
p1 <- ggplot(data %>% filter(Variable == "V1"), 
             aes(y = Percent.Altered, x = Num.CellLines.Tissue, color = Condition )) + 
  geom_line(size = 1) + 
  facet_grid(. ~ Variable, scales = "free_x") +
  scale_y_continuous(limits = c(0, 1), expand = c(0, 0)) +
  theme_classic(base_size = 16) +
  theme(
    legend.position = "none", 
    plot.margin = unit(c(0, 0, 0, 0), "cm")) +
  scale_color_brewer(palette = "Set1") +
  xlab("# Tissues")

# V2
p2 <- ggplot(data %>% filter(Variable == "V2"), 
             aes(y = Percent.Altered, x = Num.CellLines.Tissue, color = Condition )) + 
  geom_line(size = 1) + 
  facet_grid(. ~ Variable, scales = "free_x") +
  scale_y_continuous(limits = c(0, 1), expand = c(0, 0)) +
  theme_classic(base_size = 16) +
  theme(
    legend.position = "none", 
    plot.margin = unit(c(0, 0, 0, 0), "cm")) +
  scale_color_brewer(palette = "Dark2") +
  xlab("# Cell Lines") + ylab("")

# Remove the y-axis for the 2nd plot - p2 then merge 2 plots
cowplot::plot_grid(p1, 
                   p2 + 
                     theme(axis.text.y = element_blank(),
                           axis.line.y = element_blank(),
                           axis.title.y= element_blank(),
                           axis.ticks.y= element_blank()),
                   nrow = 1,
                   rel_widths = c(1.2, 1),
                   align = 'h', axis = 'tb')

reprex程序包(v0.2.0)创建于2018-03-04.

Created on 2018-03-04 by the reprex package (v0.2.0).

这篇关于ggplot:如何使用facet_grid创建不同的x轴标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆