简体   繁体   中英

Add confidence intervals with specific values to a graph line with two series

I want to reproduce the following graph:

在此处输入图像描述

And my data is the following, where the blue line is complete_preds_means and the orange line is contrafact :

structure(list(dias = structure(c(19052, 19053, 19054, 19055, 
19056, 19057, 19058, 19059, 19060, 19061, 19062, 19063, 19064, 
19065, 19066, 19067, 19068, 19069, 19070, 19071), class = "Date"), 
    complete_preds_means = c(341.07434, 381.59167, 455.47815, 
    485.05597, 527.60876, 562.63965, 602.48975, 624.663, 626.5637, 
    527.2239, 420.71643, 389.30804, 378.74396, 366.61548, 361.36566, 
    363.37253, 319.31824, 314.39688, 303.60342, 294.8934), contrafact = c(364.5, 
    358.89, 466.64, 470.11, 464.25, 487.27, 591.2, 715.33, 628.02, 
    505.98, 402.9, 316.81, 323.35, 358.61, 354.26, 369.5, 317.01, 
    336.5, 285.33, 270.91), complete_preds_lower = c(320.6368042, 
    361.7870895, 432.4487762, 461.2275833, 503.2255051, 535.7108551, 
    576.3850006, 597.9762146, 601.4407013, 504.0448837, 398.7777023, 
    368.0046799, 356.3603165, 345.5847885, 339.9679932, 342.7514801, 
    298.3247482, 293.4419693, 282.5286865, 275.4635284), complete_preds_upper = c(359.9897186, 
    402.5708664, 477.4746765, 508.7775711, 550.3326447, 587.6521027, 
    628.5320251, 649.9691833, 649.4831665, 547.9886108, 442.046402, 
    410.8121475, 399.0208908, 389.8615128, 387.4929993, 386.2935928, 
    340.140834, 336.3622116, 324.793483, 315.4606934)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

So far I have tried this:

plot_fig4 <- ggplot()+
  geom_line(data=fig4, aes(x=dias, y=complete_preds_means), colour="blue")+
  geom_line(data=fig4, aes(x=dias, y=contrafact), colour="red") + 
  geom_ribbon(aes(ymin=fig4$complete_preds_lower, ymax=fig4$complete_preds_upper))+
  labs(y="Clase ($)",
       x="") +
  scale_y_continuous(breaks=seq(from=100, to=800, by=100))+
  scale_x_date(expand = c(0, 0), date_breaks="1 month", date_labels = "%b\n%Y")

But I only get this error: Error: geom_ribbon requires the following missing aesthetics: x or y, xmin and xmax

You haven't told geom_ribbon what variable should be on the x axis. You could just add x=fig4$dias inside the aes of geom_ribbon , but this isn't the best way to use ggplot. Better to use ggplot's inheritance of data and aesthetic mappings to avoid repeating yourself and making mistakes along the way. If you change your first line to ggplot(fig4, aes(x = dias)) you don't need to do data=fig4 and x=dias in every geom call.

A couple of other issues are that you should map the color aesthetic to produce a legend, and make the alpha low on your ribbon so that it is semi-transparent. The ordering of layers is also important.

Finally, I have added some theme tweaks to make the plot more like the desired output.

ggplot(fig4, aes(x = dias)) +
  geom_line(aes(y = contrafact, color = "Contrafact"), linewidth = 1) + 
  geom_ribbon(aes(ymin = complete_preds_lower, ymax = complete_preds_upper),
                  fill = "deepskyblue4", alpha = 0.2) +
  geom_line(aes(y = complete_preds_means, color = "Predicted"), linewidth = 1) +
  geom_vline(xintercept = as.Date("2022-03-13"),
             linetype = 4, colour = "green4") +
  labs(y = "Clase ($)", x = NULL) +
  scale_color_manual(NULL, values = c("orange", "deepskyblue4")) +
  scale_y_continuous(breaks = 1:8 * 100) +
  scale_x_date(expand = c(0, 0), date_breaks = "1 month", 
               date_labels = "%b\n%Y") +
  theme_classic(base_size = 16)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM