我们可能想aes()
为ggplot()
图形定义一些全局的,但在某些层中排除它们。例如假设以下示例:
foo <- data.frame(x=runif(10),y=runif(10))
bar <- data.frame(x=c(0,1),ymin=c(-.1,.9),ymax=c(.1,1.1))
p <- ggplot(foo,aes(x=x,y=y))+geom_point()
Run Code Online (Sandbox Code Playgroud)
一切都很好。但是,当尝试添加功能区时:
p <- p + geom_ribbon(data=bar, aes(x=x,ymin=ymin,ymax=ymax), alpha=.1)
# Error: Discrete value supplied to continuous scale
Run Code Online (Sandbox Code Playgroud)
发生此错误是因为我们已经定义了y
全局的一部分aes()
,也适用于geom_ribbon()
,但bar
没有它。
我发现了两种避免此错误的可能性,其中之一是y=y
从原来的中删除ggplot(foo,aes(x=x,y=y))
,但是以后每次我需要绘制一些我应该添加到不好的内容y=y
中。aes()
另一种可能性是添加一个假y
列bar
:
bar = cbind(bar, y=0)
p <- p + geom_ribbon(data=bar, aes(x=x,ymin=ymin,ymax=ymax), alpha=.1)
Run Code Online (Sandbox Code Playgroud)
现在效果很好。然而我不喜欢这样做,因为它是一个假变量。有没有办法aes()
在ggplot()
调用时暂时禁用已定义的geom_ribbon()
?
我正在尝试将形状美学映射添加到现有的绘图中,但我收到下面的错误.有没有不同的方法来实现这一目标?如果我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
Run Code Online (Sandbox Code Playgroud)
数据框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
Run Code Online (Sandbox Code Playgroud) 我试图在谷歌地图上绘制一个线层。
数据
> dput(map)
new("SpatialLinesDataFrame"
, data = structure(list(att = c(463643, 2291491, 315237340, 10348934,
309845150, 674351, 58057, 55962, 302861, 1405635)), .Names = "att", row.names = c(NA,
10L), class = "data.frame")
, lines = list(<S4 object of class structure("Lines", package = "sp")>,
<S4 object of class structure("Lines", package = "sp")>,
<S4 object of class structure("Lines", package = "sp")>,
<S4 object of class structure("Lines", package = "sp")>,
<S4 object of class structure("Lines", package = "sp")>,
<S4 object of class structure("Lines", package = …
Run Code Online (Sandbox Code Playgroud)