我想将两个ggplots组合成一个,其中barplot位于各自的箱形图上,右侧有自己的y轴,左侧是箱形图的y轴。
我有以下数据框:
structure(list(freq = c(2.54, 2.54, 2.54, 2.54, 2.54, 2.54, 2.54,
2.54, 60.8409464345143, 39.1122842039091, 39.3877005636308, 45.0499754193691,
45.0499754193691, 39.3877005636308, 58.6865904649176, 44.3132344906534,
2.54, 45.9443299031539, 44.1265627115877, 42.8169336096051, 45.9027344014645,
59.0311448241387, 45.9443299031539, 42.8169336096051, 32.7180795716837,
33.1946366747656, 36.7737018836392, 2.54, 40.7656895507421, 32.7180795716837,
48.1371372899188, 57.6061315721649, 2.54, 2.54, 2.54, 2.54, 2.54,
2.54, 2.54, 2.54, 2.54, 36.1429860851149, 78.0906567798543, 2.54,
2.54, 2.54, 42.2476486624554, 57.0057255417609, 2.54, 2.54, 2.54,
2.54, 2.54, 28.8418507777101, 2.54, 2.54), group = c(1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4), type = c("A", "A", "A", "A",
"A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "A",
"A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B",
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "A", "A", "A",
"A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B"
), key = c("X", "X", "X", "X", "Y", "Y", "Y", "Y", "X", "X",
"X", "X", "Y", "Y", "Y", "Y", "X", "X", "X", "X", "Y", "Y", "Y",
"Y", "X", "X", "X", "X", "Y", "Y", "Y", "Y", "X", "X", "X", "X",
"Y", "Y", "Y", "Y", "X", "X", "X", "X", "Y", "Y", "Y", "Y", "X",
"X", "X", "X", "Y", "Y", "Y", "Y"), value = c(5.69351214582135,
6.88058004816183, 9.35770000434295, 6.78367036087353, 1.3577,
1.3577, 1.3577, 1.3577, 8.69682006516347, 9.56181998537027, 8.68471819806912,
7.29555259435988, 3.88187967830076, 4.05753772586725, 5.52406103174451,
4.53408069224327, 5.88059177391937, 6.4826419938047, 6.70665549257897,
6.88058004816183, 4.05753772586725, 4.18225969457391, 3.58214673033626,
3.58214673033626, 8.72876788075235, 8.61417723030231, 9.24419073081267,
7.75273519291228, 4.18225969457391, 3.58214673033626, 4.42501888259803,
4.7838315621743, 9.9597599924137, 5.92198327467201, 5.32778330568161,
7.37888971268353, 1.3577, 1.3577, 1.3577, 1.3577, 5.9597708485543,
7.97748886251906, 8.77267336467445, 7.78906392500877, 1.3577,
1.3577, 3.58214673033626, 4.69369249928824, 6.25165215170156,
6.70340234980225, 5.18167388023128, 8.63645362381043, 1.3577,
3.58214673033626, 1.3577, 1.3577)), class = "data.frame", .Names = c("freq",
"group", "type", "key", "value"), row.names = c(NA, -56L))
我为第2号地图创建了以下小数据框:
library(dplyr)
df2 <- test %>%
group_by(group, type) %>%
mutate(freq_mean = as.character(mean(freq))) %>%
select(group,type,freq_mean) %>%
group_by(group,type,freq_mean) %>%
summarise_all(funs(sum)) %>%
mutate(freq_mean = as.numeric(freq_mean))
我创建了以下图表:
p1 <- ggplot(test, aes(factor(group), value, fill = factor(key)))+
geom_boxplot()+
theme_classic()+
theme(axis.title.x = element_blank(),
axis.ticks.x = )+
facet_grid(~type)
p2 <- ggplot(df2, aes(factor(group), freq_mean))+
geom_col(alpha = 0.1)+
theme_classic()+
theme(axis.title.x = element_blank(),
axis.ticks.x = )+
facet_grid(~type)
请注意,我希望它们重叠并创建单个图,而不是刻面或将两个图放在彼此的上方/下方。像这样:
除了这里,两个图共享相同的y轴,压扁箱图。是否有可能使条形图在右边有自己的y轴?