两个带状图和线图类型图的通用图例未显示

发布于 2025-01-18 19:32:41 字数 3675 浏览 3 评论 0原文

对于我的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 ,但没有显示传奇。使用ggpubr为在这里建议的也对我不起作用,如这里所示。

---
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")
```

我如何使三个填充的领域成为一个共同的传说?是否也可以添加总体虚线 /固体线传奇? (即,“虚线= unsorted,实心=排序”)

For my rmarkdown document, I've put together two plots. The second plot is supposed to be a "zoomed in" version of the first plot.

---
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()
```

Generating the plot works fine, however the legend I want to have is missing. Particularly I want the ribbon colors to be notated. The lines are not so important - I do mention in the caption though that dashed means most unsorted dataset, and a continuous line means that the dataset is sorted.

I have marked the legend I want to have in the screenshot below.

Two plots, below I drew in the legends I want to have

From this answer I have tried working adding scale_fill_manual, but it didn't show the legend. Using ggpubr as suggested here also didn't work for me, as demonstrated 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")
```

ggpubr also doesn't draw legends

How do I get the three filled areas to show up as a common legend? Is it also possible to add an overarching dashed-line / solid-line legend? (i.e., "dashed = unsorted, solid = sorted")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若无相欠,怎会相见 2025-01-25 19:32:41

当您在 aes 外部添加填充颜色时,它会覆盖您在内部添加的填充颜色。您需要删除这些内容并在 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")

输入图片此处描述

When you add a fill colour outside aes, it over-rides the one you put inside. You need to remove these and specify your colours inside scale_fill_manual. You can follow exactly the same process with the linetype aesthetic.

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")

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文