ggplot2 x - y axis intersect while keeping axis labels

前端 未结 1 1791
醉梦人生
醉梦人生 2020-12-21 15:53

I posted my original question yesterday which got solved perfectly here

Original post

I made a few addition to my code

library(lubridate)
li         


        
相关标签:
1条回答
  • 2020-12-21 16:23

    ggplot2 doesn't make this easy. Below is one-way to approach this interactively. Basically, you just grab the relevant part of the plot (the axis line and ticks) and reposition them.

    If p is your plot

    p
    grid.force()
    # grab the relevant parts - have a look at grid.ls()
    tck <- grid.gget("axis.1-1-1-1", grep=T)[[2]] # tick marks
    ax <- grid.gget("axis.line.x", grep=T)        # x-axis line
    
    # add them to the plot, this time suppressing the x-axis at its default position
    p + lapply(list(ax, tck), annotation_custom, ymax=0) + 
              theme(axis.line.x=element_blank(), 
                    axis.ticks.x=element_blank())
    

    Which produces

    A quick note: the more recent versions of ggplot2 have the design decision to not show the axis. Also changes to axis.line are not automatically passed down to the x and y axis. Therefore, I tweaked your theme to define axis.line.x and axis.line.y separately.

    That siad, perhaps its easier (and more robust??) to use geom_hline as suggested in the comments, and geom_segment for the ticks.

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