Giter Club home page Giter Club logo

gglabeller's Introduction

gglabeller

R package with one main function, gglabeller, which launches a simple shiny gadget that enables selecting points on a ggplot to label. Label positions are determined using the fantastic ggrepel package.

Installation

Installation via devtools:

devtools::install_github("AliciaSchep/gglabeller") 

Usage

First create a plot using ggplot2 and save it to a variable:

library(gglabeller)
library(ggplot2)
library(ggrepel)

p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + 
            theme_classic(base_size = 18)

Pass the variable to gglabeller:

gglabeller_example <- gglabeller(p, aes(label = rownames(mtcars)))

Running gglabeller will open a shiny gadget in the RStudio viewer pane (or in the browser if running R in the terminal). You can click or brush over points to select them for labelling. Clicking over an already labelled point or brushing over a set of points that have all been labelled will remove the labels.

You can also modify the ggrepel parameters via the "Parameters" tab in the app.

After clicking done, the returned object is a list storing the resulting plot and a code snippet for recreating the plot de novo.

We can make a static version of the plot to save:

gglabeller_example$plot

The code snippet can be useful if you want to incorporate the plot creation into a reproducible script:

gglabeller_example$code
#> [1] "set.seed(1502996);gglabeller_data <- p$data; gglabeller_data$gglabeller_labels <- rownames(mtcars); gglabeller_data[c(1:3, 5:7, 10:17, 19:32),'gglabeller_labels'] <- ''; p + geom_text_repel(data = gglabeller_data,mapping = aes(label = gglabeller_labels), segment.color = 'red',box.padding = unit(0.5, 'lines'))"
library(magrittr)
gglabeller_example$code %>% parse(text = .) %>% eval()

You can also just get out the indices of the lbelled points:

gglabeller_example$ix
#> [1]  4  8  9 18

This can be used to get out the labels themselves.

rownames(mtcars)[gglabeller_example$ix]
#> [1] "Hornet 4 Drive" "Merc 240D"      "Merc 230"       "Fiat 128"

Limitations

Requires points to label; not targetted at line plots or other non-point plots.

gglabeller's People

Contributors

aliciaschep avatar amrrs avatar

Watchers

James Cloos avatar

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.