Setting facet transitions in stat_contour

I would like to show a contour plot using ggplotand stat_contourfor two categories of my data with facet_grid. I want to highlight a specific level based on data. Here's a similar example of a dummy example using regular data volcano.

library(dplyr)
library(ggplot2)    
v.plot <- volcano %>% reshape2::melt(.) %>% 
      mutate(dummy = Var1 > median(Var1)) %>% 
      ggplot(aes(Var1, Var2, z = value)) + 
      stat_contour(breaks = seq(90, 200, 12)) + 
      facet_grid(~dummy)

Section 1: enter image description here

Say, at each level of factors (probably in the east and west), I want to find the average height of the volcano and show it. I can calculate it manually:

volcano %>% reshape2::melt(.) %>% 
  mutate(dummy = Var1 > median(Var1)) %>% 
  group_by(dummy) %>% 
  summarise(h.bar = mean(value))

# A tibble: 2 × 2
  dummy    h.bar
  <lgl>    <dbl>
1 FALSE 140.7582
2  TRUE 119.3717

Which tells me that the average heights on each half are 141 and 119. I can draw BOTH of those that are on BOTH faces, but not only suitable on each side.

v.plot + stat_contour(breaks = c(141, 119), colour = "red", size = 2)

Section 2: enter image description here

breaks= aes(), . , , - bins=2, , .

!

+4
1

, geom.

volcano %>% reshape2::melt(.) %>% 
  mutate(dummy = Var1 > median(Var1)) %>% 
  group_by(dummy) %>% 
  mutate(h.bar = mean(value),                      # edit1
         is.close = round(h.bar) == value) %>%     #
  ggplot(aes(Var1, Var2, z = value)) + 
  stat_contour(breaks = seq(90, 200, 12)) + 
  geom_point(colour = "red", size = 3,             # edit 2
           aes(alpha = is.close)) +                #
  scale_alpha_discrete(range = c(0,1)) +           #
  facet_grid(~dummy)

edit 1 mutate(), , , value ( ) ( ).

edit2 geom_point, , , alpha 0 .

3: Point points

, , geom_path - . , .

!

+1

Source: https://habr.com/ru/post/1669096/


All Articles