ggplot在奇怪的情况下抛出一个错误

编程入门 行业动态 更新时间:2024-10-27 17:12:59
本文介绍了ggplot在奇怪的情况下抛出一个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图运行以下代码:

p < - ggplot(data = cuernavaca.map,aes( long),lat,group = group))p + geom_polygon(size = 0.1,aes(fill = pobtot))+ coord_equal()+ facet_wrap(〜nombre_municipio,scales =free)+ coord_equal()+ scale_fill_gradient2低=蓝,高=红,中点=平均值(cuernavaca.map $ pobtot,na.rm = TRUE))+ tema.mapas + ggtitle(Cuervaca y Zona metropolitana \\\PoblaciónTotal)+ labs填写=Población总)

但是我得到了这个错误:

错误:ggplot2不知道如何处理不同类的数据

对象 cuernavaca.map 是强化的 SpatialDataFrame 。

奇怪的是,这只发生在一台机器上(Ubuntu 13.04,RStudio 0.97.551,R 3.0.1),当我执行 knit2html 或者我点击按钮编织HTML ,如果我执行con中的代码行唯一的(RStudio内部)一切正常(我的意思是,情节完成),如果我剪切和粘贴在其他 .Rmd文件中的代码,它的工作原理和如果我执行它另一个Ubuntu盒子(与上面相同的规格)或MacOS X,它的工作原理!

有什么想法?

更新:

正如写在评论中,问题似乎是未命名参数的顺序,所以我将代码更改为:

ggplot(data = cuernavaca.map)+ geom_polygon(mapping = aes(x = long,y = lat ,group = group,color = NA,fill = pobtot))+ coord_equal()+ facet_wrap(〜nombre_municipio,scales =free)+ scale_fill_gradient2(low =blue ,high =red,midpoint = mean(cuernavaca.map $ pobtot, na.rm = TRUE))+ tema.mapas + ggtitle(Cuervaca y Zona metropolitana \ nPoblaciónTotal)+ labs(fill =Poblacióntotal)

知道就是一切工作......差不多。

如果您看到代码,我会删除 geom_polygon 中的 size 参数,然后设置 color 到 NA ,我这样做并非出于美观的原因,而是因为这些参数不起作用:多边形的宽度变得很大,颜色被忽略,有时会变成浅红色,在其他情况下,它会消失......

有趣的是,一个,现在也在工作,但猜猜是什么?代码如下:

ggplot(legend = FALSE)+ geom_polygon(data = cuernavaca.map,size = 0.1,aes(long, lat,group = group,color =grey80,fill = ind_vul))+ scale_fill_gradient2(low =blue,high =red,midpoint = mean (cuernavaca.map $ ind_vul,na.rm = TRUE))+ geom_point(data = puntos.df,aes(as.numeric(long),as.numeric(lat),color = geografico),size = 2)+ facet_wrap(〜municipio,scales =free)+ tema.mapas + labs(fill =Índicede vulnerabilityrabilidad)

puntos.df 是 data.frame 有经度和纬度,所以没有什么大不了的,但不是 size 参数超出 aes 和正在工作(我没有得到不知道如何处理数字......等等等等)的错误)

可能是内存问题?

解决方案

正如编辑中所述,答案是使用命名参数:

ggplot(data = cuernavaca.map)+ geom_polygon(mapping = aes(x = long,y = lat,group = group,color = NA,fill = pobtot))+ coord_equal()+ facet_wrap(〜nombre_municipio,scales =free)+ scale_fill_gradient2(low =blue ,high =red,midpoint = mean(cuernavaca.map $ pobtot,na.rm = TRUE))+

有一些没有答案的问题,但我发布了一个新的: ggplot与点的映射:大小,颜色,传奇,美学问题

I am trying to run this following code:

p <- ggplot(data=cuernavaca.map, aes(long, lat, group=group)) p + geom_polygon(size=0.1, aes(fill=pobtot)) + coord_equal() + facet_wrap(~nombre_municipio,scales="free") + coord_equal() + scale_fill_gradient2(low="blue", high="red", midpoint=mean(cuernavaca.map$pobtot, na.rm=TRUE))+ tema.mapas + ggtitle("Cuervaca y Zona metropolitana\nPoblación Total") + labs(fill="Población total")

but I got this error:

Error: ggplot2 doesn't know how to deal with data of class uneval

The object cuernavaca.map is a fortified SpatialDataFrame.

The weird thing is that this only happens in one machine (Ubuntu 13.04, RStudio 0.97.551, R 3.0.1) when I execute the knit2html or I hit the button knit HTML, If I execute the lines of code in the console (inside RStudio) everything works (I mean, the plot is done), If I cut and paste that code in other .Rmd file it works and If I execute it in another Ubuntu box (same specs as above) or MacOS X, it works!

Any ideas?

UPDATE:

As is written in the comments, the problem seems to be the order of the unnamed parameters, so I change the code to this:

ggplot(data = cuernavaca.map) + geom_polygon(mapping = aes(x = long, y = lat, group = group, colour = NA, fill = pobtot)) + coord_equal() + facet_wrap(~nombre_municipio, scales = "free") + scale_fill_gradient2(low = "blue", high = "red", midpoint = mean(cuernavaca.map$pobtot, na.rm = TRUE)) + tema.mapas + ggtitle("Cuervaca y Zona metropolitana\nPoblación Total") + labs(fill = "Población total")

And know is everything working ... almost.

If you see the code I remove the size parameter in the geom_polygon and I set the colour to NA, I made that, not for aesthetics reasons, but because, that parameters are not working: the line width of the polygon gets enormous and the color is just ignored, sometimes gets light red, in others it just disappears...

As a funny thing, the plots below this one, are working now, too, but guess what? The code is the following:

ggplot(legend = FALSE) + geom_polygon(data = cuernavaca.map, size = 0.1, aes(long, lat, group = group, colour = "grey80", fill = ind_vul)) + scale_fill_gradient2(low = "blue", high = "red", midpoint = mean(cuernavaca.map$ind_vul, na.rm = TRUE)) + geom_point(data = puntos.df, aes(as.numeric(long), as.numeric(lat), colour = geografico), size = 2) + facet_wrap(~municipio, scales = "free") + tema.mapas + labs(fill = "Índice de vulnerabilidad")

puntos.df is a data.frame with latitude and longitude, so no big deal, but not how the size parameters is out of the aes and is working (I am not getting a error of "not know how to deal with a numeric... blah blah")

Could be a memory issue?

解决方案

As is told in the edit, the answer was using named parameters:

ggplot(data = cuernavaca.map) + geom_polygon(mapping = aes(x = long, y = lat, group = group, colour = NA, fill = pobtot)) + coord_equal() + facet_wrap(~nombre_municipio, scales = "free") + scale_fill_gradient2(low = "blue", high = "red", midpoint = mean(cuernavaca.map$pobtot, na.rm = TRUE)) +

There are some unanswered questions, but I posted a new one in: ggplot map with points: size, colour, legend, aesthetics problems

更多推荐

ggplot在奇怪的情况下抛出一个错误

本文发布于:2023-10-04 19:13:23,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:抛出   情况下   奇怪   错误   ggplot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!