图标作为R-ggplot2中的x轴标签 [英] Icons as x-axis labels in R - ggplot2
问题描述
(这是图标作为x轴标签的扩展它会寻找一个 ggplot 解决方案而不是 plot ,因为 我想绘制像这样的(来自本文),其中图标(本例中为小图)用作刻度标签。 原始问题的答案是: 我们如何使用 ggplot 来做类似的绘图? 这是我越接近: 这是您尝试的构建方式。 (我在 从上面继续: $ b (This is an extension to the question Icons as x-axis labels in R. It looks for a ggplot solution instead of a plot one. Since I would like to plot something like this (from this paper) where icons, in this case small graphs, are used as tick labels. The accepted answer from the original question is: How can we make a similar plot using ggplot? This is the closer I get: This builds of your attempt. (I used Continuing from above: There will be a way to do this properly / in line with the lovely previous solution, but anyways ... 这篇关于图标作为R-ggplot2中的x轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! ggplot
是基于 grid 和 plot
基于 graphics ,方法非常不同)
library(igraph)
npoints< - 15
y < - rexp(npoints)
x
#在底部保证金(外部保证金)上保留一些额外空间
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)
#图形编号
x = 1:npoints
#将偏移量添加到第一个图形中心
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')$ b $ (i,x1 [i],x2 [i],sep =';
$ b打印'))
#删除igraph周围的绘图边距(3月),所以它们看起来更大,避免
#`图形边距过大错误
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 )
}
library(ggplot2)
图书馆(grImport)
图书馆(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文件
postscript(file = fileps,fonts = c(serif,Palatino))
plot(graph.ring(i),vertex.label.family =serif,edge。
dev.off()
#转换为符号可访问的xml
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)
rexp $ c之前使用
set.seed(1)
$ c>函数并调整图形以增加边缘厚度: plot(graph.ring(i),vertex.label = NA,edge.width = 30)
)
#初始绘图
p < - qplot(x,y,geom = c(line,point))
#使用图获得标签的x位置
g < - ggplotGrob(p)
xpos < - g $ grobs [[grep(axis-b,g $ layout $ name)]] $ children [[2]] $ grobs [[2]] $ x
#创建grob树
my_g< - do.call(grobTree,Map(symbolsGrob,pics,x = xpos,y = 0.5))
#制作第二张图
#为图片添加额外空间
#删除x轴细节
#注意注释低于y轴下限
#通过检查选择限制
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))
#删除裁剪,使图像呈现
g< - ggplotGrob(p2)
g $ layout $ clip [g $ layout $ name ==panel]< - off
$ b $ grid.newpage()
grid.draw(g)
ggplot
is based on grid and plot
is based on graphics, the approach is very different)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)
}
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)
set.seed(1)
before the rexp
function and also tweaked the graph to increase the edge thickness: 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)