【问题标题】:Object not found error with ggplot2 when adding shape aesthetic添加形状美学时,ggplot2找不到对象错误
【发布时间】:2011-07-21 20:53:01
【问题描述】:

我正在尝试向现有绘图添加形状美学映射,但收到以下错误。有没有不同的方法来实现这一点?如果我从函数调用中删除shape=Port,一切都会按预期进行。

p <- ggplot(data=w, aes(OAD,RtgValInt,color=dt,shape=Port)) +
    geom_jitter(size=3, alpha=0.75) +
     scale_colour_gradient(limits=c(min(w$dt), 
             max(w$dt)),
         low="#9999FF", high="#000066") +
     geom_point(data=data.frame(OAD=w$OAD[1], 
             RtgValInt=w$RtgValInt[1]), 
         color="red", size=3)
print(p)

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

数据框w包含以下数据。

Date          Port    OAD         RtgValInt   dt
12/31/2010  Grp1    1.463771    1.833333    14974
12/31/2010  Grp2    1.193307    2.071429    14974
11/30/2010  Grp1    1.454115    1.833333    14943
11/30/2010  Grp2    1.127755    2.071429    14943
10/29/2010  Grp1    1.434965    2.000000    14911
10/29/2010  Grp2    1.055758    2.071429    14911
09/30/2010  Grp1    1.441773    2.000000    14882
09/30/2010  Grp2    1.077799    2.071429    14882

【问题讨论】:

标签: r ggplot2


【解决方案1】:

由于每一层都继承了默认的aes映射,所以在使用不同的数据集时,需要将geom_point中的shape aes设为无效:

p <- ggplot(data=w, aes(OAD,RtgValInt,color=dt,shape=Port)) +
  geom_jitter(size=3, alpha=0.75) +
  scale_colour_gradient(limits=c(min(w$dt), 
      max(w$dt)),
    low="#9999FF", high="#000066") +
  geom_point(aes(shape=NULL), data=data.frame(OAD=w$OAD[1], 
      RtgValInt=w$RtgValInt[1]), 
    color="red", size=3)

【讨论】:

  • 或者不要在整体绘图默认值中设置形状。
  • 这是在将geom_rect 图层添加到由ggmap() 创建的绘图时发生的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-18
  • 1970-01-01
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
  • 1970-01-01
  • 2017-02-02
相关资源
最近更新 更多