Icons as x-axis labels in R - ggplot2

后端 未结 1 729
耶瑟儿~
耶瑟儿~ 2020-12-19 12:27

(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 ggplot is based

相关标签:
1条回答
  • 2020-12-19 13:01

    This builds of your attempt.

    (I used 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))

    Continuing from above:

    # 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

    There will be a way to do this properly / in line with the lovely previous solution, but anyways ...

    0 讨论(0)
提交回复
热议问题