我想做两件事:
首先,也是最重要的一点是,要让y轴随outcome_type2变量变化,因为它们的值域都非常不同,所以"C"有自己的值域,"Z"有自己的值域,"SS"有自己的值域。
第二件事是调整列的间距,这样相同的组之间就有一点空间--三个"C"列靠得很近,在它们和"Z"之间有一点额外的空白,"Z"和"SS"之间也是这样,只是为了在这三组之间多加一点区分。
我尝试在outcome_type2而不是outcome_type上修改刻面,但没有效果。
这是当前的基本代码,从技术上讲运行良好,但正如您将看到的,让它们都使用相同的Y轴确实会淹没"Z"和"SS"面板。
ggplot(dtest, aes(x = var2, y = avg2, fill = var2)) +
geom_bar(stat = "identity",
width = 1) +
facet_grid(wave ~ forcats::fct_relevel(outcome_type, "CT", "CI", "CE", "FZ", "MZ", "PSS", "CSS"),
scales = "free_y",
space = "free_y") +
theme_minimal() +
theme(legend.position = "none")
dtest <- structure(list(outcome_type = c("CT", "CT", "CT", "CI", "CI",
"CI", "CE", "CE", "CE", "FZ", "FZ", "MZ", "MZ", "PSS", "PSS",
"CSS", "CSS", "CT", "CT", "CT", "CI", "CI", "CI", "CE", "CE",
"CE", "FZ", "FZ", "MZ", "MZ", "PSS", "PSS", "CSS", "CSS"), wave = c("Wave 1",
"Wave 2", "Wave 3", "Wave 1", "Wave 2", "Wave 3", "Wave 1", "Wave 2",
"Wave 3", "Wave 2", "Wave 3", "Wave 2", "Wave 3", "Wave 1", "Wave 3",
"Wave 1", "Wave 3", "Wave 1", "Wave 2", "Wave 3", "Wave 1", "Wave 2",
"Wave 3", "Wave 1", "Wave 2", "Wave 3", "Wave 2", "Wave 3", "Wave 2",
"Wave 3", "Wave 1", "Wave 3", "Wave 1", "Wave 3"), var2 = c("Skipped",
"Skipped", "Skipped", "Skipped", "Skipped", "Skipped", "Skipped",
"Skipped", "Skipped", "Skipped", "Skipped", "Skipped", "Skipped",
"Skipped", "Skipped", "Skipped", "Skipped", "Attended", "Attended",
"Attended", "Attended", "Attended", "Attended", "Attended", "Attended",
"Attended", "Attended", "Attended", "Attended", "Attended", "Attended",
"Attended", "Attended", "Attended"), avg2 = c(30.21, 20.88, 25.43,
7.68, 8.26, 7.89, 11.15, 8, 5.99, 1.64, 0.43, 0.6, 0.77, 0.01,
-0.09, -0.2, -0.01, 24.01, 19.98, 29.04, 9.82, 12.41, 12.99,
14.35, 11.01, 10, 2.36, 2.3, 1.51, 0.91, -0.23, -0.35, -0.17,
-0.14), outcome_type2 = c("C", "C", "C", "C", "C", "C", "C",
"C", "C", "Z", "Z", "Z", "Z", "SS", "SS", "SS", "SS", "C", "C",
"C", "C", "C", "C", "C", "C", "C", "Z", "Z", "Z", "Z", "SS",
"SS", "SS", "SS")), class = c("spec_tbl_df", "tbl_df", "tbl",
"data.frame"), row.names = c(NA, -34L), spec = structure(list(
cols = list(outcome_type = structure(list(), class = c("collector_character",
"collector")), wave = structure(list(), class = c("collector_character",
"collector")), var2 = structure(list(), class = c("collector_character",
"collector")), avg2 = structure(list(), class = c("collector_double",
"collector")), outcome_type2 = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))
2条答案
按热度按时间vaj7vani1#
一种选择是为每组面板创建单独的绘图,然后使用
patchwork
将它们粘合在一起。这样做可以自动为每组面板获得“自由”缩放,并且每个面板组也有一个(且只有一个)轴。为此,首先添加一个
group
列到您的数据中,它可以用于按面板组分割您的数据集。另外,为了方便起见,我使用了一个绘图函数,它也删除了前两组面板的y轴带文本,作为重要的一步,complete
s每个数据集,以便wave
的所有组合,outcome_type
和var2
存在于每个子数据集中。mrwjdhj32#
这里是一个解决方案,我们首先识别那些avg2〈5,然后制作两个 Dataframe 的列表,并为每个 Dataframe 绘制相应的图: