将geom_path和geom_text添加到同一ggplot中会在r中产生错误 [英] Adding geom_path and geom_text to the same ggplot generates error in r

查看:143
本文介绍了将geom_path和geom_text添加到同一ggplot中会在r中产生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在同一ggplot中绘制geom_path对象和geom_text对象,但是遇到以下问题:

I am plotting a geom_path object and a geom_text object in the same ggplot but am running into the following problem:

#load the data frames
df1 <- data.frame(x=c(32, 42, 52), y=c(15, 20, 25), grp=c(1, 2, 2), site=c("A", "B", "C"))
df1$grp = factor(df1$grp)
colnames(df1)[3] = "Group"

df2 <- data.frame(x=c(32, 42, 52), y=c(15, 20, 25))

#create basic plot with site name coloured by group 
p = ggplot(df1, aes(x=x, y=y, label=site))
p = p + geom_text(aes(colour=factor(Group)), size=4)
p = p + coord_fixed()

#I try adding a path
p = p + geom_path(data=df2, aes(x=x, y=y)) 

但是得到错误 eval(expr,envir,enclos)中的错误:找不到对象站点"

But get the error Error in eval(expr, envir, enclos) : object 'site' not found

有什么想法吗?

推荐答案

在每个后续的geom_中,主要ggplot调用中的每一种美学都应有.解决方案是将label = site移动或通过将label = site设置为NULLgeom_path中取消映射.

Every aesthetic in the main ggplot call is expected in every subsequent geom_. The solution is either to move label = site or unmap it in geom_path by setting it to NULL there.

这篇关于将geom_path和geom_text添加到同一ggplot中会在r中产生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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