Skip to content

Commit

Permalink
interval plot for cluster strata model
Browse files Browse the repository at this point in the history
  • Loading branch information
qianyu313 committed Feb 14, 2024
1 parent da498de commit b447b5a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 41 deletions.
82 changes: 43 additions & 39 deletions R/intervalPlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#'
#' @param model list of model results using surveyPrev
#' @param admin level of plot
#' @param group plot by group or not
#' @param group plot by group or not
#' @param compare plot for compare multiple plot or not
#' @return This function returns the dataset that contain district name and population for given tiff files and polygons of admin level.
#' @import ggplot2
#' @author Qianyu Dong
Expand Down Expand Up @@ -66,10 +67,10 @@
#'


intervalPlot <- function(admin= 0, model=list( list("name 1"= fit1, "name 1"= fit2)), group=F){

intervalPlot <- function(admin= 0, compare=F, model=list( list("name 1"= fit1, "name 1"= fit2)), group=F){


if(compare){

if(admin==0){

Expand Down Expand Up @@ -215,7 +216,46 @@ intervalPlot <- function(admin= 0, model=list( list("name 1"= fit1, "name 1"= fi
}


}else{


res=model[[1]]
data<-res[[1]]
linedata<-res[[2]]
line2<-res[[3]]$mean

plot_fun <- function(dat) {
line1 = linedata[unique(dat$admin1.name),"mean"]
g <- ggplot(dat)

if("type" %in% colnames(dat)){
g <- g + aes(x = admin2.name, y = mean, color = type)
}else{
g <- g + aes(x = admin2.name, y = mean)
}
g <- g +
geom_point( position = position_dodge(width = 0.5),size = .8) +
geom_hline( aes(yintercept =line1,linetype = "dashed"),color="#d95f02",linewidth = .8) +
geom_hline( aes(yintercept =line2, linetype = "solid"),color="#d95f02",linewidth = .8) +
geom_errorbar(aes(ymin = lower , ymax = upper), alpha = 0.7,position = position_dodge(width = 0.5), width = 0.2) +
scale_color_brewer(palette = "Set1") +
labs(title = unique(dat$admin1.name)) +
xlab("") + ylab("") +
scale_linetype_manual(values = c("dashed", "solid"),
labels = c("admin1","national")) +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1))
}

plots <- NULL
for (adm1 in unique(data$admin1.name)){
g <- plot_fun(subset(data, admin1.name == adm1))
plots[[adm1]] <- g
}

return(plots)

}



Expand All @@ -225,42 +265,6 @@ intervalPlot <- function(admin= 0, model=list( list("name 1"= fit1, "name 1"= fi



#
#
# data<-res[[1]]
# linedata<-res[[2]]
# line2<-res[[3]]$mean
#
# plot_fun <- function(dat) {
# line1 = linedata[unique(dat$admin1.name),"mean"]
# g <- ggplot(dat)
#
# if("type" %in% colnames(dat)){
# g <- g + aes(x = admin2.name, y = mean, color = type)
# }else{
# g <- g + aes(x = admin2.name, y = mean)
# }
# g <- g +
# geom_point( position = position_dodge(width = 0.5),size = .8) +
# geom_hline( aes(yintercept =line1,linetype = "dashed"),color="#d95f02",linewidth = .8) +
# geom_hline( aes(yintercept =line2, linetype = "solid"),color="#d95f02",linewidth = .8) +
# geom_errorbar(aes(ymin = lower , ymax = upper), alpha = 0.7,position = position_dodge(width = 0.5), width = 0.2) +
# scale_color_brewer(palette = "Set1") +
# labs(title = unique(dat$admin1.name)) +
# xlab("") + ylab("") +
# scale_linetype_manual(values = c("dashed", "solid"),
# labels = c("admin1","national")) +
# theme_bw() +
# theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1))
# }
#
# plots <- NULL
# for (adm1 in unique(data$admin1.name)){
# g <- plot_fun(subset(data, admin1.name == adm1))
# plots[[adm1]] <- g
# }
#
# return(plots)

}

4 changes: 2 additions & 2 deletions R/smthdirectEST.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fhModel <- function(data, cluster.info, admin.info = NULL, admin, CI = 0.95, mo
admin2_res <- fit2$smooth
colnames(admin2_res)[colnames(admin2_res) == 'region'] <- 'DistrictName'
# colnames(admin2_res)[colnames(admin2_res) == 'mean'] <- 'value'
admin2_res$se<-sqrt(admin2_res$var)
admin2_res$sd<-sqrt(admin2_res$var)


####message for aggregation=T but missing some components and return results without aggregation
Expand Down Expand Up @@ -206,7 +206,7 @@ fhModel <- function(data, cluster.info, admin.info = NULL, admin, CI = 0.95, mo
admin1_res <- fit1$smooth
colnames(admin1_res)[colnames(admin1_res) == 'region'] <- 'admin1.name'
colnames(admin1_res)[colnames(admin1_res) == 'mean'] <- 'mean'
admin1_res$se<-sqrt(admin1_res$var)
admin1_res$sd<-sqrt(admin1_res$var)


####message for aggregation=T but missing some components and return results without aggregation
Expand Down
3 changes: 3 additions & 0 deletions man/intervalPlot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b447b5a

Please sign in to comment.