- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个共享 y 轴但具有不同 x 轴的图。我使用 facet_grid 将它们适本地分开(见图),但是两个 x 轴需要有不同的标题(而不是一个标题“Num Cell Lines.Tissue”)。我已经看到通过首先创建 2 个 ggplot 对象然后使用像 ggplotGrob 这样的函数来完成类似的事情。有没有办法直接做到这一点?
编辑 1:包含数据、代码
数据:
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")
最佳答案
我们可以使用 switch = 'x'
然后放置 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())
V1
&
V2
分别然后使用
cowplot::plot_grid
合并在一起功能:
# 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')
关于r - ggplot : How to create different x-axis titles with facet_grid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49092604/
我有一个大致如下所示的数据集: names = tibble(NAME_2=c("Location1","Location2","Location3","Location4")) dates = ti
问题是什么? ggplot2 中的每个方面可能不包含“名称”列中的所有因子水平,在这种情况下,我希望该方面忽略这些因子水平以避免图 block 中的间隙。 我尝试了什么 我尝试在 中添加和删除 sca
我想使用分面网格只显示右边距而不是底部边距。 示例代码 library(ggplot2) qplot(mpg, wt, data=mtcars) + facet_grid(cyl ~ vs, marg
我刚刚在 ggplot2 0.9 中遇到了 facet_grid() 的奇怪行为,我想知道是否有人可以向我解释一下...... 采用以下 df 数据框: var <- sample(c("red",
我有以下数据框: Date K Row Col Downward_solar_radiation Solar_radiation_calc 1 2014-01-02 1 1 1
这是 this question 的变体和 this question我还没有找到解决方案。 以mtcars为例,我想要一个类似下面的情节 ggplot(mtcars, aes(x=cyl,y=mpg
我想在一组分面网格箱线图的每一行中添加一条唯一的水平线。 x<-c("species_X","species_X","species_X", "species_Y","species_Y",
我希望有人可以帮助解决我遇到的这个绘图问题。资料可查here . 基本上,我想为我测试的 4 个模型绘制一条线(平均值)及其相关的置信区间(下、上)。我想对 Cat_Auth 变量进行分面,该变量有
我很难为我的 2 个不同的 facet_grids 获得不同的颜色。 这是我的简单示例: df <- data.frame(x = rep(1:20, each=20), y= rep(1:20, 2
我想从多面 ggplot2 图中有选择地删除不必要的方面。我看过这个问题,但不知道怎么做(也许那里的建议现在已经过时了): adding empty graphs to facet_wrap in g
我正在寻找一种使用 facet_grid 进行绘图的方法在 ggplot2只显示几个选定的方面。说我有以下情节: 例如,一直在寻找一种快速的方法来绘制方面 1 和 3。 #data y<-1:12 x
我有几个关于 ggplot2 分面的问题... 假设我有一个查询返回如下所示的数据: (请注意,它是按 Rank asc 排序的,Alarm asc 和两个 Alarms 的 Rank 为 3,因为它
我有一个如下所示的数据框: Direction ID RY Value Part_of_prog North 1 2016 29 0 No
我想同时显示右侧和底部边距,但不是两者的组合。这可能与 facet_grid 吗? library(ggplot2) ggplot(mtcars, aes(x = hp, group = vs)) +
在我下面的情节中,我想知道如何保持一切不变,但只是删除空的情节? library(tidyverse) hsb <- read.csv('https://raw.githubusercontent.c
以下示例创建了一个 ggplot与 4 个面板“A”、“B”、“C”、“D”排成一排。 我想出了如何在一列中绘制这 4 个面板。然而,仍然是个谜是如何排列 4 个面板,使“A”和“B”在第一行,而“C
我想要的是删除右侧的那些标签,即侧面灰色框上的标签。我举个例子: p <- ggplot(mtcars, aes(mpg, wt, col=factor(cyl))) + geom_point() p
我想使用 facet_grid 在彼此之上绘制一些条形图: library(ggplot2) df % summarise(cty = mean(cty), hwy = mean(hwy)) %>
我正在使用 中的“提示”数据集ggplot2 .如果我做 sp = ggplot(tips,aes(x=total_bill, y = tip/total_bill)) + geom_
在 ggplot , 当使用 facet_grid(..., space = "free_y")并且组内的点数很少,分面标题被切断。 例如... library(tidyverse) d pg Ta
我是一名优秀的程序员,十分优秀!