Produce an inset in each facet of an R ggplot while preserving colours of the original facet content

前端 未结 3 465
感情败类
感情败类 2021-02-04 16:18

I would like to produce a graphic combining four facets of a graph with insets in each facet showing a detail of the respective plot. This is one of the things I tried:



        
3条回答
  •  孤街浪徒
    2021-02-04 16:30

    Modifying off @user63230's excellent answer:

    pp <- map(unique(data_frame$max_rep), function(x) {  
      plot2 + 
        aes(alpha = ifelse(max_rep == x, 1, 0)) +
        coord_cartesian(xlim = c(12, 14),
                        ylim = c(3, 4)) +
        labs(x = NULL, y = NULL) +
        scale_alpha_identity() +
        facet_null() +
        theme(
          strip.background = element_blank(),
          strip.text.x = element_blank(),
          legend.position = "none",
          axis.text=element_blank(),
          axis.ticks=element_blank()
        )
    })
    

    Explanation:

    1. Instead of filtering the data passed into plot2 (which affects the mapping of colours), we impose a new aesthetic alpha, where lines belonging to the other replicate numbers are assigned 0 for transparency;
    2. Use scale_alpha_identity() to tell ggplot that the alpha mapping is to be used as-is: i.e. 1 for 100%, 0 for 0%.
    3. Add facet_null() to override plot2's existing facet_wrap, which removes the facet for the inset.

    Everything else is unchanged from the code in the question.

提交回复
热议问题