Coder Social home page Coder Social logo

ggannotate's Introduction

ggannotate

Lifecycle: experimental R build status Codecov test coverage CRAN status

{ggannotate} is a point-and-click tool to help you put your annotations exactly where you want them to go on your {ggplot2} plots.

The last step in data visualisation - adding annotations - is one of the most important, but it can be hard to get annotations in exactly the right place. With {ggannotate}, you interactively annotate ggplots using a Shiny-based RStudio add-in.

Installation

{ggannotate} is not yet on CRAN. It will be submitted to CRAN when it is more stable and feature-complete.

Install from GitHub:

remotes::install_github("mattcowgill/ggannotate")

Usage

Create a ggplot2 object, then call ggannotate():

library(ggplot2)
library(ggannotate)

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

ggannotate(p)

If you invoke ggannotate() without specifying a plot, it will use the last plot you modified or created, using ggplot2::last_plot():

ggplot(mtcars, 
            aes(x = wt, y = mpg)) + 
  geom_point() 

ggannotate()

You can also use the RStudio add-in to annotate your last_plot() with ggannotate().

Each of these options will launch a Shiny app. Add your annotation(s) and click ‘copy code’ to copy the annotation code to the clipboard. Then paste the copied code in your script and you’re done!

You can adjust the size of the plot in the Shiny app so you can set it to be the same as your intended output.

Lifecycle

Lifecycle: experimental

{ggannotate} is still in active development, in an experimental phase. Aspects of the functionality are quite likely to change as the package evolves.

Suggestions are welcome (please file a GitHub issue). The UI will change substantially.

ggannotate's People

Contributors

jhelvy avatar mattcowgill 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

ggannotate's Issues

flipped date scales don't work

Flipped scales work
Date scales work
Flipped date scales don't work

economics %>%
  ggplot(aes(x = date, y = unemploy)) +
  geom_line() +
  coord_flip()

ggannotate(last_plot() )�

Enable ggannotate when using ggplot2 + tidiverse pipe (%>%)

HI @MattCowgill,

First of all, ggannotate is such a great idea and I hope you can develop it further!
So, I was trying to draw some lines in a ggplot2 plot that included some data cleaning and wrapping with the tidiverse package. Once I tried to open ggannotate it pops out the following error:

Error in all_sorted %>% select(scaffold, taxonomy) %>% ggplot(aes(fill = taxonomy, :
could not find function "%>%"

If have some idea of how can I sort this out it would be great,
Cheers.

Add ggtext::geom_textbox option to insert the annotation

Great tool - thanks!!

Could be a nice improvement to add the option of getting the annotation R statement as ggtext::geom_textbox (together with related styling options...) as this would give a lot more options to style the annotation - ideally adding also the capacity to link the geom_textbox to the area of interest in the chart with a geom_curve arrows!

Currently did that manually - see example...

geom_curve(aes(xend = 10.9, yend = 2959.9, x = 6.9, y = 28668.7), size = 2, color = "tan", arrow = arrow(length = unit(0.07, "npc"))) + ggtext::geom_textbox(data = data.frame(x = 6.9, y = 28668.7, label = "Why so many people are using **other** category? \n Is there an important missing category ?"), mapping = aes(x = x, y = y, label = label), width = unit(0.6, "npc"), family = "Lato", size = 7, fill = "cornsilk", inherit.aes = FALSE)

Nothing renders in shiny

I am attaching a screen shot that should be a reprex of sorts built off of data(VonBort). The shiny starts but the plot is deformed [I think all the plotting is in a tiny splice at the bottom]. If I click anywhere in the box, it crashes. The error is:

Warning: Error in UseMethod: no applicable method for 'ggplot_build' applied to an object of class "function"

The box is Ubuntu 18.04LTS with an almost entirely CRAN package installation. This package is such a great idea, anything that I can do to help I will. Incidentally, I also tried it with

fake.data <- data.frame(x=seq(1,10), y=seq(2,11))
q <- ggplot(fake.data) + aes(x=x, y=y) + geom_point()
ggannotate:::ggannotate(q)

And get the same behavior.

gganotate

Apologies for my bad choice of screen shot apps. I really should look for a better one.

Compress output more

Ensure structure() isn't returned for arrow() etc

Don't specify param when it's the default

Unable to run - "register_theme_dependency"

I started up ggannotate after a while of not using it, and I can't get it to work at all. The shiny window pops up but I see

An error has occurred!
could not find function "register_theme_dependency"

I'm guessing this is a shiny issue - perhaps the function name has changed or something? This is with ggannotate_0.1.0.900 and shiny_1.7.2

ggannotate fails when x is datetime

The error I get is "Error: Invalid input: time_trans works with objects of class POSIXct only"

I did get the proper code though (lubridate::as_datetime() added by me to make it work)

  geom_curve(data = data.frame(x = lubridate::as_datetime(1691077324.05326),
                               y = 222.017858130503, 
    xend = lubridate::as_datetime(1691071613.9407),
    yend = 22.6778110317712), 
    mapping = aes(x = x, y = y, xend = xend, yend = yend), 
    arrow = arrow(30L, unit(0.1, "inches"), "last", "closed"), inherit.aes = FALSE)

Round x and y values in annotation code

Options:

  1. Round to a pre-defined number of digits (3?);
  2. Infer the appropriate number of digits to round to, based on the limits of the relevant axis.

I favour (2)

Fill color in geom_rect remains in geom_label

After using geom_rect, the fill color remains for the geom_label fill. Could fix this by adding a fill input for geom_label, and maybe resetting the default values when the user switches between different geoms.

fix flipped coords

shiny returns x,y coords that do not recognise a flip
use ggplot2::summarise_coord() on a built plot to get flipped status and transform accordingly

Add box annotation

In addition to labels and arrows, it's sometimes helpful to also draw a box around sections of a chart. I've added this in my simple app, and I'd be happy to make this addition as well. With how you've set things up for this app, I think I could make this addition pretty quickly.

Add factor levels for facets

When you specify facet order using factor levels, ggannotate returns back to alphabetical order.

library(tidyverse)

data <- tribble(~r_awesomeness,~name,~Nationality,~time,
                1,"Matt","Western Austalian",1,
                1,"Will","Australian",1,
                10,"Hadley","New Zealander",1,
                2,"Matt","Western Austalian",2,
                2,"Will","Australian",2,
                10,"Hadley","New Zealander",2,
                2,"Matt","Western Austalian",3,
                5,"Will","Australian",3,
                10,"Hadley","New Zealander",3) %>% 
  mutate(name = fct_relevel(name,"Will","Hadley","Matt"))


data %>% ggplot(aes(x = time, 
                    y = r_awesomeness, 
                    fill = Nationality))+
  geom_bar(stat = "identity") +
  grattan_fill_manual(n = 3)+
  guides(col = guide_legend(nrow = 5))+
  facet_grid(.~name) +
  geom_text(data = data.frame(x = 1.29669354378515,
                              y = 7.28929935866944,
                              label = "annotation",
                              name = "Matt"),
            mapping = aes(x = x,
                          y = y,
                          label = label),
            size = 3.86605783866058,
            angle = 0L,
            lineheight = 1L,
            hjust = 0.5,
            vjust = 0.5,
            colour = "black",
            family = "sans",
            fontface = "plain",
            inherit.aes = FALSE,
            show.legend = FALSE) 

This can be fixed by entering in the factor orders manually:

 geom_text(data = data.frame(x = 1.29669354378515,
                              y = 7.28929935866944,
                              label = "annotation",
                              name = factor("Matt", levels = c("Will","Hadley","Matt")

But it would be even better if the levels were pulled in by ggannotate.

Consider using annotate() instead of geom_text() / geom_label()

When the user adds a label, I would suggest using annotate(). This enables you to avoid having to create a data frame and mapping the aesthetics. Instead you just give it the coordinates and label, along with all the other label options. You can still set text or label annotations by using annotate(geom = "text") or annotate(geom = "label"). The returned code is just a little more parsimonious than what you get with geom_text() or geom_label(). I usually only use those when I'm mapping multiple labels from a data frame.

I'd be happy to implement this one myself if you want.

Enable image saving

The primary use case of ggannotate is to generate the code to produce the annotation(s) for a plot.

A secondary use case could be to export the annotated plot directly from the ggannotate app

annotate a formula

Hello, I would like to know how to easily annotate some text with subscripts and italics, which will facilitate me to annotate a line of formula.

Refinements to multiple annotations

At the moment the Shiny gadget can only add one annotation at a time. The user can copy their code out and then repeat the process to do multiple annotations. They should be able to do multiple within the one Shiny window. This will require properly modularising the Shiny app.

error when using a new source window

Hello Matthew

Love the package! Great job!!

The ggannotate function works as expected:

image

But it errors when I use a source window in R Studio (ie I 'pop out' a window):

image

When running the ggannotate add-in with code highlighted from a source window, I will get the error:

 ggannotate:::ggannotate()
Error in UseMethod("primary_selection") : 
  no applicable method for 'primary_selection' applied to an object of class "NULL"

Many thanks

Wayland issue with copy paste

Hello, I was trying this from Ubuntu 22.04, which uses wayland as default. The shiny app works perfectly, but I get:

Warning: Error in : Clipboard on X11 requires 'xclip' (recommended) or 'xsel'; Clipboard on Wayland requires 'wl-copy' and 'wl-paste'.
  3: shiny::runApp
  2: shiny::runGadget
  1: ggannotate::ggannotate

`factor()` in `facet_*()` breaks mapping

For example, executing this results in annotation being placed in every facet:

p <- mtcars %>%
  ggplot(aes(x = wt, y = mpg)) +
  geom_point() +
  facet_wrap(~factor(cyl))

ggannotate(p)

This works:

p <- mtcars %>%
  mutate(cyl = factor(cyl)) %>%
  ggplot(aes(x = wt, y = mpg)) +
  geom_point() +
  facet_wrap(~cyl)

ggannotate(p)

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.