[英]Place strip panels on opposite sides of plot
Some journals require that figures with several graphs have each of the plots lettered.一些期刊要求带有多个图形的图形在每个图形上都标上字母。 I'm trying to do the same with
facet_wrap
in ggplot2
because I've organized the data in such a way that covariates for all my explanatory variables are in one column and the grouping variables for the covariates are in another column.我正在尝试对
facet_wrap
中的ggplot2
做同样的facet_wrap
,因为我以这样一种方式组织数据,即所有解释变量的协变量都在一列中,而协变量的分组变量在另一列中。
I followed the instructions here to give the effect that the x-axis label is different for each plot (when it's really a panel).我按照这里的说明给出了每个图的 x 轴标签不同的效果(当它真的是一个面板时)。
But I still need labels for each plot (column figure.letters
) in the figure, preferably in the top-left hand corner.但我仍然需要图中每个图(列
figure.letters
)的标签,最好在左上角。 I've attempted to add it in the facet_wrap
formula, so now each plot has two strip panels.我试图将它添加到
facet_wrap
公式中,所以现在每个图都有两个条形面板。
require(ggplot2)
my.df <- data.frame(
param = rep(c('Distance (km)', 'Energy (j)', 'Girth (m)', 'Height (cm)', 'Incline (degrees)'), each = 20),
explanatory = rnorm(100, 0, 1),
response = rnorm(100, 0, 1),
figure.letter = rep(LETTERS[1:5], each = 20)
)
ggplot(my.df, aes(x = explanatory, y = response)) +
geom_point() +
facet_wrap(. ~ param + figure.letter, strip.position = 'bottom') +
theme_bw() +
theme(
strip.placement = 'bottom',
strip.background = element_blank()
)
There are a lot of really good answers here , but they're more geared towards moving all the strip panels to the top and then left-justifying the panel text.有很多真正好的答案在这里,但他们更多的面向移动对所有的条板的顶部,然后左对齐面板文本。 I'm looking to separate the two strip panels so that the figure letter panel sits on top and is left-justified while the
param
strip panel remains at the bottom.我希望将两个条形面板分开,以便数字字母面板位于顶部并左对齐,而
param
条形面板保留在底部。
I'm also hoping to avoid writing code for several plots and then use patchwork
to get the figure labels.我也希望避免为多个图编写代码,然后使用
patchwork
的图形标签。
Is there a way to take the strip panel for the figure.letter
and move it to the top while keeping the strip panel for param
at the bottom?有没有办法将
figure.letter
的条形面板移到顶部,同时将param
的条形面板保持在底部?
You can use geom_text()
to place labels outside the plot area if you turn off clipping in coord_cartesian()
and use the same fixed axis limits for all facets.如果您在
coord_cartesian()
关闭裁剪coord_cartesian()
所有方面使用相同的固定轴限制,则可以使用geom_text()
将标签放置在绘图区域之外。
If you have different axis limits for different facets, then the patchwork route will likely be your only option.如果您对不同方面有不同的轴限制,那么拼凑路线可能是您唯一的选择。
require(ggplot2)
#> Loading required package: ggplot2
set.seed(1234)
my.df <- data.frame(
param = rep(c('Distance (km)', 'Energy (j)', 'Girth (m)', 'Height (cm)', 'Incline (degrees)'), each = 20),
explanatory = rnorm(100, 0, 1),
response = rnorm(100, 0, 1)
)
df.label <- data.frame(
param = unique(my.df$param),
x = -2.8,
y = 3.6,
figure.letter = LETTERS[1:5]
)
ggplot(my.df, aes(x = explanatory, y = response)) +
geom_point() +
geom_text(
data = df.label, aes(x = x, y = y, label = figure.letter),
hjust = 0, vjust = 0
) +
facet_wrap(. ~ param, strip.position = 'bottom') +
coord_cartesian(
xlim = c(-2.8, 2.8),
ylim = c(-3.3, 3.3),
expand = FALSE,
clip = "off"
) +
theme_bw() +
theme(
strip.placement = 'bottom',
strip.background = element_blank(),
plot.margin = margin(16.5, 5.5, 5.5, 5.5))
#> Warning: Suppressing axis rendering when strip.position = 'bottom' and
#> strip.placement == 'outside'
Created on 2020-01-05 by the reprex package (v0.3.0)由reprex 包(v0.3.0) 于 2020 年 1 月 5 日创建
Throwing my hat into the ring for my own question, combining the scenario @Claus brought up where the x-axis could possibly have different limits (thus using patchwork) and @camille suggestion of splitting data and then using cowplot.将我的帽子扔进戒指中以解决我自己的问题,结合@Claus 提出的场景,其中 x 轴可能具有不同的限制(因此使用拼凑)和@camille 建议拆分数据,然后使用牛图。
UPDATE Now includes an additional suggestion by @Claus to use the argument plotlist
in plot_grid
to plot a list of graphs.现在,更新包括由@Claus额外的建议,使用参数
plotlist
在plot_grid
绘制图表的列表。
#== Required Packages
require(ggplot2)
require(cowplot)
#== Make a Dataset
my.df <- data.frame(
param = rep(c('Distance (km)', 'Energy (j)', 'Girth (m)', 'Height (cm)', 'Incline (degrees)'), each = 20),
explanatory = c(rnorm(20, 0, 1), rnorm(20, 40, 30),
rnorm(20, -5, 0.5), rnorm(20, 100, 300),
rnorm(20, 0.1, 0.02)),
response = rnorm(100, 0, 1)
)
#== Split, Plot, and Save
my.list <- lapply(split(my.df, my.df$param), function(x){
ggplot(x, aes(x = explanatory, y = response)) +
xlab(unique(x$param)) +
geom_point() +
theme_bw() +
theme(
strip.placement = 'bottom',
strip.background = element_blank()
)
})
#== Plot Grid
plot_grid(plotlist = my.list, labels = 'AUTO')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.