Adding individual arrows in multiple plots

前端 未结 1 1117
我在风中等你
我在风中等你 2021-01-16 08:47

I want to add arrows in 2 plots produced with ggplot and faceting. Problem: how can I avoid a replication of the arrow in both graphs? I would like to add individual arrows

相关标签:
1条回答
  • 2021-01-16 09:46

    As far as I can tell, to add individual layers onto a facet, you need to supply a data frame with the corresponding facet value.

    From the ggplot2 facet_grid page:

    # If you combine a facetted dataset with a dataset that lacks those 
    # facetting variables, the data will be repeated across the missing 
    # combinations: 
    

    So just doing xp + geom_segment(aes(x,xend,y,yend)) will draw on all facets, because the facetting variable is missing.

    Your facetting variable is z, so you can either:

    • create a dataframe arrow.df with x, y, and z being 'A'
    • feed the z into aes directly.

    Second option seems handier:

    xp + geom_segment(aes(x=4,xend=4,y=f1x4+3,yend=f1x4,z='A')  # <-- see the z='A'
                          , arrow=arrow(length=unit(0.4,"cm")
                            )
                         ) +
          geom_text(aes(x=4,y=f1x4+5, label="a",z='A'))         # <-- see the z='A'
    

    So you just add in a factorvariablename=factor to the aes argument to plot on that particular panel (since your z column in the data frame for xp has levels 'A' and 'B').

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