Skip to content

Instantly share code, notes, and snippets.

@yutannihilation
Created October 23, 2018 21:37
Show Gist options
  • Select an option

  • Save yutannihilation/e6f5c19b692bd759301588b9162b0153 to your computer and use it in GitHub Desktop.

Select an option

Save yutannihilation/e6f5c19b692bd759301588b9162b0153 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(gghighlight)

d <- mtcars %>%
  #filter(drat >= 3) %>%
  select(mpg,disp,hp,drat) %>%
  gather(variable, value, -disp, -hp, -drat) 
d
#>     disp  hp drat variable value
#> 1  160.0 110 3.90      mpg  21.0
#> 2  160.0 110 3.90      mpg  21.0
#> 3  108.0  93 3.85      mpg  22.8
#> 4  258.0 110 3.08      mpg  21.4
#> 5  360.0 175 3.15      mpg  18.7
#> 6  225.0 105 2.76      mpg  18.1
#> 7  360.0 245 3.21      mpg  14.3
#> 8  146.7  62 3.69      mpg  24.4
#> 9  140.8  95 3.92      mpg  22.8
#> 10 167.6 123 3.92      mpg  19.2
#> 11 167.6 123 3.92      mpg  17.8
#> 12 275.8 180 3.07      mpg  16.4
#> 13 275.8 180 3.07      mpg  17.3
#> 14 275.8 180 3.07      mpg  15.2
#> 15 472.0 205 2.93      mpg  10.4
#> 16 460.0 215 3.00      mpg  10.4
#> 17 440.0 230 3.23      mpg  14.7
#> 18  78.7  66 4.08      mpg  32.4
#> 19  75.7  52 4.93      mpg  30.4
#> 20  71.1  65 4.22      mpg  33.9
#> 21 120.1  97 3.70      mpg  21.5
#> 22 318.0 150 2.76      mpg  15.5
#> 23 304.0 150 3.15      mpg  15.2
#> 24 350.0 245 3.73      mpg  13.3
#> 25 400.0 175 3.08      mpg  19.2
#> 26  79.0  66 4.08      mpg  27.3
#> 27 120.3  91 4.43      mpg  26.0
#> 28  95.1 113 3.77      mpg  30.4
#> 29 351.0 264 4.22      mpg  15.8
#> 30 145.0 175 3.62      mpg  19.7
#> 31 301.0 335 3.54      mpg  15.0
#> 32 121.0 109 4.11      mpg  21.4

ggplot(d, aes(disp,value,color=factor(variable))) +
  #facet_wrap( ~ drat, labeller = label_both) +
  geom_point() +
  gghighlight(drat >= 3, use_group_by = FALSE) +
  geom_smooth(method='loess')

Created on 2018-10-24 by the reprex package (v0.2.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment