R语言 两个图重叠(ggplot)

kwvwclae  于 2023-03-05  发布在  其他
关注(0)|答案(1)|浏览(360)

有人能帮我把这两个图合并成一个吗?理想情况下,彩色图应该被阶跃图覆盖,这样“ox_0”的计数就可以显示在彩色图上(以直方图或阶跃的形式)。

我有情节1:

ggplot(subset(stw, !is.na(will)), aes(ox_0, fill=will)) +
    geom_bar(position="fill") +
    ggtitle("Willingness to repeat surgery per Oxford Knee Score at baseline") +
    xlab("Preoperative Oxford Knee Score")+ ylab("")+
    scale_y_continuous("", labels=c("0 %", "25%", "50%", "75%", "100%"))+
    theme(
    axis.text.y = element_text(size=12, color=1),
    axis.title.x = element_text(size=15, color=1),
    axis.text.x = element_text(size=12, color=1),
    axis.title.y = element_text(size=15, color=1),
    plot.title = element_text(hjust=0.5, size=16),
    strip.text.x = element_text(size=12),
    legend.text = element_text(size=13),
    legend.title = element_text(size=15))+
    scale_fill_manual("Would you repeat surgery?", values=col.traf, aesthetics = "fill")

和图2:

ggplot(subset(stw, !is.na(will)), aes(ox_0))+
    stat_bin(geom="step", binwidth = 1)
0s7z1bwu

0s7z1bwu1#

这里有一个来自dplyr的数据集的例子。在这个例子中,我只把fill美学放入geom_bar层,并使用y = after_stat(count)/1000把第二层的范围(最大值684)带到“填充”栏必须覆盖的范围0:1中。在这里,我添加了一个辅助轴标签来显示应该如何解释阶梯线。

ggplot(dplyr::storms, aes(day)) +
  geom_bar(aes(fill = status), position = "fill") +
  stat_bin(geom = "step", binwidth = 1, aes(y = after_stat(count)/1000)) +
  scale_y_continuous(sec.axis = sec_axis(~ . *1000))

相关问题