par(mfrow=c(3,3))
# mortality age 50
plot(DataAge50$Year,DataAge50$DA_Female/DataAge50$EA_Female,xlab="Year",ylab="Mortality rates",main="Age 50",type="l",col="green",cex=1)
lines(DataAge50$Year,DataAge50$DBC_Female/DataAge50$EBC_Female,xlab="Year",ylab="Mortality rates",main="Age=50",type="l",col="blue",cex=1)
lines(DataAge50$Year,DataAge50$DM_Female/DataAge50$EM_Female,xlab="Year",ylab="Mortality rates",main="Age=50",type="l",col="red",cex=1)
lines(DataAge50$Year,DataAge50$DNB_Female/DataAge50$ENB_Female,xlab="Year",ylab="Mortality rates",main="Age=50",type="l",col="black",cex=1)
lines(DataAge50$Year,DataAge50$DNL_Female/DataAge50$ENL_Female,xlab="Year",ylab="Mortality rates",main="Age=50",type="l",col="yellow",cex=1)
lines(DataAge50$Year,DataAge50$DNTN_Female/DataAge50$ENTN_Female,xlab="Year",ylab="Mortality rates",main="Age=50",type="l",col="beige",cex=1)
lines(DataAge50$Year,DataAge50$DNS_Female/DataAge50$ENS_Female,xlab="Year",ylab="Mortality rates",main="Age=50",type="l",col="orange",cex=1)
lines(DataAge50$Year,DataAge50$DO_Female/DataAge50$EO_Female,xlab="Year",ylab="Mortality rates",main="Age=50",type="l",col="blue",cex=1)
lines(DataAge50$Year,DataAge50$DPEI_Female/DataAge50$EPEI_Female,xlab="Year",ylab="Mortality rates",main="Age=50",type="l",col="grey",cex=1)
lines(DataAge50$Year,DataAge50$DQ_Female/DataAge50$EQ_Female,xlab="Year",ylab="Mortality rates",main="Age=50",type="l",col="chocolate",cex=1)
lines(DataAge50$Year,DataAge50$DS_Female/DataAge50$ES_Female,xlab="Year",ylab="Mortality rates",main="Age=50",type="l",col="salmon",cex=1)
lines(DataAge50$Year,DataAge50$DY_Female/DataAge50$EY_Female,xlab="Year",ylab="Mortality rates",main="Age=50",type="l",col="maroon",cex=1)
#Moratlity rates by age=55
plot(DataAge55$Year,DataAge55$DA_Female/DataAge55$EA_Female,xlab="Year",ylab="Mortality rates",main="Age 55",type="l",col="green",cex=1)
lines(DataAge55$Year,DataAge55$DBC_Female/DataAge55$EBC_Female,xlab="Year",ylab="Mortality rates",main="Age 55",type="l",col="blue",cex=1)
lines(DataAge55$Year,DataAge55$DM_Female/DataAge55$EM_Female,xlab="Year",ylab="Mortality rates",main="Age 55",type="l",col="red",cex=1)
lines(DataAge55$Year,DataAge55$DNB_Female/DataAge55$ENB_Female,xlab="Year",ylab="Mortality rates",main="Age=55",type="l",col="black",cex=1)
lines(DataAge55$Year,DataAge55$DNL_Female/DataAge55$ENL_Female,xlab="Year",ylab="Mortality rates",main="Age=55",type="l",col="yellow",cex=1)
lines(DataAge55$Year,DataAge55$DNTN_Female/DataAge55$ENTN_Female,xlab="Year",ylab="Mortality rates",main="Age=55",type="l",col="beige",cex=1)
lines(DataAge55$Year,DataAge55$DNS_Female/DataAge55$ENS_Female,xlab="Year",ylab="Mortality rates",main="Age=55",type="l",col="orange",cex=1)
lines(DataAge55$Year,DataAge55$DO_Female/DataAge55$EO_Female,xlab="Year",ylab="Mortality rates",main="Age=55",type="l",col="blue",cex=1)
lines(DataAge55$Year,DataAge55$DPEI_Female/DataAge55$EPEI_Female,xlab="Year",ylab="Mortality rates",main="Age=55",type="l",col="grey",cex=1)
lines(DataAge55$Year,DataAge55$DQ_Female/DataAge55$EQ_Female,xlab="Year",ylab="Mortality rates",main="Age=55",type="l",col="chocolate",cex=1)
lines(DataAge55$Year,DataAge55$DS_Female/DataAge55$ES_Female,xlab="Year",ylab="Mortality rates",main="Age=55",type="l",col="salmon",cex=1)
lines(DataAge55$Year,DataAge55$DY_Female/DataAge55$EY_Female,xlab="Year",ylab="Mortality rates",main="Age=55",type="l",col="maroon",cex=1)
#mortality age 60
plot(DataAge60$Year,DataAge60$DA_Female/DataAge60$EA_Female,xlab="Year",ylab="Mortality rates",main="Age=60",type="l",col="green",cex=1)
lines(DataAge60$Year,DataAge60$DBC_Female/DataAge60$EBC_Female,xlab="Year",ylab="Mortality rates",main="Age=60",type="l",col="blue",cex=1)
lines(DataAge60$Year,DataAge60$DM_Female/DataAge60$EM_Female,xlab="Year",ylab="Mortality rates",main="Age 60",type="l",col="red",cex=1)
lines(DataAge60$Year,DataAge60$DNB_Female/DataAge60$ENB_Female,xlab="Year",ylab="Mortality rates",main="Age=60",type="l",col="black",cex=1)
lines(DataAge60$Year,DataAge60$DNL_Female/DataAge60$ENL_Female,xlab="Year",ylab="Mortality rates",main="Age=60",type="l",col="yellow",cex=1)
lines(DataAge60$Year,DataAge60$DNTN_Female/DataAge60$ENTN_Female,xlab="Year",ylab="Mortality rates",main="Age=60",type="l",col="beige",cex=1)
lines(DataAge60$Year,DataAge60$DNS_Female/DataAge60$ENS_Female,xlab="Year",ylab="Mortality rates",main="Age=60",type="l",col="orange",cex=1)
lines(DataAge60$Year,DataAge60$DO_Female/DataAge60$EO_Female,xlab="Year",ylab="Mortality rates",main="Age=60",type="l",col="blue",cex=1)
lines(DataAge60$Year,DataAge60$DPEI_Female/DataAge60$EPEI_Female,xlab="Year",ylab="Mortality rates",main="Age=60",type="l",col="grey",cex=1)
lines(DataAge60$Year,DataAge60$DQ_Female/DataAge60$EQ_Female,xlab="Year",ylab="Mortality rates",main="Age=60",type="l",col="chocolate",cex=1)
lines(DataAge60$Year,DataAge60$DS_Female/DataAge60$ES_Female,xlab="Year",ylab="Mortality rates",main="Age=60",type="l",col="salmon",cex=1)
lines(DataAge60$Year,DataAge60$DY_Female/DataAge60$EY_Female,xlab="Year",ylab="Mortality rates",main="Age=60",type="l",col="maroon",cex=1)
#mortality age 65
plot(DataAge65$Year,DataAge65$DA_Female/DataAge65$EA_Female,xlab="Year",ylab="Mortality rates",main="Age=65",type="l",col="green",cex=1)
lines(DataAge65$Year,DataAge65$DBC_Female/DataAge65$EBC_Female,xlab="Year",ylab="Mortality rates",main="Age=65",type="l",col="blue",cex=1)
lines(DataAge65$Year,DataAge65$DM_Female/DataAge65$EM_Female,xlab="Year",ylab="Mortality rates",main="Age 65",type="l",col="red",cex=1)
lines(DataAge65$Year,DataAge65$DNB_Female/DataAge65$ENB_Female,xlab="Year",ylab="Mortality rates",main="Age=65",type="l",col="black",cex=1)
lines(DataAge65$Year,DataAge65$DNL_Female/DataAge65$ENL_Female,xlab="Year",ylab="Mortality rates",main="Age=65",type="l",col="yellow",cex=1)
lines(DataAge65$Year,DataAge65$DNTN_Female/DataAge65$ENTN_Female,xlab="Year",ylab="Mortality rates",main="Age=65",type="l",col="beige",cex=1)
lines(DataAge65$Year,DataAge65$DNS_Female/DataAge65$ENS_Female,xlab="Year",ylab="Mortality rates",main="Age=65",type="l",col="orange",cex=1)
lines(DataAge65$Year,DataAge65$DO_Female/DataAge65$EO_Female,xlab="Year",ylab="Mortality rates",main="Age=65",type="l",col="blue",cex=1)
lines(DataAge65$Year,DataAge65$DPEI_Female/DataAge65$EPEI_Female,xlab="Year",ylab="Mortality rates",main="Age=65",type="l",col="grey",cex=1)
lines(DataAge65$Year,DataAge65$DQ_Female/DataAge65$EQ_Female,xlab="Year",ylab="Mortality rates",main="Age=65",type="l",col="chocolate",cex=1)
lines(DataAge65$Year,DataAge65$DS_Female/DataAge65$ES_Female,xlab="Year",ylab="Mortality rates",main="Age=65",type="l",col="salmon",cex=1)
lines(DataAge65$Year,DataAge65$DY_Female/DataAge65$EY_Female,xlab="Year",ylab="Mortality rates",main="Age=65",type="l",col="maroon",cex=1)
#mortality age 70
plot(DataAge70$Year,DataAge70$DA_Female/DataAge70$EA_Female,xlab="Year",ylab="Mortality rates",main="Age=70",type="l",col="green",cex=1)
lines(DataAge70$Year,DataAge70$DBC_Female/DataAge70$EBC_Female,xlab="Year",ylab="Mortality rates",main="Age=70",type="l",col="blue",cex=1)
lines(DataAge70$Year,DataAge70$DM_Female/DataAge70$EM_Female,xlab="Year",ylab="Mortality rates",main="Age=70",type="l",col="red",cex=1)
lines(DataAge70$Year,DataAge70$DNB_Female/DataAge70$ENB_Female,xlab="Year",ylab="Mortality rates",main="Age=70",type="l",col="black",cex=1)
lines(DataAge70$Year,DataAge70$DNL_Female/DataAge70$ENL_Female,xlab="Year",ylab="Mortality rates",main="Age=70",type="l",col="yellow",cex=1)
lines(DataAge70$Year,DataAge70$DNTN_Female/DataAge70$ENTN_Female,xlab="Year",ylab="Mortality rates",main="Age=70",type="l",col="beige",cex=1)
lines(DataAge70$Year,DataAge70$DNS_Female/DataAge70$ENS_Female,xlab="Year",ylab="Mortality rates",main="Age=70",type="l",col="orange",cex=1)
lines(DataAge70$Year,DataAge70$DO_Female/DataAge70$EO_Female,xlab="Year",ylab="Mortality rates",main="Age=70",type="l",col="blue",cex=1)
lines(DataAge70$Year,DataAge70$DPEI_Female/DataAge70$EPEI_Female,xlab="Year",ylab="Mortality rates",main="Age=70",type="l",col="grey",cex=1)
lines(DataAge70$Year,DataAge70$DQ_Female/DataAge70$EQ_Female,xlab=”Year”,ylab=”Mortality rates”,main=”Age=70”,type=”l”,col=”chocolate”,cex=1)
lines(DataAge70$Year,DataAge70$DS_Female/DataAge70$ES_Female,xlab="Year",ylab="Mortality rates",main="Age=70",type="l",col="salmon",cex=1)
lines(DataAge70$Year,DataAge70$DY_Female/DataAge70$EY_Female,xlab="Year",ylab="Mortality rates",main="Age=70",type="l",col="maroon",cex=1)
# mortality age 75
plot(DataAge75$Year,DataAge75$DA_Female/DataAge75$EA_Female,xlab="Year",ylab="Mortality rates",main="Age=75",type="l",col="green",cex=1)
lines(DataAge75$Year,DataAge75$DBC_Female/DataAge75$EBC_Female,xlab="Year",ylab="Mortality rates",main="Age=75",type="l",col="blue",cex=1)
lines(DataAge75$Year,DataAge75$DM_Female/DataAge75$EM_Female,xlab="Year",ylab="Mortality rates",main="Age 75",type="l",col="red",cex=1)
lines(DataAge75$Year,DataAge75$DNB_Female/DataAge75$ENB_Female,xlab="Year",ylab="Mortality rates",main="Age=75",type="l",col="black",cex=1)
lines(DataAge75$Year,DataAge75$DNL_Female/DataAge75$ENL_Female,xlab="Year",ylab="Mortality rates",main="Age=75",type="l",col="yellow",cex=1)
lines(DataAge75$Year,DataAge75$DNTN_Female/DataAge75$ENTN_Female,xlab="Year",ylab="Mortality rates",main="Age=75",type="l",col="beige",cex=1)
lines(DataAge75$Year,DataAge75$DNS_Female/DataAge75$ENS_Female,xlab="Year",ylab="Mortality rates",main="Age=75",type="l",col="orange",cex=1)
lines(DataAge75$Year,DataAge75$DO_Female/DataAge75$EO_Female,xlab="Year",ylab="Mortality rates",main="Age=75",type="l",col="blue",cex=1)
lines(DataAge75$Year,DataAge75$DPEI_Female/DataAge75$EPEI_Female,xlab="Year",ylab="Mortality rates",main="Age=75",type="l",col="grey",cex=1)
lines(DataAge75$Year,DataAge75$DQ_Female/DataAge75$EQ_Female,xlab="Year",ylab="Mortality rates",main="Age=75",type="l",col="chocolate",cex=1)
lines(DataAge75$Year,DataAge75$DS_Female/DataAge75$ES_Female,xlab="Year",ylab="Mortality rates",main="Age=75",type="l",col="salmon",cex=1)
lines(DataAge75$Year,DataAge75$DY_Female/DataAge75$EY_Female,xlab="Year",ylab="Mortality rates",main="Age=75",type="l",col="maroon",cex=1)
# mortality age 80
plot(DataAge80$Year,DataAge80$DA_Female/DataAge80$EA_Female,xlab="Year",ylab="Mortality rates",main="Age=80",type="l",col="green",cex=1)
lines(DataAge80$Year,DataAge80$DBC_Female/DataAge80$EBC_Female,xlab="Year",ylab="Mortality rates",main="Age=80",type="l",col="blue",cex=1)
lines(DataAge80$Year,DataAge80$DM_Female/DataAge80$EM_Female,xlab="Year",ylab="Mortality rates",main="Age=80",type="l",col="red",cex=1)
lines(DataAge80$Year,DataAge80$DNB_Female/DataAge80$ENB_Female,xlab="Year",ylab="Mortality rates",main="Age=80",type="l",col="black",cex=1)
lines(DataAge80$Year,DataAge80$DNL_Female/DataAge80$ENL_Female,xlab="Year",ylab="Mortality rates",main="Age=80",type="l",col="yellow",cex=1)
lines(DataAge80$Year,DataAge80$DNTN_Female/DataAge80$ENTN_Female,xlab="Year",ylab="Mortality rates",main="Age=80",type="l",col="beige",cex=1)
lines(DataAge80$Year,DataAge80$DNS_Female/DataAge80$ENS_Female,xlab="Year",ylab="Mortality rates",main="Age=80",type="l",col="orange",cex=1)
lines(DataAge80$Year,DataAge80$DO_Female/DataAge80$EO_Female,xlab="Year",ylab="Mortality rates",main="Age=80",type="l",col="blue",cex=1)
lines(DataAge80$Year,DataAge80$DPEI_Female/DataAge80$EPEI_Female,xlab="Year",ylab="Mortality rates",main="Age=80",type="l",col="grey",cex=1)
lines(DataAge80$Year,DataAge80$DQ_Female/DataAge80$EQ_Female,xlab="Year",ylab="Mortality rates",main="Age=80",type="l",col="chocolate",cex=1)
lines(DataAge80$Year,DataAge80$DS_Female/DataAge80$ES_Female,xlab="Year",ylab="Mortality rates",main="Age=80",type="l",col="salmon",cex=1)
lines(DataAge80$Year,DataAge80$DY_Female/DataAge80$EY_Female,xlab="Year",ylab="Mortality rates",main="Age=80",type="l",col="maroon",cex=1)
[enter link description here][1]
如何编写代码来为这7个图设置一个共同的图例: 我尝试过这段代码,但结果却是微不足道的
legend(“topright”,legend=c(“Alberta”,”British Columbia”,”Manitoba”,”New Brunswick”,”New Foundlands”,New Terrotories”, New scotia”, “Ontario”,Prince Edouad Island”,”Quebec”, “SaKastchewan””Yukon”),cex=0.8,fill.colors)
请帮助我改进代码吗?
答案 0 :(得分:1)
如果没有尝试甚至看过数据,这段代码就无法运行,但它应该让您了解如何简化流程:
# perform the repetitive normalisation of the columns of data
normalise <- function(d, varnames = c("A","BC","M","NB","NL",
"NTN","NS","O","PEI","Q","S","Y")){
dnames <- paste0("D", varnames, "_Female")
enames <- paste0("E", varnames, "_Female")
result <- data.frame(d[["Year"]], d[dnames] / d[enames])
names(result) <- c("date", varnames)
result
}
# combine all the datasets in a list
l1 <- list(age50 = DataAge50,
age55 = DataAge55,
age60 = DataAge60,
age65 = DataAge65,
age70 = DataAge70,
age75 = DataAge75,
age80 = DataAge80)
# apply our function to all datasets
l2 <- lapply(l1, normalise)
library(reshape2)
# combine datasets into a long format data.frame for plotting
m <- melt(l2, id="date")
str(m)
library(ggplot2)
# plot with facetting and automatic legend
ggplot(m, aes(date, value, colour=variable)) +
facet_wrap(~L1, ncol=3) +
geom_path()