【发布时间】:2015-04-29 09:22:59
【问题描述】:
(这是问题Icons as x-axis labels in R 的扩展。它寻找ggplot 解决方案而不是plot 解决方案。因为ggplot 基于grid和plot是基于graphics的,做法很不一样)
我想绘制类似(from this paper) 的图,其中图标(在本例中为小图)用作刻度标签。
原始问题接受的答案是:
library(igraph)
npoints <- 15
y <- rexp(npoints)
x <- seq(npoints)
# reserve some extra space on bottom margin (outer margin)
par(oma=c(3,0,0,0))
plot(y, xlab=NA, xaxt='n', pch=15, cex=2, col="red")
lines(y, col='red', lwd=2)
# graph numbers
x = 1:npoints
# add offset to first graph for centering
x[1] = x[1] + 0.4
x1 = grconvertX(x=x-0.4, from = 'user', to = 'ndc')
x2 = grconvertX(x=x+0.4, from = 'user', to = 'ndc')
for(i in x){
print(paste(i, x1[i], x2[i], sep='; '))
# remove plot margins (mar) around igraphs, so they appear bigger and
# `figure margins too large' error is avoided
par(fig=c(x1[i],x2[i],0,0.2), new=TRUE, mar=c(0,0,0,0))
plot(graph.ring(i), vertex.label=NA)
}
我们如何使用 ggplot 制作类似的情节?
这是离我最近的地方:
library(ggplot2)
library(grImport)
library(igraph)
npoints <- 5
y <- rexp(npoints)
x <- seq(npoints)
pics <- vector(mode="list", length=npoints)
for(i in 1:npoints){
fileps <- paste0("motif",i,".ps")
filexml <- paste0("motif",i,".xml")
# Postscript file
postscript(file = fileps, fonts=c("serif", "Palatino"))
plot(graph.ring(i), vertex.label.family="serif", edge.label.family="Palatino")
dev.off()
# Convert to xml accessible for symbolsGrob
PostScriptTrace(fileps, filexml)
pics[i] <- readPicture(filexml)
}
xpos <- -0.20+x/npoints
my_g <- do.call("grobTree", Map(symbolsGrob, pics, x=xpos, y=0))
qplot(x, y, geom = c("line", "point")) + annotation_custom(my_g, xmin=-Inf, xmax=Inf, ymax=0.4, ymin=0.3)
【问题讨论】:
-
一点也不!这个要求 ggplot 解决方案。
-
三:a)利用ggplot的能力(易用性、功能等)在近期进一步完善剧情。 (b) 我在代码的其他部分使用 ggplot (同质性) (c) 有趣和好奇。
-
@hrbrmstr 另请注意,使用 opts(axis.text.x = my_axis()) 已弃用
-
@hrbrmstr 您链接的解决方案对于
ggplot2的最新版本根本不起作用