在ggplot中绘制多边形时is.finite(x)中的错误

时间:2019-03-26 13:57:42

标签: r ggplot2

升级到R 3.5.3(在Windows下)并更新所有安装的软件包后,使用ggplot编写的脚本不再起作用。该脚本大胆地尝试通过根据定义的级别填充多边形来绘制地图。

   TimeZone tz = TimeZone.getTimeZone("EST");
    if(tz.useDaylightTime()){
    //Never enters here
    }

用于填充多边形(“ fill.var”,1列x 60002行)的数据来自名为zona.df的数据框

for (i in 1:3) {

fill.var<-zona.df[i+8]    
# Mapping municipalities by level (nivel)
ggp <- ggplot(data=zona.df,aes(x=long,y=lat,group=group)) 
ggp <- ggp + geom_polygon(aes(fill=fill.var))     # draw polygons
ggp <- ggp + geom_path(color="grey", linetype=1)  # draw boundaries
ggp <- ggp + coord_equal() + xlab(" ") + ylab(" ")
ggp <- ggp + scale_fill_gradientn(colours=c("green","yellow","orange","red"),na.value
= "transparent", breaks=c(0,1,2,3),labels=c("Normal","Moderado","Alto","Extremo"),
                                  limits=c(0,3)) ggp <- ggp + coord_map("lagrange")
# Overlay 28 areas 
ggp <- ggp + geom_path(data=muni.df, aes(x=long, y=lat, group=group),color="grey", linetype=1)
ggp <- ggp + geom_path(data=muni_area.df, aes(x=long, y=lat, group=group),color="blue", size=0.4)
# Overlay provinces 
ggp <- ggp + geom_path(data=pr.df, aes(x=long, y=lat, group=group),color="black", size=1)
print(ggp)

以及尝试绘制时的错误消息

> head(zona.df)
  zona       long      lat order  hole piece id group nivel1 nivel2 nivel3
1    1 -0.2777490 40.25998     1 FALSE     1  1   1.1      1      2      3
2    1 -0.2652719 40.25012     2 FALSE     1  1   1.1      1      2      3
3    1 -0.2633704 40.24859     3 FALSE     1  1   1.1      1      2      3
4    1 -0.2448370 40.23688     4 FALSE     1  1   1.1      1      2      3
5    1 -0.2418003 40.23496     5 FALSE     1  1   1.1      1      2      3
6    1 -0.2396455 40.23361     6 FALSE     1  1   1.1      1      2      3

> dput(head(zona.df,30))
structure(list(zona = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), long = c(-0.27774902, 
-0.26527193, -0.26337045, -0.24483699, -0.24180027, -0.2396455, 
-0.2348754, -0.23393393, -0.23880772, -0.24654681, -0.25187707, 
-0.26866825, -0.28338055, -0.29171539, -0.31074526, -0.31179492, 
-0.31203856, -0.31306117, -0.31369715, -0.31414035, -0.31477492, 
-0.31534867, -0.31637842, -0.31722535, -0.3175309, -0.31863818, 
-0.3202096, -0.32107671, -0.32114285, -0.33818943), lat = c(40.25998024, 
40.25011967, 40.24858841, 40.2368797, 40.23496073, 40.2336122, 
40.22491394, 40.22321599, 40.22386393, 40.21180703, 40.20347558, 
40.19665815, 40.20210305, 40.20519903, 40.20969666, 40.20959496, 
40.20967268, 40.21025494, 40.21051292, 40.21089254, 40.21118652, 
40.21123587, 40.21104359, 40.21131546, 40.21131354, 40.21094288, 
40.21012356, 40.20928791, 40.20910028, 40.22051057), order = 1:30, 
    hole = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE), piece = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", 
    "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", 
    "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", 
    "23"), class = "factor"), id = c("1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1"), group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1.1", "2.1", "2.2", 
    "2.3", "3.1", "3.2", "3.3", "4.1", "5.1", "6.1", "6.2", "6.3", 
    "6.4", "7.1", "8.1", "9.1", "10.1", "10.2", "11.1", "11.2", 
    "12.1", "12.2", "13.1", "13.2", "13.3", "13.4", "13.5", "13.6", 
    "13.7", "13.8", "14.1", "15.1", "16.1", "17.1", "17.2", "17.3", 
    "17.4", "17.5", "18.1", "18.2", "19.1", "19.2", "20.1", "20.2", 
    "20.3", "20.4", "20.5", "20.6", "20.7", "20.8", "20.9", "20.10", 
    "20.11", "20.12", "20.13", "20.14", "20.15", "20.16", "20.17", 
    "20.18", "20.19", "20.20", "20.21", "20.22", "21.1", "21.2", 
    "21.3", "21.4", "22.1", "23.1", "24.1", "24.2", "24.3", "24.4", 
    "24.5", "24.6", "24.7", "24.8", "24.9", "24.10", "24.11", 
    "24.12", "24.13", "24.14", "24.15", "24.16", "24.17", "24.18", 
    "24.19", "24.20", "24.21", "24.22", "24.23", "25.1", "26.1", 
    "27.1", "28.1"), class = "factor"), nivel1 = c(1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), nivel2 = c(2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), 
    nivel3 = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L)), row.names = c(NA, 30L), class = "data.frame")

> class(fill.var)
[1] "data.frame"
> dput(head(fill.var,30))
structure(list(nivel1 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L)), row.names = c(NA, 30L), class = "data.frame")
  

不知道如何为类型的对象自动选择比例   data.frame。默认为连续。 is.finite(x)中的错误:默认   类型“列表”未实现的方法

会话信息:

print(ggp)

也许问题出在ggplot是在R 3.5.2下构建的?我应该降级ggplot2吗?要从github安装dev版本吗?

预先感谢

0 个答案:

没有答案