ggplot2中的geom_path的更改行类型可以

发布于 2025-02-11 18:52:43 字数 1831 浏览 1 评论 0原文

我有一个数据集(下面的一个小摘录),该数据集绘制了三行 - 两行是从一个置信区间的一个模型估计的,第三行是结果的外部确认。

bestFit <- data.frame(Year = c(2006, 2007, 2008,
                               2006, 2007, 2008,
                               2006, 2007, 2008),
                      Estimate = c(4208, 4570, 4925,
                                   0872, 1324, 1193,
                                   6009, 5705, 6474),
                      Lo95CI = c(2903, 3561, 4036,
                                 0693, 1113, 0999,
                                   NA,   NA,   NA),
                      Hi95CI = c(5209, 5453, 5725,
                                 0998, 1489, 1330,
                                   NA,   NA,   NA),
                      Reconstruction = c("Abun", "Abun", "Abun",
                                         "Recr", "Recr", "Recr",
                                         "Extl", "Extl", "Extl"))
bestFit$Reconstruction <- factor(bestFit$Reconstruction, ordered = TRUE,
                         levels = c("Abun", "Recr", "Extl"))

ggplot(aes(y = Estimate, x = Year, fill = Reconstruction), data = bestFit) + 
    geom_ribbon(aes(ymin = Lo95CI, ymax = Hi95CI, 
                    fill = Reconstruction), alpha = 0.25) +
    geom_point(aes(colour = Reconstruction)) + 
    geom_path(aes(colour = Reconstruction), size = 1) +
    scale_x_continuous(breaks = seq(2006, 2008, 2)) +
    scale_fill_manual(values = c("#F8766D", "#00BA38", "#619CFF")) + 
    scale_color_manual(values = c("#F8766D", "#00BA38", "#619CFF")) +
    scale_linetype_manual(values = c("solid", "solid", "dotted"))

我可以手动设置颜色,但是在手动设置行类型时遇到了麻烦。我希望前两条线(来自我的模型)是固体的,第三行(来自外部模型)虚线。

I have a data set (a small excerpt below) that is plotting three lines - two estimated from one model with confidence intervals and a third line that serves as an external confirmation of the results.

bestFit <- data.frame(Year = c(2006, 2007, 2008,
                               2006, 2007, 2008,
                               2006, 2007, 2008),
                      Estimate = c(4208, 4570, 4925,
                                   0872, 1324, 1193,
                                   6009, 5705, 6474),
                      Lo95CI = c(2903, 3561, 4036,
                                 0693, 1113, 0999,
                                   NA,   NA,   NA),
                      Hi95CI = c(5209, 5453, 5725,
                                 0998, 1489, 1330,
                                   NA,   NA,   NA),
                      Reconstruction = c("Abun", "Abun", "Abun",
                                         "Recr", "Recr", "Recr",
                                         "Extl", "Extl", "Extl"))
bestFit$Reconstruction <- factor(bestFit$Reconstruction, ordered = TRUE,
                         levels = c("Abun", "Recr", "Extl"))

ggplot(aes(y = Estimate, x = Year, fill = Reconstruction), data = bestFit) + 
    geom_ribbon(aes(ymin = Lo95CI, ymax = Hi95CI, 
                    fill = Reconstruction), alpha = 0.25) +
    geom_point(aes(colour = Reconstruction)) + 
    geom_path(aes(colour = Reconstruction), size = 1) +
    scale_x_continuous(breaks = seq(2006, 2008, 2)) +
    scale_fill_manual(values = c("#F8766D", "#00BA38", "#619CFF")) + 
    scale_color_manual(values = c("#F8766D", "#00BA38", "#619CFF")) +
    scale_linetype_manual(values = c("solid", "solid", "dotted"))

enter image description here

I can manually set the colors, but am having trouble manually setting the line type. I want the first two lines (which are from my model) to be solid and the third line (which is from an external model) dashed.

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

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

发布评论

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

评论(1

风吹过旳痕迹 2025-02-18 18:52:43

从评论中:

您尚未映射LineType美学。添加lineType =重建aes geom_path的一部分中。

From comments:

You haven't mapped the linetype aesthetic. Add linetype = Reconstruction inside the aes portion of your geom_path call.

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