两个带状图和线图未显示的共同图例
Common legend for two plots of type ribbon and line plots not showing
对于我的 rmarkdown 文档,我将两个图放在一起。第二个情节应该是第一个情节的“放大”版本。
---
title: TwoGraphs
output: pdf_document
---
```{r echo=FALSE,fig.show="hold", out.width="50%",fig.cap="Results"}
library(ggplot2)
data <- data.frame(
a_max = 1:10 * 10,
a_min = 1:10 * 9,
b_max = 1:10 * 5,
b_min = 1:10 * 4,
c_max = 1:10 * 0.002,
c_min = 1:10 * 0.001
)
ggplot(data, aes(x = 1:10)) +
xlab("Time") + ylab("Amount") +
# a
geom_ribbon(aes(ymax = a_max, ymin = a_min, fill = "A set"), fill = '#9B7FBF') +
geom_line(aes(y = a_max), color = "#1e152a", size=1.5, linetype="dashed") +
geom_line(aes(y = a_min), color = "#1e152a", size=1.5) +
# b
geom_ribbon(aes(ymax = b_max, ymin = b_min, fill = "B set"), fill = '#ACD8DD') +
geom_line(aes(y = b_max), color = "#5ab1bb", size=1.5, linetype="dashed") +
geom_line(aes(y = b_min), color = "#5ab1bb", size=1.5) +
# c
geom_ribbon(aes(ymax = c_max, ymin = c_min, fill = "C set"), fill = '#deebd1') +
geom_line(aes(y = c_max), color = "#a5c882", size=1.5, linetype="dashed") +
geom_line(aes(y = c_min), color = "#a5c882", size=1.5) +
theme_bw()
# zoom in on c
ggplot(data, aes(x = 1:10)) +
xlab("Time") + ylab("Amount") +
geom_ribbon(aes(ymax = c_max, ymin = c_min, fill = "C set"), fill = '#deebd1') +
geom_line(aes(y = c_max), color = "#a5c882", size=1.5, linetype="dashed") +
geom_line(aes(y = c_min), color = "#a5c882", size=1.5) +
theme_bw()
```
生成绘图效果很好,但是缺少我想要的图例。特别是我想要标注色带颜色。线条不是那么重要 - 我确实在标题中提到了,虽然虚线表示大多数未排序的数据集,而实线表示数据集已排序。
我在下面的截图中标出了我想要的图例
I have tried working adding scale_fill_manual
, but it didn't show the legend. Using ggpubr as suggested here 对我也不起作用,如此处所示。
---
title: TwoGraphs
output: pdf_document
---
```{r echo=FALSE,fig.show="hold", out.width="50%",fig.cap="Results"}
library(ggplot2)
data <- data.frame(
a_max = 1:10 * 10,
a_min = 1:10 * 9,
b_max = 1:10 * 5,
b_min = 1:10 * 4,
c_max = 1:10 * 0.002,
c_min = 1:10 * 0.001
)
p1 <- ggplot(data, aes(x = 1:10)) +
xlab("Time") + ylab("Amount") +
# a
geom_ribbon(aes(ymax = a_max, ymin = a_min, fill = "Aha"), fill = '#9B7FBF') +
geom_line(aes(y = a_max), color = "#1e152a", size=1.5, linetype="dashed") +
geom_line(aes(y = a_min), color = "#1e152a", size=1.5) +
# b
geom_ribbon(aes(ymax = b_max, ymin = b_min, fill = "Behe"), fill = '#ACD8DD') +
geom_line(aes(y = b_max), color = "#5ab1bb", size=1.5, linetype="dashed") +
geom_line(aes(y = b_min), color = "#5ab1bb", size=1.5) +
# c
geom_ribbon(aes(ymax = c_max, ymin = c_min, fill = "Cehe"), fill = '#deebd1') +
geom_line(aes(y = c_max), color = "#a5c882", size=1.5, linetype="dashed") +
geom_line(aes(y = c_min), color = "#a5c882", size=1.5) +
theme_bw()
p2 <- ggplot(data, aes(x = 1:10)) +
xlab("Time") + ylab("Amount") +
geom_ribbon(aes(ymax = c_max, ymin = c_min, fill = "Cehe"), fill = '#deebd1') +
geom_line(aes(y = c_max), color = "#a5c882", size=1.5, linetype="dashed") +
geom_line(aes(y = c_min), color = "#a5c882", size=1.5) +
theme_bw()
library(ggpubr)
ggarrange(p1,p2,common.legend = TRUE,legend = "bottom")
```
如何让三个填充区域显示为普通图例?是否也可以添加总体 dashed-line / solid-line 图例? (即,“虚线 = 未排序,实线 = 排序”)
当您在 aes
外添加填充颜色时,它 over-rides 就是您放在里面的颜色。您需要删除这些并在 scale_fill_manual
中指定您的颜色。您可以使用线型美学完全相同的过程。
ggplot(data, aes(x = 1:10)) +
xlab("Time") + ylab("Amount") +
# a
geom_ribbon(aes(ymax = a_max, ymin = a_min, fill = "A set")) +
geom_line(aes(y = a_max, linetype = "sorted"), color = "#1e152a", size=1.5) +
geom_line(aes(y = a_min, linetype = "unsorted"), color = "#1e152a", size=1.5) +
# b
geom_ribbon(aes(ymax = b_max, ymin = b_min, fill = "B set")) +
geom_line(aes(y = b_max, linetype = "sorted"), color = "#5ab1bb", size=1.5) +
geom_line(aes(y = b_min, linetype = "unsorted"), color = "#5ab1bb", size=1.5) +
# c
geom_ribbon(aes(ymax = c_max, ymin = c_min, fill = "C set")) +
geom_line(aes(y = c_max, linetype = "sorted"), color = "#a5c882", size=1.5) +
geom_line(aes(y = c_min, linetype = "unsorted"), color = "#a5c882", size=1.5) +
theme_bw() +
scale_fill_manual(values = c(`A set` = '#9B7FBF', `B set` = '#ACD8DD',
`C set` = '#deebd1')) +
scale_linetype_manual(values = c(sorted = 1, unsorted = 2)) +
guides(linetype = guide_legend(override.aes = list(color = "black", size = 0.4))) +
labs(fill = "", linetype = "") +
theme(legend.position = "bottom",
legend.direction = "vertical")
对于我的 rmarkdown 文档,我将两个图放在一起。第二个情节应该是第一个情节的“放大”版本。
---
title: TwoGraphs
output: pdf_document
---
```{r echo=FALSE,fig.show="hold", out.width="50%",fig.cap="Results"}
library(ggplot2)
data <- data.frame(
a_max = 1:10 * 10,
a_min = 1:10 * 9,
b_max = 1:10 * 5,
b_min = 1:10 * 4,
c_max = 1:10 * 0.002,
c_min = 1:10 * 0.001
)
ggplot(data, aes(x = 1:10)) +
xlab("Time") + ylab("Amount") +
# a
geom_ribbon(aes(ymax = a_max, ymin = a_min, fill = "A set"), fill = '#9B7FBF') +
geom_line(aes(y = a_max), color = "#1e152a", size=1.5, linetype="dashed") +
geom_line(aes(y = a_min), color = "#1e152a", size=1.5) +
# b
geom_ribbon(aes(ymax = b_max, ymin = b_min, fill = "B set"), fill = '#ACD8DD') +
geom_line(aes(y = b_max), color = "#5ab1bb", size=1.5, linetype="dashed") +
geom_line(aes(y = b_min), color = "#5ab1bb", size=1.5) +
# c
geom_ribbon(aes(ymax = c_max, ymin = c_min, fill = "C set"), fill = '#deebd1') +
geom_line(aes(y = c_max), color = "#a5c882", size=1.5, linetype="dashed") +
geom_line(aes(y = c_min), color = "#a5c882", size=1.5) +
theme_bw()
# zoom in on c
ggplot(data, aes(x = 1:10)) +
xlab("Time") + ylab("Amount") +
geom_ribbon(aes(ymax = c_max, ymin = c_min, fill = "C set"), fill = '#deebd1') +
geom_line(aes(y = c_max), color = "#a5c882", size=1.5, linetype="dashed") +
geom_line(aes(y = c_min), color = "#a5c882", size=1.5) +
theme_bw()
```
生成绘图效果很好,但是缺少我想要的图例。特别是我想要标注色带颜色。线条不是那么重要 - 我确实在标题中提到了,虽然虚线表示大多数未排序的数据集,而实线表示数据集已排序。
我在下面的截图中标出了我想要的图例
scale_fill_manual
, but it didn't show the legend. Using ggpubr as suggested here 对我也不起作用,如此处所示。
---
title: TwoGraphs
output: pdf_document
---
```{r echo=FALSE,fig.show="hold", out.width="50%",fig.cap="Results"}
library(ggplot2)
data <- data.frame(
a_max = 1:10 * 10,
a_min = 1:10 * 9,
b_max = 1:10 * 5,
b_min = 1:10 * 4,
c_max = 1:10 * 0.002,
c_min = 1:10 * 0.001
)
p1 <- ggplot(data, aes(x = 1:10)) +
xlab("Time") + ylab("Amount") +
# a
geom_ribbon(aes(ymax = a_max, ymin = a_min, fill = "Aha"), fill = '#9B7FBF') +
geom_line(aes(y = a_max), color = "#1e152a", size=1.5, linetype="dashed") +
geom_line(aes(y = a_min), color = "#1e152a", size=1.5) +
# b
geom_ribbon(aes(ymax = b_max, ymin = b_min, fill = "Behe"), fill = '#ACD8DD') +
geom_line(aes(y = b_max), color = "#5ab1bb", size=1.5, linetype="dashed") +
geom_line(aes(y = b_min), color = "#5ab1bb", size=1.5) +
# c
geom_ribbon(aes(ymax = c_max, ymin = c_min, fill = "Cehe"), fill = '#deebd1') +
geom_line(aes(y = c_max), color = "#a5c882", size=1.5, linetype="dashed") +
geom_line(aes(y = c_min), color = "#a5c882", size=1.5) +
theme_bw()
p2 <- ggplot(data, aes(x = 1:10)) +
xlab("Time") + ylab("Amount") +
geom_ribbon(aes(ymax = c_max, ymin = c_min, fill = "Cehe"), fill = '#deebd1') +
geom_line(aes(y = c_max), color = "#a5c882", size=1.5, linetype="dashed") +
geom_line(aes(y = c_min), color = "#a5c882", size=1.5) +
theme_bw()
library(ggpubr)
ggarrange(p1,p2,common.legend = TRUE,legend = "bottom")
```
如何让三个填充区域显示为普通图例?是否也可以添加总体 dashed-line / solid-line 图例? (即,“虚线 = 未排序,实线 = 排序”)
当您在 aes
外添加填充颜色时,它 over-rides 就是您放在里面的颜色。您需要删除这些并在 scale_fill_manual
中指定您的颜色。您可以使用线型美学完全相同的过程。
ggplot(data, aes(x = 1:10)) +
xlab("Time") + ylab("Amount") +
# a
geom_ribbon(aes(ymax = a_max, ymin = a_min, fill = "A set")) +
geom_line(aes(y = a_max, linetype = "sorted"), color = "#1e152a", size=1.5) +
geom_line(aes(y = a_min, linetype = "unsorted"), color = "#1e152a", size=1.5) +
# b
geom_ribbon(aes(ymax = b_max, ymin = b_min, fill = "B set")) +
geom_line(aes(y = b_max, linetype = "sorted"), color = "#5ab1bb", size=1.5) +
geom_line(aes(y = b_min, linetype = "unsorted"), color = "#5ab1bb", size=1.5) +
# c
geom_ribbon(aes(ymax = c_max, ymin = c_min, fill = "C set")) +
geom_line(aes(y = c_max, linetype = "sorted"), color = "#a5c882", size=1.5) +
geom_line(aes(y = c_min, linetype = "unsorted"), color = "#a5c882", size=1.5) +
theme_bw() +
scale_fill_manual(values = c(`A set` = '#9B7FBF', `B set` = '#ACD8DD',
`C set` = '#deebd1')) +
scale_linetype_manual(values = c(sorted = 1, unsorted = 2)) +
guides(linetype = guide_legend(override.aes = list(color = "black", size = 0.4))) +
labs(fill = "", linetype = "") +
theme(legend.position = "bottom",
legend.direction = "vertical")