Git Product home page Git Product logo

gghighlight's Introduction

gghighlight

R build status CRAN_Status_Badge

Highlight geoms in ggplot2.

Installation

install.packages("gghighlight")

# Or the development version from GitHub:
# install.packages("devtools")
devtools::install_github("yutannihilation/gghighlight")

Example

(For the full version, please refer to Introduction to gghighlight).

Suppose we have a data that has so many series that it is hard to identify them by their colours as the differences are so subtle.

library(ggplot2)

ggplot(d) +
  geom_line(aes(idx, value, colour = type))

With gghighlight(), we can highlight the lines whose max values are larger than 20:

library(gghighlight)

p <- ggplot(d) +
  geom_line(aes(idx, value, colour = type)) +
  gghighlight(max(value) > 20)
#> label_key: type
p

The result is a usual ggplot object, so it is fully customizable. For example, it can be used with custom themes and facets.

p + theme_minimal()

p + theme_minimal() + facet_wrap(~ type)

gghighlight() can highlight almost any geoms. For more details, please read Introduction to gghighlight.

gghighlight's People

Contributors

jsta avatar ljupch0 avatar maxdrohde avatar teunbrand avatar yutannihilation avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gghighlight's Issues

Keep the same colour scale before and after highlighting

The scale for a continuous variable gets reset when highlighting is applied.

Is it possible to keep the same scale before and after highlighting?

library(gghighlight)
#> Loading required package: ggplot2

set.seed(1)

d <- data.frame(
  idx = c( 1, 1, 1, 2, 2, 2, 3, 3, 3),
  value = c( 1, 2, 3,10,11,12, 9,10,11),
  category = rep(c("a","b","c"), 3),
  cont_var = runif(9),
  stringsAsFactors = FALSE
)

ggplot(d, aes(x = idx, y = value, color = cont_var)) +
  geom_point() +
  scale_color_continuous()

The the values of ticks on the legend of cont_var (and the colors of the non-bleached dots) change after adding gghighlight().

ggplot(d, aes(x = idx, y = value, color = cont_var)) +
  geom_point() +
  scale_color_continuous() +
  gghighlight(cont_var < 0.5)

Created on 2018-11-18 by the reprex package (v0.2.1)

Session info
devtools::session_info()
#> ─ Session info ──────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.5.1 (2018-07-02)
#>  os       Debian GNU/Linux 9 (stretch)
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       Etc/UTC                     
#>  date     2018-11-18                  
#> 
#> ─ Packages ──────────────────────────────────────────────────────────────
#>  package     * version date       lib source        
#>  assertthat    0.2.0   2017-04-11 [1] CRAN (R 3.5.1)
#>  backports     1.1.2   2017-12-13 [1] CRAN (R 3.5.1)
#>  base64enc     0.1-3   2015-07-28 [1] CRAN (R 3.5.1)
#>  bindr         0.1.1   2018-03-13 [1] CRAN (R 3.5.1)
#>  bindrcpp    * 0.2.2   2018-03-29 [1] CRAN (R 3.5.1)
#>  callr         3.0.0   2018-08-24 [1] CRAN (R 3.5.1)
#>  cli           1.0.1   2018-09-25 [1] CRAN (R 3.5.1)
#>  colorspace    1.3-2   2016-12-14 [1] CRAN (R 3.5.1)
#>  crayon        1.3.4   2017-09-16 [1] CRAN (R 3.5.1)
#>  curl          3.2     2018-03-28 [1] CRAN (R 3.5.1)
#>  desc          1.2.0   2018-05-01 [1] CRAN (R 3.5.1)
#>  devtools      2.0.1   2018-10-26 [1] CRAN (R 3.5.1)
#>  digest        0.6.18  2018-10-10 [1] CRAN (R 3.5.1)
#>  dplyr         0.7.8   2018-11-10 [1] CRAN (R 3.5.1)
#>  evaluate      0.12    2018-10-09 [1] CRAN (R 3.5.1)
#>  fs            1.2.6   2018-08-23 [1] CRAN (R 3.5.1)
#>  gghighlight * 0.1.0   2018-07-04 [1] CRAN (R 3.5.1)
#>  ggplot2     * 3.1.0   2018-10-25 [1] CRAN (R 3.5.1)
#>  glue          1.3.0   2018-07-17 [1] CRAN (R 3.5.1)
#>  gtable        0.2.0   2016-02-26 [1] CRAN (R 3.5.1)
#>  htmltools     0.3.6   2017-04-28 [1] CRAN (R 3.5.1)
#>  httr          1.3.1   2017-08-20 [1] CRAN (R 3.5.1)
#>  knitr         1.20    2018-02-20 [1] CRAN (R 3.5.1)
#>  labeling      0.3     2014-08-23 [1] CRAN (R 3.5.1)
#>  lazyeval      0.2.1   2017-10-29 [1] CRAN (R 3.5.1)
#>  magrittr      1.5     2014-11-22 [1] CRAN (R 3.5.1)
#>  memoise       1.1.0   2017-04-21 [1] CRAN (R 3.5.1)
#>  mime          0.6     2018-10-05 [1] CRAN (R 3.5.1)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 3.5.1)
#>  pillar        1.3.0   2018-07-14 [1] CRAN (R 3.5.1)
#>  pkgbuild      1.0.2   2018-10-16 [1] CRAN (R 3.5.1)
#>  pkgconfig     2.0.2   2018-08-16 [1] CRAN (R 3.5.1)
#>  pkgload       1.0.2   2018-10-29 [1] CRAN (R 3.5.1)
#>  plyr          1.8.4   2016-06-08 [1] CRAN (R 3.5.1)
#>  prettyunits   1.0.2   2015-07-13 [1] CRAN (R 3.5.1)
#>  processx      3.2.0   2018-08-16 [1] CRAN (R 3.5.1)
#>  ps            1.2.1   2018-11-06 [1] CRAN (R 3.5.1)
#>  purrr         0.2.5   2018-05-29 [1] CRAN (R 3.5.1)
#>  R6            2.3.0   2018-10-04 [1] CRAN (R 3.5.1)
#>  Rcpp          1.0.0   2018-11-07 [1] CRAN (R 3.5.1)
#>  remotes       2.0.2   2018-10-30 [1] CRAN (R 3.5.1)
#>  rlang         0.3.0.1 2018-10-25 [1] CRAN (R 3.5.1)
#>  rmarkdown     1.10    2018-06-11 [1] CRAN (R 3.5.1)
#>  rprojroot     1.3-2   2018-01-03 [1] CRAN (R 3.5.1)
#>  scales        1.0.0   2018-08-09 [1] CRAN (R 3.5.1)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 3.5.1)
#>  stringi       1.2.4   2018-07-20 [1] CRAN (R 3.5.1)
#>  stringr       1.3.1   2018-05-10 [1] CRAN (R 3.5.1)
#>  testthat      2.0.1   2018-10-13 [1] CRAN (R 3.5.1)
#>  tibble        1.4.2   2018-01-22 [1] CRAN (R 3.5.1)
#>  tidyselect    0.2.5   2018-10-11 [1] CRAN (R 3.5.1)
#>  usethis       1.4.0   2018-08-14 [1] CRAN (R 3.5.1)
#>  withr         2.1.2   2018-03-15 [1] CRAN (R 3.5.1)
#>  xml2          1.2.0   2018-01-24 [1] CRAN (R 3.5.1)
#>  yaml          2.2.0   2018-07-25 [1] CRAN (R 3.5.1)
#> 
#> [1] /usr/local/lib/R/site-library
#> [2] /usr/local/lib/R/library

When use_group_by=TRUE, cannot use group variable in predicates

This error is very unintuitive.

library(ggplot2)
library(gghighlight)

ggplot(iris, aes(Sepal.Width, fill = Species)) +
  geom_histogram() +
  geom_highlight(Species != "setosa") +
  facet_wrap(~Species)
#> Error in summarise_impl(.data, dots): Evaluation error: object 'Species' not found.

Created on 2018-05-23 by the reprex package (v0.2.0).

It's not always good to set colour/fill to grey

library(ggplot2)
library(gghighlight)

p <- ggplot(diamonds, aes(depth, colour = cut)) +
  geom_density() +
  xlim(55, 70)
p
#> Warning: Removed 45 rows containing non-finite values (stat_density).

p + geom_highlight(max(depth) > 65)
#> Warning: Removed 45 rows containing non-finite values (stat_density).

#> Warning: Removed 45 rows containing non-finite values (stat_density).

Created on 2018-05-30 by the reprex package (v0.2.0).

boxplot is highlighted strangely

library(ggplot2)
library(gghighlight)

ggplot(mpg, aes(class, hwy, colour = cyl)) +
  geom_boxplot() +
  geom_highlight(cyl > 4)
#> Warning: You set use_group_by = TRUE, but grouped calculation failed.
#> Falling back to ungrouped filter operation...

ggplot(mpg, aes(class, hwy, colour = cyl, group = class)) +
  geom_boxplot() +
  geom_highlight(cyl > 4)
#> Warning: You set use_group_by = TRUE, but grouped calculation failed.
#> Falling back to ungrouped filter operation...
#> Don't know how to automatically pick scale for object of type function. Defaulting to continuous.
#> Error: Column `x` must be a 1d atomic vector or a list

Implement geom_highlight()

c.f. https://yutani.rbind.io/post/2017-11-07-ggplot-add/

Step 1) Implement the basic function, geom_clone()

Clone the layers above and replace some of the parameters with specified ones:

ggplot() +
  geom_line(data = d1, aes(x, y), ...) + 
  geom_clone(data = d2) + 

Step 2) Implement the core function, geom_highlight()

Clone the layers above and filter the data based on the given predicate:

ggplot(d, aes(foo, bar)) +
  geom_point() +
  geom_highlight(bar > 20 & baz == "A")

Maybe we need two variants?

  • geom_highlight_ungrouped()
  • geom_highlight_grouped()

Step 3) Implement the preset functions, geom_highlight_*() family

  • geom_highlight_point()
  • geom_highlight_line()
  • geom_highlight_bar()
  • geom_highlight_smooth()

Step 4) Re-implement the current functions, gghighlight_point() and gghighlight_line()

For backward compatibility.

Example in README does not work

library(gghighlight)

d <- purrr::map_dfr(
    letters,
    ~ data.frame(idx = 1:400,
                 value = cumsum(runif(400, -1, 1)),
                 type = .,
                 stringsAsFactors = FALSE))

gghighlight_line(d, aes(idx, value, colour = type), max(value) > 20)
#> Warning: package 'bindrcpp' was built under R version 3.4.4
#> Error in .subset2(x, i): no such index at level 1

Created on 2018-04-01 by the reprex package (v0.2.0).

Session info
devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.4.3 (2017-11-30)
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  tz       America/Chicago             
#>  date     2018-04-01
#> Packages -----------------------------------------------------------------
#>  package     * version    date       source                            
#>  assertthat    0.2.0      2017-04-11 CRAN (R 3.4.0)                    
#>  backports     1.1.2      2017-12-13 CRAN (R 3.4.3)                    
#>  base        * 3.4.3      2017-12-07 local                             
#>  bindr         0.1.1      2018-03-13 CRAN (R 3.4.4)                    
#>  bindrcpp    * 0.2.2      2018-03-29 CRAN (R 3.4.4)                    
#>  colorspace    1.3-2      2016-12-14 CRAN (R 3.4.0)                    
#>  compiler      3.4.3      2017-12-07 local                             
#>  datasets    * 3.4.3      2017-12-07 local                             
#>  devtools      1.13.5     2018-02-18 CRAN (R 3.4.3)                    
#>  digest        0.6.15     2018-01-28 CRAN (R 3.4.3)                    
#>  dplyr         0.7.4      2017-09-28 CRAN (R 3.4.2)                    
#>  evaluate      0.10.1     2017-06-24 cran (@0.10.1)                    
#>  gghighlight * 0.0.1      2017-10-05 CRAN (R 3.4.2)                    
#>  ggplot2       2.2.1.9000 2018-03-31 Github (tidyverse/ggplot2@3c9c504)
#>  glue          1.2.0      2017-10-29 CRAN (R 3.4.2)                    
#>  graphics    * 3.4.3      2017-12-07 local                             
#>  grDevices   * 3.4.3      2017-12-07 local                             
#>  grid          3.4.3      2017-12-07 local                             
#>  gtable        0.2.0      2016-02-26 CRAN (R 3.4.0)                    
#>  htmltools     0.3.6      2017-04-28 CRAN (R 3.4.0)                    
#>  knitr         1.20       2018-02-20 CRAN (R 3.4.3)                    
#>  lazyeval      0.2.1      2017-10-29 CRAN (R 3.4.2)                    
#>  magrittr      1.5        2014-11-22 CRAN (R 3.4.0)                    
#>  memoise       1.1.0      2017-04-21 CRAN (R 3.4.0)                    
#>  methods     * 3.4.3      2017-12-07 local                             
#>  munsell       0.4.3      2016-02-13 CRAN (R 3.4.0)                    
#>  pillar        1.2.1      2018-02-27 CRAN (R 3.4.3)                    
#>  pkgconfig     2.0.1      2017-03-21 CRAN (R 3.4.0)                    
#>  plyr          1.8.4      2016-06-08 CRAN (R 3.4.0)                    
#>  purrr         0.2.4      2017-10-18 CRAN (R 3.4.2)                    
#>  R6            2.2.2      2017-06-17 CRAN (R 3.4.0)                    
#>  Rcpp          0.12.16    2018-03-13 CRAN (R 3.4.4)                    
#>  rlang         0.2.0.9001 2018-04-01 Github (r-lib/rlang@49d7a34)      
#>  rmarkdown     1.9        2018-03-01 CRAN (R 3.4.3)                    
#>  rprojroot     1.3-2      2018-01-03 CRAN (R 3.4.3)                    
#>  scales        0.5.0.9000 2018-03-31 Github (hadley/scales@d767915)    
#>  stats       * 3.4.3      2017-12-07 local                             
#>  stringi       1.1.7      2018-03-12 CRAN (R 3.4.4)                    
#>  stringr       1.3.0      2018-02-19 CRAN (R 3.4.3)                    
#>  tibble        1.4.2      2018-01-22 CRAN (R 3.4.3)                    
#>  tools         3.4.3      2017-12-07 local                             
#>  utils       * 3.4.3      2017-12-07 local                             
#>  withr         2.1.2      2018-04-01 Github (jimhester/withr@79d7b0d)  
#>  yaml          2.1.18     2018-03-08 CRAN (R 3.4.4)

label must be filled with white

Related to #8

library(gghighlight)
library(ggplot2)

d <- tibble::tribble(
  ~x, ~y, ~type, ~value,
  1,  2,   "a",     0,
  2,  3,   "a",     1,
  3,  4,   "a",     0,
  1,  3,   "b",     1,
  2,  2,   "b",     5,
  1,  4,   "c",    10,
  3,  3,   "c",    10
)
ggplot(d, aes(x, y, colour = type, fill = type)) +
  geom_line() +
  geom_point(shape = "circle filled") +
  geom_highlight(mean(value) > 1, use_direct_label = TRUE)
#> label_key: type

Created on 2018-06-02 by the reprex package (v0.2.0).

Specifying 'x' argument for targeted geom layer.

Hi!

First, thanks for the package! This is an incredibly smart tool. My issue is related to the 'sf' package support. Given that I often use supplemental layers to create my publication-ready maps, I was wondering if there's a way to specify the target layer for which I wanting gghighlight to utilize, given that plotting multiple geom_sf layers seems to confuse gghighlight because it doesn't know what to inherit for it's filter.

The error I get when mapping out code similar to the example below is:

Error in ggplot_add.gg_highlighter(object, p, objectname) : there is no layer to highlight!

Do you have a suggestion on how to resolve this? I've tried a number of different things (including putting nc directly in the ggplot() call with it's aesthetic mappings, but gghighlight seems to keep getting confused about what to inherit.

gg_map <- nc %>%
  ggplot() +
  gghighlight(grepl("^[A-C]", NAME)) +
  geom_sf(data=coast, fill = "aliceblue", color = "aliceblue", alpha = 0.6) + # Supplemental Layer 1
  geom_sf(data=us_states, fill = "white", color = "gray62", alpha = 0.7) + # Supplemental Layer 2
  geom_sf(data=nc, aes(fill = AREA), alpha = 0.8) + # Target Layer for gghighlight
  ggtitle("Polygons whose names start with A-C are highlighted!")

gg_map

Using rank functions

Highlight the top 5 of key values like this:

scale_highlight_colour(key = x *2 + y * 2, limit = 5)

Handle stat(foo) and ...foo...

library(ggplot2)
library(gghighlight)

ggplot(mpg, aes(displ)) +
  geom_histogram(aes(y = stat(count))) +
  geom_highlight()
#> Error in mutate_impl(.data, dots): Evaluation error: object 'count' not found.

Created on 2018-05-31 by the reprex package (v0.2.0).

Modify plot data

so that it will be facetted nicely.

But, what if n is specified? Should the plot data be preserved? Or, attach it to all other layers?

highlight based on shape, not just color

This is a great package! I want to make figures that are also readable for people who are color blind. Is it possible to add support for highlighting based on shape, in addition to highlighting based on color?

e.g. 
ggplot(d, aes(x, y, colour = type)) +
  geom_point(size = 10) +
  gghighlight(type == "b", shape="square")

Handle default aesthetics better

While this works as expected,

ggplot(d) +
  geom_line(aes(idx, value, colour = type)) +
  scale_highlight_colour(max(value) > 20)

this will be with no color.

ggplot(d,aes(idx, value, colour = type)) +
  geom_line() +
  scale_highlight_colour(max(value) > 20)

image

How to handle layers where the provided predicate is invalid

Related: #66

When applying gghighlight() on an existing plot, it's not always that all the layers contain the consistent data for the predicate. Sometimes the calculation fails. What should I do then? Ignore? Let it fail? Or provide an option to choose which layer to ignore?

label should copy the position of the target layer?

library(ggplot2)
library(gghighlight)

ggplot(data.frame(x = 1, y = 1:10, text = letters[1:10]), aes(x = x, y = y)) +
  geom_point(position = position_jitter(seed = 1L)) +
  geom_highlight(label_key = text)

ggplot(data.frame(x = 1, y = 1:10, text = letters[1:10]), aes(x = x, y = y)) +
  geom_point(position = position_jitter(seed = 1L)) +
  geom_highlight(label_key = text, label_params = list(fill = "white", position = position_jitter(seed = 1L)))

Created on 2018-06-02 by the reprex package (v0.2.0).

Is geom_highlight() really a geom?

Some thoughts:

  • If I call this geom, should geom_highlight() accept mapping and data?
  • Otherwise, it should warn and ignore it.
  • If this is not a geom, are there any good name?

Respect "group" variable in mapping

Originally reported at https://stackoverflow.com/q/52117370. gghighlight() fails to bleach the layers for discrete scales. This is because calculate_group_info() doesn't respect the group key unlike ggplot2::: add_group(). I forgot if there's some reason, but it seems this needs to be fixed anyway.

d <- tibble::tibble(
  x = c(   1,    1,    2,    2),
  x_discrete = as.character(x),
  y = c(   1,    2,    4,    3),
  g = c("g1", "g2", "g1", "g2")
)

library(gghighlight)
#> Loading required package: ggplot2

# For continuous scale, it works.
p1 <- ggplot(d, aes(x, y, group = g, colour = g)) +
  geom_line() +
  theme_minimal()

egg::ggarrange(
  p1,
  p1 + gghighlight(g == "g1")
)
#> Warning: You set use_group_by = TRUE, but grouped calculation failed.
#> Falling back to ungrouped filter operation...
#> label_key: g

p2 <- p1 + aes(x_discrete, y, group = g, colour = g)

# For discrete scale, bleached layer fails to be rendered.
egg::ggarrange(
  p2,
  p2 + gghighlight(g == "g1")
)
#> label_key: x_discrete
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?

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

Can we use `gghighlight()` for `autoplot()`?

Hi, thank you for the greatest package.
Now, I'm trying to use gghighlight() for a result of autoplot(), but it occurs an error.

For instance:

library(glmnetUtils)
library(ggfortify)

model <- glmnet(medv ~ ., data = MASS::Boston)

g <- autoplot(model, xvar = "lambda")
g

rplot19

library(gghighlight)

g + gghighlight(variable == "crim", use_group_by = FALSE)

Error in mutate_impl(.data, dots) :
Evaluation error: object 'variable' not found

head(g$data)
#>  Df   Lambda  dev.ratio    index variable value
#> 1  0 6.777654 0.00000000 1.913631     crim     0
#> 2  1 6.175546 0.09238648 1.820597     crim     0
#> 3  2 5.626927 0.17376616 1.727563     crim     0
#> 4  2 5.127046 0.25269182 1.634530     crim     0
#> 5  2 4.671574 0.31820575 1.541496     crim     0
#> 6  2 4.256564 0.37259654 1.448462     crim     0

What would you suggest I do?

labels doesn't respect colour aesthetics

ggplot(d) +
  geom_line(aes(idx, value, colour = type)) +
  scale_highlight_colour(max(value) > 20) +
  annotate_highlights(max(value) > 20, aes(idx, value, label = type, colour = type))

image

Support geom_sf()

library(ggplot2)
library(gghighlight)

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"))
#> Reading layer `nc' from data source `C:\Users\user1\Documents\R\win-library\3.5\sf\shape\nc.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> epsg (SRID):    4267
#> proj4string:    +proj=longlat +datum=NAD27 +no_defs

ggplot(nc) +
  geom_sf() +
  geom_highlight(AREA > 0.15, use_group_by = FALSE)
#> Error in arrange_impl(.data, dots): Argument 1 is of unsupported type list

Created on 2018-05-27 by the reprex package (v0.2.0).

Bleach colors more flexibly

It might be useful to provide an option not to bleach the color completely. Should I provide bleacher functions?

use `transmute()`

Need to revert 5925b98

library(gghighlight)
library(ggplot2)
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

ggplot(nc) +
    geom_sf(aes(fill = AREA)) + gghighlight(grepl("^A", NAME)) +
    ggtitle("Polygons that starts with A")
#> Warning in bind_rows_(x, .id): Vectorizing 'sfc_MULTIPOLYGON' elements may
#> not preserve their attributes

Created on 2018-06-04 by the reprex package (v0.2.0).

Advice to use `seed` for position_jitter()

Bad

library(ggplot2)
library(gghighlight)

ggplot(mpg, aes(cyl, hwy, colour = year)) +
  geom_jitter() + 
  geom_highlight(max(cyl) > 4, use_direct_label = FALSE)

Good

ggplot(mpg, aes(cyl, hwy, colour = year)) +
  geom_jitter(position = position_jitter(seed = 1)) + 
  geom_highlight(max(cyl) > 4, use_direct_label = FALSE)

Custom Geom and Stat

  • use highlight aes to choose which one to highlight
  • do not show legend for highlight
  • add filter_func as parameter to stat and calculate highlight aes by that

group_key is not always a symbol, expressions may be used

library(ggplot2)
library(gghighlight)

ggplot(economics_long, aes(date, value01, colour = variable)) +
  geom_line() +
  geom_highlight()

ggplot(economics_long, aes(date, value01, colour = factor(variable))) +
  geom_line() +
  geom_highlight()
#> Error in factor(variable): object 'variable' not found

Created on 2018-05-30 by the reprex package (v0.2.0).

Error: `:=` can only be used within a quasiquoted argument

Hi I was trying to follow your examples to highlight my boxplot in ggplot it just didn't work.
Here is my code:

> P2 = ggplot(plotdf) +                                       
+     geom_boxplot(aes(x = N, y = Evalue, colour = pepSeq)) + 
+     scale_y_log10() +                                       
+     theme(legend.position = "None") +                       
+     gghighlight(Evalue < 0.0001, label_key = pepSeq)        
Error: `:=` can only be used within a quasiquoted argument    

I ran ls() and there is no other variables named Evalue

Could you please help me with where I'm wrong?

Remove n argument for simplicity?

Because it doesn't seem to do the right thing.

library(gghighlight)
library(ggplot2)
library(patchwork)

d <- tibble::tribble(
  ~x, ~y, ~type, ~value,
  1,  2,   "a",     0,
  2,  3,   "a",     1,
  3,  4,   "a",     0,
  1,  3,   "b",     1,
  2,  2,   "b",     5,
  1,  4,   "c",    10,
  3,  3,   "c",    10
)
p <- ggplot(d, aes(x, y, colour = type)) +
  geom_line() +
  geom_point(shape = "circle filled")

p1 <- p + geom_highlight(mean(value) > 1)
#> label_key: type
p2 <- p + geom_highlight(mean(value) > 1, n = 1)
#> label_key: type

p1 / p2

(p1 + facet_wrap(~ type)) / (p2 + facet_wrap(~ type))

# What we want is...
p2$data <- d
p2

Created on 2018-06-02 by the reprex package (v0.2.0).

Attach data to all layers

Should this be the same?

The plot data is substituted with the sieved version, so the layer without data accordingly uses it. But, is it preferable to use the original one?

library(ggplot2)
library(gghighlight)

set.seed(2)
d <- purrr::map_dfr(
  letters,
  ~ data.frame(idx = 1:400,
               value = cumsum(runif(400, -1, 1)) * 10,
               type = .,
               stringsAsFactors = FALSE))

ggplot(d) +
  geom_point(aes(idx, value)) +
  geom_line(aes(idx, value, colour = type)) +
  geom_highlight(max(value) > 200, n = 1)

# Specify original `d` on the unhighlighted layer.

ggplot(d) +
  geom_point(aes(idx, value), d) +
  geom_line(aes(idx, value, colour = type)) +
  geom_highlight(max(value) > 200, n = 1)

Created on 2018-05-24 by the reprex package (v0.2.0).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.