Jon*_*ley 14 r ggplot2 axis-labels r-grid
我希望我可以将漂亮的导入和绘图功能grImport与令人敬畏的图形功能相结合ggplot2,但我根本不理解grid系统,以找到实现我想要的优雅方式.我想要的是用ggplot2图表导入的图像替换图表中的x轴刻度标签grImport.
由于两个软件包都使用grid函数,我希望有一种方法grid.symbols()可以在ggplot2框架内使用,这是理想的,或者至少在设备中的现有图上使用.知道这些事情的人是否知道这样做的有效方法?或者,任何人都可以向我指出更多信息,以帮助我了解grobs,视口等?我已经阅读了关于Grid图形模型的Paul Murrel的R图形书的免费章节,但是我对ggplot2的内部工作原理并不熟悉,无法建立链接.
我的问题非常类似于使用图像作为绘图点的现有问题,尽管我对轴标签而不是绘图点更感兴趣.但是,我很好奇两个任务的相似程度,以及是否可以为此目的调整该解决方案.我无法自己解决这个问题.
这是我的第一篇文章,所以我不允许发布图像,但是这段代码实现了我想要的东西.注意:这种方法使用了一种丑陋,丑陋的黑客,它不便携,而且不尽如人意.我想要生成的最终绘图将具有facets(facet_grid),并且x轴用于在每个轴刻度处具有不同图片的因子,而不是连续变量,这就是为什么我正在寻找更一般/更强大的解决方案不需要大量的试验和错误.
library(ggplot2)
## library(grImport) # not needed for this example, but would be for grid.symbols()
p <- ggplot(mtcars, aes(cyl, mpg)) + stat_summary(fun.data = "mean_cl_boot")
print(p)
## Replace (in this case, overlay) x-axis tick labels with a graphic / grob
iconSize <- 0.05
iconHt <- 0.2
padding <- 0.09 # horizontal padding around axis: I found this by trial & error
tickSp <- (1-padding)/(4*2)
downViewport("axis_h-5-3")
## I would use grid.symbols() with an imported Picture in place of grid.circle(),
## but the idea is the same: draw a shape at the ticks along the axis.
for (i in 0:(max(mtcars$cyl) - min(mtcars$cyl)) )
{
grid.circle(x = (padding/2 + tickSp*(i*2)), y = iconHt,
r = iconSize*(min(mtcars$cyl)+i), gp = gpar(fill="black"))
}
upViewport()
Run Code Online (Sandbox Code Playgroud)
koh*_*ske 21
这是一个例子:
# convert ps to RGML
PostScriptTrace(file.path(system.file(package = "grImport"), "doc", "GNU.ps"), "GNU.xml")
PostScriptTrace(file.path(system.file(package = "grImport"), "doc", "tiger.ps"), "tiger.xml")
# read xml
pics <- list(a = readPicture("GNU.xml"), b = readPicture("tiger.xml"))
# custom function for x axis label.
my_axis <- function () {
structure(
function(label, x = 0.5, y = 0.5, ...) {
absoluteGrob(
do.call("gList", mapply(symbolsGrob, pics[label], x, y, SIMPLIFY = FALSE)),
height = unit(1.5, "cm"))
}
)}
qplot(factor(c("a", "b")), 1:2) + opts( axis.text.x = my_axis())
Run Code Online (Sandbox Code Playgroud)

对于单个面板,从x轴grob中提取信息非常简单,并将其替换为您自己的.我不确定如何将它干净地扩展到自动多面板轴.
library(grid)
library(ggplot2)
p <- qplot(1:12, rnorm(12))
grid.newpage()
g <- ggplotGrob(p)
grid.draw(g)
g0 <- getGrob(g, gPath("axis.text.x"), grep=TRUE)
grid.set(gPath("axis.text.x"),
pointsGrob(x = g0$x, y=0*g0$x + unit(0.5,"npc"),
pch=19, gp=gpar(col=seq_along(g0$x)),
name = g0$name), grep = TRUE)
Run Code Online (Sandbox Code Playgroud)

下面是使用自定义 grob 轴标签的技巧。
library(grid)
library(ggplot2)
## convert the labels to some parameter to be used in the custom grob
## here simply an index that will be interpreted as color
mapping <- function(x, ...){
seq_along(x)
}
library(grImport)
hourglass <- new("Picture",
paths= list(new("PictureFill",
x=c(0, 1, 0, 1),
y=c(0, 0, 1, 1))),
summary= new("PictureSummary",
numPaths=1,
xscale=c(0, 1),
yscale=c(0, 1)))
## bare bones edit of theme_text()
my_axis <- function ()
{
structure(function(label, x = 0.5, y = 0.5, default.units = "npc", ...) {
cols <- mapping(label)
symbolsGrob(hourglass, x, 0*x + unit(0.5, "npc"),
use.gc=FALSE,size=unit(5,"mm"), gp=gpar(fill=cols))
}, class = "theme", type = "custom", call = match.call())
}
qplot(1:12, rnorm(12)) +
opts( axis.text.x = my_axis(), axis.ticks.margin = unit(0.5, "cm"))
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
2068 次 |
| 最近记录: |