R 您能否在ggplot2的顶部/右侧轻松绘制地毯/轴?
以下示例没有固有的含义。。。它只是为了展示标签、地毯等的特殊位置,并代表[编辑](a)我正在进行的一个更大的项目,我无法详细讨论,(b)需要使用ggplot,以及(c)需要图形的视觉特征,类似于下面给出的图中所反映的图形 是否可以直接使用ggplot2或通过修改网格来重新创建以下内容R 您能否在ggplot2的顶部/右侧轻松绘制地毯/轴?,r,ggplot2,R,Ggplot2,以下示例没有固有的含义。。。它只是为了展示标签、地毯等的特殊位置,并代表[编辑](a)我正在进行的一个更大的项目,我无法详细讨论,(b)需要使用ggplot,以及(c)需要图形的视觉特征,类似于下面给出的图中所反映的图形 是否可以直接使用ggplot2或通过修改网格来重新创建以下内容 x <- rnorm(20) y <- rnorm(20) plot(x, y, axes=F, xlab="", ylab="") axis(side = 1, at = round(mean(x
x <- rnorm(20)
y <- rnorm(20)
plot(x, y, axes=F, xlab="", ylab="")
axis(side = 1, at = round(mean(x), 2))
axis(side = 2, at = round(mean(y), 2))
axis(side = 3, at = round( range(x), 2 ))
axis(side = 4, at = round( range(y), 2 ))
rug(x, side=3)
rug(y, side=4)
x我将回应@Gavin的问题,但为了避免麻烦,这应该会让你非常接近:
qplot(x,y) +
geom_segment(data = data.frame(x), aes(x = x, y = max(x) - .05, xend = x, yend = max(x))) + #x-rug
geom_segment(data = data.frame(x), aes(x = min(x), y = max(x), xend = max(x), yend = max(x))) + #x-rug
geom_segment(data = data.frame(y), aes(x = max(x) + .05, y = y, xend = max(x), yend = y)) + #y-rug
geom_segment(data = data.frame(y), aes(x = max(x) + .05, y = min(y), xend = max(x) + .05, yend = max(y) )) + #y-rug
scale_x_continuous(breaks = NA) +
scale_y_continuous(breaks = NA) +
xlab(NULL) +
ylab(NULL) +
geom_text(aes(label = round(mean(x),2), x = mean(x), y = min(y) - .2), size = 4) +
geom_text(aes(label = round(mean(y),2), x = min(x) - .2, y = mean(y)), size = 4) +
geom_text(aes(label = round(max(x),2), x = max(x) + .2, y = max(y) + .2), size = 4)
#...add other text labels to your heart's desire.
如果不需要将地毯放在顶部和右侧,可以利用geom_rug()
。我不知道有什么简单的方法可以“移动”x或y轴,使其远离预定义的位置。这样的东西可能更容易消化/使用:
df <- data.frame(x,y)
qplot(x,y, data = df, geom = c("point", "rug")) # + ...any additional geom's here
df接受的解决方案
蔡斯的回答(修改)
Chase的回答中有一些X和Y不正确,导致顶部/右侧轴意外浮动。。。以下是它的更新版本:
xxx <- function(x, y) {
p <- qplot(x,y) +
geom_segment(data = data.frame(x),
aes(x = x,
y = max(y) + .05,
xend = x,
yend = max(y) + .1 )) + #top-ticks
geom_segment(data = data.frame(x),
aes(x = min(x),
y = max(y) + .1,
xend = max(x),
yend = max(y) + .1 )) + #top-axis
geom_segment(data = data.frame(y),
aes(x = max(x) + .1,
y = y,
xend = max(x) + .05,
yend = y)) + #right-ticks
geom_segment(data = data.frame(y),
aes(x = max(x) + .1,
y = min(y),
xend = max(x) + .1,
yend = max(y) )) + #right-axis
scale_x_continuous(breaks = NA) +
scale_y_continuous(breaks = NA) +
xlab(NULL) +
ylab(NULL) +
geom_text(aes(label = round(mean(x), 2),
x = mean(x),
y = min(y) - .2),
size = 4) +
geom_text(aes(label = round(mean(y), 2),
x = min(x) - .2,
y = mean(y)),
size = 4) +
geom_text(aes(label = round(max(y), 2),
x = max(x) + .5,
y = max(y) + .0),
size = 4) + #right-max
geom_text(aes(label = round(min(y), 2),
x = max(x) + .5,
y = min(y) - .0),
size = 4) + #right-min
geom_text(aes(label = round(max(x), 2),
x = max(x) + .0,
y = max(y) + .2),
size = 4) + #top-max
geom_text(aes(label = round(min(x), 2),
x = min(x) + .0,
y = max(y) + .2),
size = 4) #top-min
}
x <- rnorm(20)
y <- rnorm(20)
(xxx(x, y))
到
来自
x0 = unit(0, "npc"), x1 = unit(0.03, "npc"),
到
库(ggplot2)
GeomRugAlt想要ggplot2解决方案的原因是什么?这只是你想要绘制的一系列面板中的一个吗?如果不是这样,我看不出ggplot2或lattice over base graphics对于像这样简单的东西有什么好处,而lattice/ggplot等价物将涉及到一定程度的修改以实现相同的结果。(IMHO)@William唯一的迹象是“更大的计划”,它可以掩盖许多罪恶;-)什么不是“那么简单”?单个数据集的绘图还是示例?除非您需要ggplot2/lattice绘制条件图的功能,否则它们在这方面太过复杂了。现在,如果你想让一个绘图与你已经在ggplot2中完成的其他现有绘图相匹配,作为这个项目的一部分,我可以理解Q。那么说“…我正在处理的一个更大的项目,它使用ggplot2进行图形/绘图”有什么错?我明白。我不是唯一一个想知道为什么需要ggplot2的人,所以这并不明显。我同意在Q中提供无休止的细节/上下文是没有用的,但当有人要求更多的上下文时,傲慢是没有用的。@Gavin:还有。。。如果你读了这篇文章,我不认为这里有“傲慢”。。。这种解释完全是由于在以计算机为媒介的交流中失去了音频/视频线索。请重新阅读我的评论,假设我不是故意刁难你,我希望你能看到它们只是关于如何提供反馈的反馈,受ASCII和500字符限制的约束。@William感谢更新Q(+1)。一些评论/编辑已在电子邮件中传递。我当然是本着上述@Joshua评论的精神询问了我的原始评论。有一种倾向是使用它,因为在很多情况下,它并不总是最有用的工具,但它却是街区里最酷的新手。别误会我的意思,我真的很喜欢ggplot2,而且我一直都在使用它。根据我的经验,ggplot的高级特性常常会使自定义的绘图更难生成。@Chase,close的确。。。“但我知道我更喜欢哪一个情节,”加文-完全同意,尽管有些丑陋之处可能会被包装成一个R函数。我希望有人会过来炫耀一下网格编辑技巧,好好利用哈德利书的附录C……我已经读了好几遍了,但还是不能完全理解它。另一方面,常见的“地毯”在哪里/我在任何工作中都没有遇到过它们,但我可以看到它们对某些应用有帮助。一个有趣的解决方案。。。我很确定我能做到。非常感谢。地毯便于显示简单的分布,特别是在ggplot支持alpha。。。我想会有一个解决方案,可以使用分段Grob或类似的,但这似乎是不必要的低水平。。。特别是因为选择地毯的侧面进行绘制在基础图形中非常(高级)简单。如果有人有其他解决方案,我很乐意看到。除了@William对rugs的评论外,如果您不直接显示数据(适合于rugs的平滑函数),或者在显示双变量数据时不容易识别观测值的单变量分布(如William的数据/示例),rugs(rugs)往往很有用。
y0 = unit(1.02, "npc"), y1 = unit(1.05, "npc"),
x0 = unit(0, "npc"), x1 = unit(0.03, "npc"),
x0 = unit(1.02, "npc"), x1 = unit(1.05, "npc"),
library(ggplot2)
GeomRugAlt <- proto(Geom, {
draw <- function(., data, scales, coordinates, ...) {
rugs <- list()
data <- coordinates$transform(data, scales)
if (!is.null(data$x)) {
rugs$x <- with(data, segmentsGrob(
x0 = unit(x, "native"), x1 = unit(x, "native"),
y0 = unit(1.02, "npc"), y1 = unit(1.05, "npc"),
gp = gpar(col = alpha(colour, alpha), lty = linetype, lwd = size * .pt)
))
}
if (!is.null(data$y)) {
rugs$y <- with(data, segmentsGrob(
y0 = unit(y, "native"), y1 = unit(y, "native"),
x0 = unit(1.02, "npc"), x1 = unit(1.05), "npc"),
gp = gpar(col = alpha(colour, alpha), lty = linetype, lwd = size * .pt)
))
}
gTree(children = do.call("gList", rugs))
}
objname <- "rug_alt"
desc <- "Marginal rug plots"
default_stat <- function(.) StatIdentity
default_aes <- function(.) aes(colour="black", size=0.5, linetype=1, alpha = 1)
guide_geom <- function(.) "path"
examples <- function(.) {
p <- ggplot(mtcars, aes(x=wt, y=mpg))
p + geom_point()
p + geom_point() + geom_rug_alt()
p + geom_point() + geom_rug_alt(position='jitter')
}
})
geom_rug_alt <- GeomRugAlt$build_accessor()
x <- rnorm(20)
y <- rnorm(20)
p <- qplot(x,y)
p
p + geom_rug() + geom_rug_alt()