我有一个图表,其中有4组(条1 + 2,条3 + 4等),但数据集中没有这方面的迹象。如何手动添加空间,添加共享组标签(x轴)并重新着色4组?
数据(融化):
Curent Ggplot代码:
xdr<-melt(result)
ggplot(
aes(x = variable, y = value), data = xdr) +
stat_summary(fun.y = "mean", geom = "bar") +
coord_cartesian(ylim=c(0.6,0.85)) +
stat_summary(fun.y = mean, geom = "bar") +
stat_summary(fun.data = mean_se, geom = "errorbar")
答案 0 :(得分:1)
我认为您希望使用mutate()
以适合您的数据的方式添加组,然后facet_wrap()
来制作您正在讨论的子图。
library(tidyverse)
df <- tribble(
~variable, ~value,
"baseline1", 0.730,
"baseline2", 0.521,
"baseline3", 0.762,
"baseline4", 0.655,
"baseline5", 0.604,
"baseline6", 0.710,
"baseline7", 0.528,
"baseline8", 0.172
)
df %>%
mutate(group = (row_number() + 1) %/% 2,
group = paste("Group", group)) %>%
ggplot(aes(variable, value, fill = group)) +
geom_col(show.legend = FALSE) +
facet_wrap(~group, nrow = 1, scales = "free_x")