将比例更改为离散值

时间:2014-05-16 19:53:48

标签: r ggplot2

我使用连续刻度创建了一系列11幅不同年份的地图。通过从一个地图移动到下一个地图,人们应该看到值随时间的变化。但是,我使用的是连续刻度,因为某种颜色会引起混淆,例如与另一个相比,红色表示一年内的价值差异很大。因此,我想用固定值范围的离散标度替换连续标度。

您可以在这里找到11张地图中的一张:the data and the original (working) code

这是我尝试将比例转换为离散的,然而,这不起作用:

dat <- read.csv("alexis.csv")

library(reshape)

dat <- melt(dat, id=c("Port", "latitude", "longitude"))

library(ggplot2)
library(cshapes)

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", "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(breaks = c(0, 0.2, 0.5, 1, 1.5, 2, 2.5, 3, 7), labels=NULL) +   
   scale_y_continuous(labels=NULL) +   
   labs(x = NULL, y = NULL)
   scale_fill_manual(breaks = c("0", "0.2", "0.5", "1", "1.5", "2", "2.5", "3", "7"),
   values = c("yellow", "orange", "red", "green", "lightblue", "darkblue", "brown", "black")) +   
   theme(axis.ticks = element_blank(), axis.text.y = element_blank(),
         legend.position="bottom") +
 name = paste(paste("prch", i, sep=""), ".pdf", sep="")
 ggsave(name, p1, width=8, height=4)
}

我收到以下错误消息:

Error in scale_fill_manual(breaks = c("0", "0.2", "0.5", "1", "1.5", "2",  : 
  target of assignment expands to non-language object

我怀疑这可能不是唯一的问题!我非常感谢任何帮助。

0 个答案:

没有答案