简体   繁体   中英

How to find rate of growth and plot from population data year by year?

I'm trying to graph the population growth in % from the population of each year. How would i do this in R?

My data looks like this:

Year    Population
1960    10276477
1961    10483000
1962    10742000
.       .
.       .
.       .
etc towards 2016

I've done:

Year <- Population$Year
Amount <- Population$Population
x=c(Year)
y=c(Amount)
plot(x,y)

Now i'm just wondering how to get the growth percentages and graphing that as well. Thanks!

The previous poster said it right, you really need to look into dplyr and ggplot2. This probably isn't as compact as it could be but I think it gets the job done...

library(tidyverse)
library(ggplot2)
library(lubridate)

Create vectors & Data Frame

Year <- seq(ymd("1960/1/1"), ymd("2016/1/1"), by = "years")
Population <- runif(length(Year), min = 100000, max = 10000000)
df <- data_frame(Year, Population)

Dplyr Operations

df %>% 
mutate(Previous_Year = lag(Population, 1), Change = Population - Previous_Year, Percent_Change = Change/Previous_Year*100) %>%
ggplot(aes(x = Year, y = Percent_Change)) +
geom_line() +
geom_smooth()

Basically the mutate call creates a new column, the lag offsets the population data by 1 row so you can do straightforward subtraction between the two, then you can divide them and multiply by 100

You can actually skip creating the "previous_year" column (for ex, Change = Population - lag(Population,1) but I separated the steps to show what's happening.

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