如何使用R将连续比例​​替换为地图中的离散比例?

时间:2014-05-12 15:52:32

标签: r map scale

我想找到在11个时间段内将继续比例替换为离散比例的方法,我的意思是我有一个比例由百分比完成的比例,但现在我想用11个周期代替它对应于第12行(年)中的年份

如果有人可以帮助我,我真的很感激,在下一个链接是数据库和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 个答案:

答案 0 :(得分:1)

我从这样的事情开始,只是为了检查这是否是所需的输出

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