Search code examples
rdictionaryscale

How to replace the continuous scale to a discrete scale in a map using R?


I want to find the way to replace the continue scale to a discrete scale for the 11 time periods, I mean I have a scale in which the classification is done by percents, but now I wanna replace it by the 11 periods that correspond to the years in the line 12(years)

Please if someone can help me I really appreciate it, in the next link is the database and the R code (same below) https://www.dropbox.com/sh/rctlpzcv5nx0aun/AAAmRpSfSLiDeEu6Q0svr-tga

dat = read.csv("alexis.csv")
dat = melt(dat, id=c("Port", "latitude", "longitude"))

library(ggplot2)
library(cshapes)
library(reshape)

world = cshp(date=as.Date("2008-1-1"))
world.points = fortify(world)

names = unique(dat$variable)
year = c("1843", "1840-1845", "1869", "1866-1870", "1894", "1900 Dec.", 
     "1912", "1922", "1932", "1940", "1950") 

dat$value = dat$value*100
dat$Pdiff = "<20%"
dat$Pdiff[dat$value<50 & dat$value>=20] = "<50%"
dat$Pdiff[dat$value<100 & dat$value>=50] = "<100%"
dat$Pdiff[dat$value<150 & dat$value>=100] = "<150%"
dat$Pdiff[dat$value<200 & dat$value>=150] = "<200%"
dat$Pdiff[dat$value<250 & dat$value>=200] = "<250%"
dat$Pdiff[dat$value<300 & dat$value>=250] = "<300%"
dat$Pdiff[dat$value>=300] = "<=300%"
dat$Pdiff = factor(dat$Pdiff)

reorder(X = dat$Pdiff, dat$Pdiff, new.order=c("<20%","<50%","<100%","<150%",
    "<200%","<250%","<300%", "<=300%"))

years = c("1843", "1840-1845", "1869", "1866-1870", "1894", "Dec 1900",
      "1912", "1922", "1932", "1940", "1950")

for(i in 1:length(names)){
temp = na.omit(dat[dat$variable==names[i],])

p1 = ggplot(world.points, aes(long, lat, group=group)) +  
geom_polygon(fill="white") +
geom_point(data=temp, aes(x=longitude, y=latitude, group=value, col=value)) +
coord_equal() + 
scale_x_continuous(labels=NULL) +   
scale_y_continuous(labels=NULL) +   
labs(x = NULL, y = NULL, fill = "% Irish") +
scale_colour_gradientn("Price Change %", colours=c("yellow", "red")) +    
theme(axis.ticks = element_blank(), axis.text.y = element_blank(),
     legend.position="bottom") +
ggtitle(years[i])
name = paste(paste("prch", i, sep=""), ".pdf", sep="")
ggsave(name, p1, width=8, height=4)
}

Solution

  • I'd start with something like this just to check if this is the kind of desired output

    df <- read.delim("D:/Programacao/R/Stackoverflow/Nova pasta/alexis.csv",
                     head = T, dec = '.', sep = ',',
                     stringsAsFactors = F)
    names(df)[4:14] = c("1843", "1840to1845", "1869", "1866to1870", "1894", "Dec1900",
              "1912", "1922", "1932", "1940", "1950")
    library(reshape)
    df1 <- melt(df, id=c("Port", "latitude", "longitude"))
    df1$value  <- df1$value*100
    df1$Pdiff <- cut(df1$value, breaks = c(0, 20, 50, 100, 150, 200,
                                           250, 300, max(df1$value, na.rm = T)))
    head(df1, 10)
    library(ggplot2)
    ggplot(aes(x=longitude, y = latitude), data = df1) +
      geom_point(aes(colour = Pdiff)) +
      facet_wrap(~variable) +
      coord_map()
    

    simple example

    EDIT: with ggmap

    library(ggmap)
    map_loc <- get_map(location = c(mean(df1$longitude), mean(df1$latitude)),
                       source = 'google', zoom = 3)
    mapw <- ggmap(map_loc, extent = 'device')
    mapw + 
      geom_point(aes.inherit = F,
                 aes(x = longitude, y = latitude, colour = Pdiff),
                 data = df1) +
      facet_wrap(~variable) + 
      coord_map()
    

    ggmap