使用ggplot无法在散点图顶部生成多边形 [英] Trouble producing a polygon on top of a scatterplot using ggplot

查看:171
本文介绍了使用ggplot无法在散点图顶部生成多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正尝试将我的图形知识从R中的 plot 函数转换为 ggplot 函数。我已经开始为给定的数据集构造散点图和相应的图例,但是我想使用 ggplot 将函数 geom_polygon 合并到我的图中$ C>。



具体来说,我想从散点图的原点捕捉一个三角形区域。为了再现性,假设我有以下数据集:

pre $ rawdata <-data.frame(matrix(c(1,1, 1,
2,1,-1,
3,-1,-1,
4,-1,1,
4,-2,2),5, 3,byrow = TRUE))
名称(rawdata)< -c(Town,x.coordinate,y.coordinate)
rawdata [,1]<因子(rawdata [,1])$ ​​b $ b

为了构造散点图和图例,我被告知要做到以下几点:

  p1 <-ggplot(data = rawdata,aes(x = x.coordinate,y = y。坐标,颜色=城镇,形状=城镇))
+ theme_bw()+ geom_point()

结果如下:
点击此处



我现在想要做的是产生一个多边形。为此,我构建了以下数据框以用于 geom_polygon 函数中:

  geom_polygon(data = polygondata,aes(x = xa,y = ya),color =darkslategray2,
fill =darkslategray2,alpha = 0.25)

然而,当我将它与 p1 结合时,出现以下错误:

  eval中的错误(expr,envir,enclos):object'Town'not found 

从一些混乱中,我注意到当我省略参数时, ggplot 函数,我可以很容易地生成所需的输出,如图所示这里。然而,我希望为美学保持形状

我也会遇到类似的问题,当我尝试生成连接箭头散点图使用 ggplot 。但是,我会在解决这个问题之后,因为根本问题可能在这里。

解决方案

将以下内容添加到 polygondata

  polygondata $ Town = NA 

即使你没有在 geom_polygon 中使用这个变量,ggplot希望它在那里如果该列在ggplot的主要调用中用于审美。



或者,如果您将初始绘图中的美学贴图移至 geom_point ,则可以避免该错误比主ggplot调用,像这样:

  p1 < -  ggplot(data = rawdata)+ 
theme_bw( )+
geom_point(aes(x = x.coordinate,y = y.coordinate,color = Town,shape = Town))

在这种情况下,您不需要为 polygondata 小镇列c>。


Currently, I am trying to transition my graphical knowledge from the plot function in R, to the ggplot function. I have began constructing scatterplots and corresponding legends for a given data set, however I want to incorporate the function geom_polygon onto my plots using ggplot.

Specifically, I want to capture a triangular region from the origin of a scatterplot. For reproducibility, say I have the following data set:

rawdata<-data.frame(matrix(c(1,1,1,
                         2,1,-1,
                         3,-1,-1,
                         4,-1,1,
                         4,-2,2),5,3,byrow=TRUE))
names(rawdata)<-c("Town","x.coordinate","y.coordinate")
rawdata[,1]<-as.factor(rawdata[,1])

To construct a scatterplot along with a legend, I have been told to do the following:

p1<-ggplot(data=rawdata,aes(x=x.coordinate,y=y.coordinate,colour=Town,shape=Town)) 
+ theme_bw() + geom_point()  

The result is the following: Click here.

What I want to do now is produce a polygon. To do so, I have construct the following dataframe to use in the geom_polygon function:

geom_polygon(data=polygondata,aes(x = xa, y = ya),colour="darkslategray2",
             fill = "darkslategray2",alpha=0.25)

However, when I combine this with p1, I get the following error:

Error in eval(expr, envir, enclos) : object 'Town' not found

From some messing around, I have noticed that when I omit the shape argument from the ggplot function, I can easily produce the desired output which is shown here. However, I wish to keep the shape for aesthetics.

I also get a similar problem when I try to produce arrows which connect points on the scatterplot using ggplot. However, I will address this problem after, as the root problem may be here.

解决方案

Add the following to polygondata:

polygondata$Town = NA

Even though you're not using that variable in geom_polygon, ggplot expects it to be there if that column is used for an aesthetic in the main call to ggplot.

Alternatively, I think you could avoid the error if you move the aesthetic mapping in the initial plot to geom_point rather than the main ggplot call, like this:

p1 <- ggplot(data=rawdata) + 
        theme_bw() + 
        geom_point(aes(x=x.coordinate, y=y.coordinate, colour=Town, shape=Town)) 

In that case, you wouldn't need to add a Town column to polygondata.

这篇关于使用ggplot无法在散点图顶部生成多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆