我有同样的问题,这是在前面发布的: R-style axes with ggplot 我尝试过baptiste建议的解决方案:
library(ggplot2)
d <- data.frame(x=1:10, y=rnorm(10))
base_breaks_x <- function(x){
b <- pretty(x)
d <- data.frame(y=-Inf, yend=-Inf, x=min(b), xend=max(b))
list(geom_segment(data=d, aes(x=x, y=y, xend=xend, yend=yend)),
scale_x_continuous(breaks=b))
}
base_breaks_y <- function(x){
b <- pretty(x)
d <- data.frame(x=-Inf, xend=-Inf, y=min(b), yend=max(b))
list(geom_segment(data=d, aes(x=x, y=y, xend=xend, yend=yend)),
scale_y_continuous(breaks=b))
}
ggplot(d, aes(x,y)) +
geom_point() +
theme_bw() +
opts(panel.border = theme_blank(),
panel.grid.major = theme_blank(),
panel.grid.minor = theme_blank()) +
base_breaks_x(d$x) +
base_breaks_y(d$y)
并发现这只是在情节美学时才起作用 只是由aes(x,y)组成。从框架中绘制数据 一列包含颜色改变的因子,例如
d <- data.frame(x=1:10, y=rnorm(10),name=rep(c("blue","red"),5))
ggplot(d, aes(x,y,colour=name))+ ...
给出错误消息
"Error in eval(expr, envir, enclos) : object 'name' not found"
如何解决这个问题?
非常感谢你的帮助!
答案 0 :(得分:0)
您收到此错误是因为您在colour=name
电话中设置了ggplot()
,而base_breaks_x()
和base_breaks_y()
包含geom_segment()
调用也尝试查找变量{{ 1}}在这些函数的数据帧内。有两种方法可以解决这个问题。
首先,将name
从colour=name
移至ggplot()
的{{1}}。
aes()
其次,通过在geom_point()
来电中添加ggplot(d, aes(x,y)) +
geom_point(aes(colour=name))
来修改功能base_breaks_x()
和base_breaks_y()
。
inherit.aes=FALSE