- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题在这里已经有了答案:
Nested facets in ggplot2 spanning groups
(2 个回答)
去年关闭。
我在 ggplot
中创建了一个图表里面有两个变量 facet_grid
.
我希望每个方面的标题仅在该方面的中心重复一次。
例如,第一个原始(上面)中的 0 和 1 将只出现一次并且出现在中间。
在我原来的情节中,每个方面的情节数量不相等。因此,使用 patchwork
将两个图拼凑在一起/cowplot
/ggpubr
效果不佳。
我更喜欢只使用 ggplot
的解决方案/hack .
样本数据:
df <- head(mtcars, 5)
df %>%
ggplot(aes(gear, disp)) +
geom_bar(stat = "identity") +
facet_grid(~am + carb,
space = "free_x",
scales = "free_x") +
ggplot2::theme(
panel.spacing.x = unit(0,"cm"),
axis.ticks.length=unit(.25, "cm"),
strip.placement = "outside",
legend.position = "top",
legend.justification = "center",
legend.direction = "horizontal",
legend.key.size = ggplot2::unit(1.5, "lines"),
# switch off the rectangle around symbols
legend.key = ggplot2::element_blank(),
legend.key.width = grid::unit(2, "lines"),
# # facet titles
strip.background = ggplot2::element_rect(
colour = "black",
fill = "white"),
panel.background = ggplot2::element_rect(
colour = "white",
fill = "white"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
structure(list(par = c("Par1", "Par1", "Par1", "Par1", "Par1",
"Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1",
"Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1",
"Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par2", "Par2",
"Par2"), channel_1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 1L, 1L, 1L), .Label = c("Center", "Left \nFrontal",
"Left \nFrontal Central", "Left \nCentral Parietal", "Left \nParietal Ooccipital",
"Left", "Right \nFrontal", "Right \nFrontal Central", "Right \nCentral Parietal",
"Right \nParietal Ooccipital", "Right"), class = "factor"), freq = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L,
3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Alpha",
"Beta", "Gamma"), class = "factor"), group = c("a", "b", "c",
"a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a",
"b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b",
"c"), m = c(0.488630500442935, 0.548666228768508, 0.0441536349332613,
0.304475866391531, 0.330039488441422, 0.0980622573307064, 0.0963996979198171,
0.301679466108907, 0.240618782227119, 0.35779695722622, 0.156116647839907,
0.0274546218676152, 0.0752501569920047, 0.289342864254614, 0.770518960576786,
0.548130676907356, 0.180158614358946, 0.238520826021687, 0.406326198917495,
0.159739769132509, 0.140739952534666, 0.295427640977557, 0.106130817023844,
0.214006898241167, 0.31081727835652, 0.366982521446529, 0.264432086988446,
0.0761271112139142, 0.0811642772125171, 0.0700455890939194),
se = c(0.00919040825504951, 0.00664655073810519, 0.0095517721611042,
0.00657090455386036, 0.00451135146762504, 0.0188625074573698,
0.00875378313351897, 0.000569521129673224, 0.00691447732630984,
0.000241814142091401, 0.0124584589176995, 0.00366855139256551,
0.0072981677277562, 0.0160663614099261, 0.00359337442316408,
0.00919725279757502, 0.040856967817406, 0.00240910563984416,
0.0152236046767608, 0.00765487375180611, 0.00354140237391633,
0.00145468584619171, 0.0185141245423404, 0.000833307847848054,
0.0038193622895167, 0.0206130436440409, 0.0066911922721337,
7.3079999953491e-05, 0.0246233416039572, 0.00328150956514463
)), row.names = c(NA, -30L), class = c("tbl_df", "tbl", "data.frame"
))
df %>%
ggplot(aes(channel_1, m,
group = group,
fill = group,
color = group)) +
facet_grid(~par + freq,
space="free_x",
scales="free_x") +
geom_errorbar(
aes(min = m - se, ymax = m + se, alpha = 0.01),
width = 0.2, size = 2, color = "black",
position = position_dodge(width = 0.6)) +
geom_bar(stat = "identity",
position = position_dodge(width = 0.6),
# color = "black",
# fill = "white",
width = 0.6,
size = 2, aes(alpha = 0.01)) +
scale_shape_manual(values = c(1, 8, 5)) +
labs(
color = "",
fill = "",
shape = "") +
guides(
color = FALSE,
shape = FALSE) +
scale_alpha(guide = "none")
最佳答案
最快的黑客:用情节伪造方面并结合。这需要一些黑客攻击,但它可能仍然比搞乱 grobs 更少黑客攻击:
library(patchwork)
library(tidyverse)
df <- head(mtcars,5)
df <- df %>% mutate(am_carb = factor(paste(am,carb,sep = '_'),
labels = c( ' 1','2','1','4')))
##note!! the blank space in ' 1' label is on purpose!!! this is to make those labels unique, otherwise it would consider both '1' the same category!!
p1 <-
df %>%
ggplot(aes(gear, disp)) +
geom_bar(stat = "identity") +
facet_grid(~am_carb, scales = "free_x") +
theme(panel.spacing.x = unit(0,"cm"),
plot.margin = margin(t = -2),
strip.background = element_rect(colour = "black",fill = "white"),
panel.background = element_rect(colour = "white", fill = "white"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
p2 <-
df %>%
ggplot(aes(gear, disp)) +
geom_blank() +
facet_grid(~ am, scales = "free_x") +
theme(panel.spacing.x = unit(0,"cm"),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
plot.margin = margin(b = -2),
strip.background = element_rect(colour = "black",fill = "white"),
panel.background = element_rect(colour = "white", fill = "white"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
p2/p1 + plot_layout(heights = c(0.1,100) )
cowplot
.
mydat <- structure(list(par = c("Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par1", "Par2", "Par2", "Par2"), channel_1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 1L, 1L, 1L), .Label = c("Center", "Left \nFrontal", "Left \nFrontal Central", "Left \nCentral Parietal", "Left \nParietal Ooccipital", "Left", "Right \nFrontal", "Right \nFrontal Central", "Right \nCentral Parietal", "Right \nParietal Ooccipital", "Right"), class = "factor"), freq = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Alpha", "Beta", "Gamma"), class = "factor"), group = c("a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c"), m = c(0.488630500442935, 0.548666228768508, 0.0441536349332613, 0.304475866391531, 0.330039488441422, 0.0980622573307064, 0.0963996979198171, 0.301679466108907, 0.240618782227119, 0.35779695722622, 0.156116647839907, 0.0274546218676152, 0.0752501569920047, 0.289342864254614, 0.770518960576786, 0.548130676907356, 0.180158614358946, 0.238520826021687, 0.406326198917495, 0.159739769132509, 0.140739952534666, 0.295427640977557, 0.106130817023844, 0.214006898241167, 0.31081727835652, 0.366982521446529, 0.264432086988446, 0.0761271112139142, 0.0811642772125171, 0.0700455890939194), se = c(0.00919040825504951, 0.00664655073810519, 0.0095517721611042, 0.00657090455386036, 0.00451135146762504, 0.0188625074573698, 0.00875378313351897, 0.000569521129673224, 0.00691447732630984, 0.000241814142091401, 0.0124584589176995, 0.00366855139256551, 0.0072981677277562, 0.0160663614099261, 0.00359337442316408, 0.00919725279757502, 0.040856967817406, 0.00240910563984416, 0.0152236046767608, 0.00765487375180611, 0.00354140237391633, 0.00145468584619171, 0.0185141245423404, 0.000833307847848054, 0.0038193622895167, 0.0206130436440409, 0.0066911922721337, 7.3079999953491e-05, 0.0246233416039572, 0.00328150956514463)), row.names = c(NA, -30L), class = c("tbl_df", "tbl", "data.frame"))
library(tidyverse)
library(cowplot)
#>
#> ********************************************************
#> Note: As of version 1.0.0, cowplot does not change the
#> default ggplot2 theme anymore. To recover the previous
#> behavior, execute:
#> theme_set(theme_cowplot())
#> ********************************************************
mydat <- mydat %>% mutate(par_freq = factor(paste(par,freq,sep = '_'), labels = c('Alpha', 'Beta', 'Gamma', 'Gamma ' )))
p1 <-
mydat %>%
ggplot(aes(channel_1, m, group = group, fill = group, color = group)) +
geom_bar(stat = "identity") +
facet_grid( ~ par_freq, scales = "free_x", space="free_x") +
theme(panel.spacing.x = unit(0,"cm"),
plot.margin = margin(t = -2),
strip.background = element_rect(colour = "black",fill = "white"),
panel.background = element_rect(colour = "white", fill = "white"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = 'none')
p2 <-
mydat %>%
ggplot(aes(channel_1, m, group = group, fill = group, color = group)) +
geom_blank() +
facet_grid(~ par) +
theme(panel.spacing.x = unit(0,"cm"),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
plot.margin = margin(b = -2),
strip.background = element_rect(colour = "black",fill = "white"),
panel.background = element_rect(colour = "white", fill = "white"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
gt <- cowplot::as_gtable(p2)
gt$widths[5] <- 8*gt$widths[7]
cowplot::plot_grid(gt, p1, align = "v", axis = 'l',nrow = 2, rel_heights = c(5, 100))
# you need to play around with the values unfortunately.
ggnomics
,有一个更简单的解决方案。包 - 见我的第二个答案
p_demo <- ggplot(mydat, aes(channel_1, m)) +
geom_bar(stat = "identity") +
facet_grid(~par +freq , space = "free_x", scales = "free_x") +
theme(panel.spacing.x = unit(0,"cm"))
gt <- cowplot::as_gtable(p_demo)
gtable::gtable_show_layout(gt)
关于r - ggplot - 集中 facet_grid 标题并且只出现一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60822398/
我有一个大致如下所示的数据集: 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
我是一名优秀的程序员,十分优秀!