r - 图标作为 R 中的 x 轴标签 - ggplot2

标签 r ggplot2

(这是问题 Icons as x-axis labels in R 的扩展。它寻找 ggplot 解决方案而不是绘图解决方案。由于 ggplot 基于网格而 plot 基于图形,因此方法非常不同)

我想绘制这样的图 (from this paper)其中图标(在本例中为小图形)用作刻度标签。

enter image description here

原始问题的公认答案是:

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)  
}

enter image description here

我们如何使用 制作类似的图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)

enter image description here

最佳答案

这是建立在你的尝试之上的。

(我在 set.seed(1) 函数之前使用了 rexp 并且还调整了图形以增加边缘厚度: plot(graph.ring(i), vertex.label=NA, edge.width=30) )

从上面继续:

# Initial plot
p <- qplot(x, y, geom = c("line", "point")) 

# Use the plot to get the x-positions of the labels
g <- ggplotGrob(p)    
xpos <- g$grobs[[grep("axis-b", g$layout$name)]]$children[[2]]$grobs[[2]]$x

# Create grob tree 
my_g <- do.call("grobTree", Map(symbolsGrob, pics, x=xpos, y=0.5))

# Make second plot
# Add extra space under the plot for the images 
# Remove x-axis details
# Note the annotation is below the lower y-axis limit
# The limits were selected by inspection
p2 <- p + annotation_custom(my_g, xmin=-Inf, xmax=Inf, ymax=-0.1, ymin=-0.2) + 
            theme(axis.text.x = element_blank(), 
                  plot.margin=unit(c(1,1,2,1), "cm"))

# remove clipping so the images render
g <- ggplotGrob(p2)
g$layout$clip[g$layout$name=="panel"] <- "off"

grid.newpage()
grid.draw(g)

enter image description here

将有一种方法可以正确/符合以前可爱的解决方案,但无论如何......

关于r - 图标作为 R 中的 x 轴标签 - ggplot2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29939447/

相关文章:

r - 如何提取多个字符串的匹配部分?

滚动平方根和其他功能

r - 如何在 ggplot2 R 中绘制一条回归线,但按不同因素绘制颜色点?

r - 识别并绘制被 NA 包围的数据点

r - 保存具有透明背景的 R 图时出现问题

r - 设置 ggplot 标题以反射(reflect) dplyr 分组

r - 为 R 图中的每个因子水平分配颜色

r - 图例未出现在 R 图中

r - ggplot 分组 geom_bar - 将因子类别的标签添加到 x 轴标签

r - 如何将图片插入到 ggplot 图中的每个单独的条中