0

I have a census data of 8 years and their corresponding population values. I have worked through a model and computed the fitted values corresponding to the given years in the data and also estimated the values of the population between the two given years (10 years gap).

I want to plot the fitted values over all the years (census year and non-census year). In that very plot I want to plot the given population observations against the census years.

What would be the best way to approach this problem in R? Any hint or advice would be appreciated.

I have tried to dig in the internet to find any tutorial, unfortunately so far I have failed to come across any that caters to my need.

This is the snippet of the data:

Census Yr.  Observed    Fitted
1881        155179  159065.7154
                    165281.5066
                    171412.0231
                    177443.1571
                    183361.9961
                    189156.8866
                    194817.4808
                    200334.7674
                    205701.0867
                    210910.132
1891        223314  215956.9368
                    220837.8509
                    225550.5055
                    230093.7686
                    234467.6931
                    238673.4581
                    242713.3047
                    246590.4685
                    250309.1086
                    253874.2355
1901        265780  257291.6383
User1865345
  • 105
  • 4
  • 1
    Hi and welcome! You will likely get much better, faster help if you edit your question to make it reproducible, including sample data, code, and desired output. Some tips on asking a good reproducible question are [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Good luck! – jpsmith Aug 18 '23 at 14:22
  • I don't really understand what the problem is. As long as you have x and y coordinates you can put however many points on a plot that you want. It's very hard to help you without a much more specific problem statement - could you share some sample data (preferably in a copy/pasteable format like `dput(your_data[1:10, ])` for the first 10 rows of your data) and show your best attempt at coding a solution so far? – Gregor Thomas Aug 18 '23 at 14:23
  • @GregorThomas, I have added a portion of the data in order to provide a better insight. – User1865345 Aug 18 '23 at 14:42
  • What is in the `Observed` column where you show blanks? Are they `NA` values? Or are they empty strings `""` (which would mean your `Observed` column is `character` not `numeric` class)? Either way, you'll need to fill in those missing values with the values you actually want plotted. How to do that will depend on your data structure and what's there. If you use `dput()` to share sample data, then it will be much clearer for us. `dput(your_data[1:10, ])` will give copy/pasteable R code to exactly reproduce the first 10 rows of your data including the data structure info. – Gregor Thomas Aug 18 '23 at 14:46
  • @GregorThomas, here observed means those were the population value that were observed in the respective years. Between the two census years, there was no official observation. That's why they are blank. – User1865345 Aug 18 '23 at 14:48
  • @GregorThomas, I have copied the portion of the data from the spreadsheet where I calculated the fitted values. My plan was I would make a data frame in R with those columns and make a graph as per my requirement. – User1865345 Aug 18 '23 at 14:50
  • Okay, so if you don't want to plot `Observed` values for intercensal years, you don't need to fill those it. But you'll presumably want to fill in the `Census Yr.` column. But it sounds like the first step is going to be to import your data in R. Is your data in R? If so **please** share it with `dput()`**. If not, start working on importing it. What have you tried and where did you get stuck? – Gregor Thomas Aug 18 '23 at 14:52
  • @GregorThomas, thanks for understanding the problem now. Okay, I would update my post after importing the data and updating the year column. Appreciate your feedback. (And no, the data is not in R.) – User1865345 Aug 18 '23 at 14:53
  • Sure @jpsmith, I would update the post after I import the data in R. Thanks for the feedback. – User1865345 Aug 18 '23 at 14:54

1 Answers1

1

Your data appear to increment by one year - if so, you could try something like:

plot(x = seq(min(df$year, na.rm = TRUE), 
               max(df$year, na.rm = TRUE)+9, by = 1),
       y = df$Fitted, pch = 25, bg = "seagreen", xlab = "Year")
points(x = df$year, y = df$Observed, pch = 24, bg = "maroon")
legend("topleft", c("Observed", "Fitted"), pch = 24:25, 
       pt.bg = c("maroon","seagreen"), bty = "n")

enter image description here

Data:

df <- read.table(text = "year  Observed    Fitted
1881        155179  159065.7154
NA NA                    165281.5066
NA NA                    171412.0231
NA NA                    177443.1571
NA NA                    183361.9961
NA NA                    189156.8866
NA NA                    194817.4808
NA NA                    200334.7674
NA NA                    205701.0867
NA NA                    210910.132
1891        223314  215956.9368
NA NA                    220837.8509
NA NA                    225550.5055
NA NA                    230093.7686
NA NA                    234467.6931
NA NA                    238673.4581
NA NA                    242713.3047
NA NA                    246590.4685
NA NA                    250309.1086
NA NA                    253874.2355", h = TRUE)
jpsmith
  • 11,023
  • 5
  • 15
  • 36
  • 1
    Thanks for the quick answer, jpsmith. +1. I would work on it and would update my subsequent actions. – User1865345 Aug 18 '23 at 15:11
  • @User1865345 great! hope it helps. Note I tweaked it a bit - I based the main plot on the predicted then the `points` on the observed (the original edit was reversed). – jpsmith Aug 18 '23 at 15:21
  • 1
    I have taken inspiration from your code (fancy way of saying) and used ggplot2. The plot was what I needed. Appreciate your help (Accepted the answer last night already). – User1865345 Aug 19 '23 at 05:47