2014-05-12 82 views
0

我想找到一種方法來取代11個時間段的連續標度到離散標度,我的意思是我有一個標度,其中分類是按百分比完成的,但現在我想用相應的11個時間段替換它在線路12(年)年如何用R代替地圖中的連續比例到離散比例尺?

請,如果有人能幫助我,我真的很感激它,在接下來的環節是數據庫,將R代碼(下同)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) 
} 

回答

1

我d從這樣的事情開始只是爲了檢查這是否是所需的輸出種類

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

編輯:用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