The following code chunk works. It is one of the many chunks in my RMD file (each demarcated by r{}
)
I'm frustrated in adding figure captions for the two plots I created. Spent much time looking at discussions and have not made any progress. Such a caption is not needed for this assignment but I want to eventually use Rmarkdown to write papers. Thank you for your help.
Srini
library(dplyr)
library(ggplot2)
# 'pd_csvfile': population dynamics data CSV file
ComputeMeanAFSR <- function(pd_csvfile) {
popdyn_df <- read.csv(file=pd_csvfile, header=TRUE, as.is=TRUE)
## NEED AN AFSR VARIABLE in new COLUMN.
popdyn_df$AFSR <- popdyn_df$births/popdyn_df$py.women
## Return DF
return(popdyn_df)
}
## CALL FUNCTIONS to Compute AFSR for UK, USA
## AFSR is saved as a new variable column in new CSV files
USA_AFSR <- ComputeMeanAFSR("USA.csv")
UK_AFSR <- ComputeMeanAFSR("UK.csv")
## Plot AFSR Bar Charts for USA and UK
ggplot(data=USA_AFSR, aes(x=USA_AFSR$age, y=USA_AFSR$AFSR, fill=USA_AFSR$period)) + geom_bar(colour="black", stat="identity") +
labs(fill = "Period") + xlab("Age") + ylab("AFSR") +
ylim(0, 0.6) + ggtitle("AFSR for USA") +
theme(axis.text.x = element_text(size=12,angle=45,hjust=1,vjust=1))
ggplot(data=UK_AFSR, aes(x=UK_AFSR$age, y=UK_AFSR$AFSR, fill=UK_AFSR$period)) + geom_bar(colour="black", stat="identity") +
labs(fill = "Period") + xlab("Age") + ylab("AFSR") +
ylim(0, 0.6) + ggtitle("AFSR for UK") +
theme(axis.text.x = element_text(size=12,angle=45,hjust=1,vjust=1))