In ggplot2, how can I change the border of selected facets?

后端 未结 2 705
一个人的身影
一个人的身影 2021-02-08 19:57

Taking the graph from ggplot2 help pages:

ggplot(mtcars, aes(factor(cyl))) + geom_bar() + facet_grid(. ~ vs)

Is it possible to change the borde

2条回答
  •  自闭症患者
    2021-02-08 20:24

    I was trying to implement a facet border as well. I did just a little tweaking of the answer supplied by Hadley in the thread mentioned in the question as follows:

     # Outline colours 
    outline <- data.frame( 
      cyl = c(4, 6, 8), 
      outline_color = c('green', 'orange', 'red') 
    ) 
    
    # Points defining square region for background 
    square <- with(mtcars, data.frame( 
      x = c(-Inf, Inf, Inf, -Inf), 
      y = c(-Inf, -Inf, Inf, Inf)
      ))
    
    ggplot(mtcars, aes(x = mpg, y = wt)) + 
      geom_polygon(aes(x = x,y = y, color = outline_color, fill = NA), data = merge(outline, square)) + 
      geom_point() + 
      scale_fill_identity() + 
      facet_grid(. ~ cyl) 
    

    Produces the following graph with differing facet borders:

提交回复
热议问题