R/plotGraphs.R

Defines functions graphPostcodeArea graphPostcode

Documented in graphPostcode

#' Postcode graph.
#' @description Plot vehicle ownership graph based on input postcode.
#' @param postcodes The first part of a UK postcode.
#' @param type "Cars", "Motorcycles", or "Other" 
#' @importFrom magrittr "%>%"
#' @examples
#' graphPostcode(postcodes=c("SW9", "SE5"), postcodes_label = "South-East London")
graphPostcode <- function(postcodes=c("SW9"), type="Cars", postcodes_label = postcodes[1]) {
    textpostcodes <- paste(postcodes,collapse=", ")
    print(textpostcodes)
    print(type)
    if (is.null(df_global)) {
        VEH0122::getDataTable(type=type)
    } # if null df_global
    df_postcode <- df_global[df_global$Postcode %in% postcodes,]
    df_postcode$Postcodes <-postcodes_label
    graphdata <- reshape2::melt(df_postcode, id="Postcodes", variable.factor=FALSE)
    names(graphdata)[c(2,3)] <-c("Quarter", "CarsOwned")
    graphdata$Postcodes <- postcodes_label
    graphdata$CarsOwned <- as.numeric(graphdata$CarsOwned)
    graphdata <-graphdata %>%
        dplyr::filter(!is.na(CarsOwned)) %>%
        dplyr::group_by(Postcodes,Quarter) %>%
        dplyr::summarise(CarsOwned = sum(CarsOwned, na.rm=TRUE))
    names(graphdata)[3] <- "CarsOwned"
    ## pplot <- ggplot2::ggplot(graphdata, ggplot2::aes(Quarter, CarsOwned, group = Postcodes)) +
    pplot <- ggplot2::ggplot(graphdata, ggplot2::aes(Quarter, CarsOwned, group=1)) +
        ggplot2::theme_bw() +
        ggplot2::theme(#panel.grid.major.y =ggplot2::element_blank(),
                       panel.grid.minor =ggplot2::element_blank(),
#                       legend.position = 'none',
                     axis.text.x = ggplot2::element_text(angle = 60, hjust = 1, size=8),
                     axis.ticks = ggplot2::element_blank(),
                       panel.border=ggplot2::element_rect(fill=NA,color=NA)) +
                                        # ggplot2::geom_smooth(stat="smooth", se=FALSE, alpha=1/100) +
        ggplot2::stat_smooth(geom='line', alpha=0.2, se=FALSE) +
        ## ggplot2::geom_point(ggplot2::aes(group=Postcodes,colour=Postcodes), alpha=0.9) + 
        ## ggplot2::geom_point(ggplot2::aes(group=1,colour=Postcodes), alpha=0.9) +
        ggplot2::geom_point(ggplot2::aes(group=1), alpha=0.9) +
        ggplot2::geom_text(size=2, ggplot2::aes(label=CarsOwned), nudge_y = -35, alpha=0.3) +
        ggplot2::labs(x="Quarter", y=ggplot2::element_blank(), title=paste(type, "in", textpostcodes))
    pplot
    ## pplot + ggplot2::geom_line() + ggplot2::geom_smooth(stat="smooth")
    ## pplot +  #+
#        ggplot2::geom_line() + ggplot2::geom_smooth(stat="smooth")
    } #graphPostcode



graphPostcodeArea <- function(area="Liverpool") {
postcode_vector <- getPostcodeArea(area)
graphPostcode(postcodes=postcode_vector, postcodes_label=area)
    }
mihamn/VEH0122 documentation built on July 11, 2020, 3:45 a.m.