1
votes

I accessed this graph of estimation of number of cases of diabetes and future projections of numbers for every two year estimation data points from year 2000. The graph is factually incorrect as the points on line do not coincide with the scale on left. I am trying to replot it in ggplot2 or ggplotly. While replotting I intend to make two line graphs in a single plot - One for estimations over last few years and the other for future projections made in those years for next 20-25 years and the year on which the projections were made. Any help is highly appreciable. enter image description here

Here is the data that was used to plot the graph - Estimated numbers with year are represented in blue while Projected numbers for future years are represented by red line. Since, there are multiple projected numbers for few year, I am intending to keep the highest number on the line graph.

EstimationYear Estimates (in millions) Projections (in millions) Projection Year
2000 151 333 2025
2003 194 380 2025
2006 246 438 2030
2009 285 552 2030
2011 366 578 2030
2013 382 592 2035
2015 415 642 2040
2017 425 629 2045
2019 463 700 2045
2
Hi there and welcome to stack overflow. Please don't post data as images (for reasons detailed here). One of the best way to share data in R is to append the output of dput(your_data_sample) to your question. - teunbrand
Thanks for pointing that out. I have changed the data from image to table. - AST

2 Answers

0
votes

Your question is more about the data wrangling than the actual plotting with ggplot. Once you have the data in the right shape, the plotting command is just a few lines.

  1. prepare the data for the estimation (blue) points. Set a column type to "estimation".
  2. prepare the data for the projected (red) points. Set a column type to "projection".
  3. use bind_rows to combine both tables.
  4. In the aesthetics of ggplot use color=type
0
votes

Here is a start in how you can go recreate the plot from the data. I haven't put any effort in recreating the balloons, set the theme to something more elegant and those kind of things.

library(ggplot2)

txt <- "2000    151 333 2025
2003    194 380 2025
2006    246 438 2030
2009    285 552 2030
2011    366 578 2030
2013    382 592 2035
2015    415 642 2040
2017    425 629 2045
2019    463 700 2045"

df <- read.table(text = txt)

# Putting years and values in the same columns
# Probably some tidyverse function can do this more elegantly
df <- rbind(cbind(unname(df[1:2]), type = "Estimates"),
            cbind(unname(df[4:3]), type = "Projection"))
colnames(df) <- c("year", "value", "type")

# We're reordering on value, because the red line does not touch year-duplicates
df <- df[order(df$value, decreasing = TRUE),]

ggplot(df, aes(year, value, colour = type)) +
  # Formula notation to filter out data for the line
  geom_line(data = ~ subset(., !duplicated(year))) +
  geom_point() +
  scale_colour_manual(
    values = c("Estimates" = "dodgerblue",
               "Projection" = "tomato")
  ) +
  scale_y_continuous(limits = c(0, NA),
                     name = "Millions")

Created on 2021-01-06 by the reprex package (v0.3.0)