Icons as x-axis labels in R - ggplot2 -
(this extension question icons x-axis labels in r. looks ggplot solution instead of plot one. since ggplot
based on grid , plot
based on graphics, approach different)
i plot (from paper) icons, in case small graphs, used tick labels.
the accepted answer original question is:
library(igraph) npoints <- 15 y <- rexp(npoints) x <- seq(npoints) # reserve 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 first graph centering x[1] = x[1] + 0.4 x1 = grconvertx(x=x-0.4, = 'user', = 'ndc') x2 = grconvertx(x=x+0.4, = 'user', = 'ndc') for(i in x){ print(paste(i, x1[i], x2[i], sep='; ')) # remove plot margins (mar) around igraphs, appear bigger , # `figure margins large' error 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) }
how can make similar plot using ggplot?
this closer get:
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 xml accessible 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)
this builds of attempt.
(i used set.seed(1)
before rexp
function , tweaked graph increase edge thickness: plot(graph.ring(i), vertex.label=na, edge.width=30)
)
continuing above:
# initial plot p <- qplot(x, y, geom = c("line", "point")) # use plot x-positions of 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 space under plot images # remove x-axis details # note annotation below lower y-axis limit # limits selected 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 images render g <- ggplotgrob(p2) g$layout$clip[g$layout$name=="panel"] <- "off" grid.newpage() grid.draw(g)
there way / in line lovely previous solution, anyways ...
Comments
Post a Comment