About me:
- 💼 I’m an economist at the Australian Government's Department of Employment and Workplace Relations.
- ®️ I’m an R enthusiast!
Interactively annotate ggplots
License: Other
About me:
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
Hi,
thanks for this!
I had forgotten the name of the package and my first reflex would have been to check in the addinslist (https://github.com/daattali/addinslist) to find it again but it looks like it's not yet there. Would you consider submitting it?
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)
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.
Detect polar coords, give user a warning that they don't work; #18
Options:
I favour (2)
In response to: https://twitter.com/SteveDX_/status/1229457531468156931
This should be possible using shiny::nearPoints()
update status when a parameter is modified (https://stackoverflow.com/questions/46732849/shiny-detect-a-change-in-input-with-a-warning-in-ui-r), only include modified parameters in call
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
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)
Use geom_*
Don’t put params in a list
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.
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.
as in
mtcars %>%
mutate(cyl2 = cyl) %>%
ggplot(aes(x = wt, y = mpg)) +
geom_point() +
coord_flip() +
facet_wrap(~cyl2)
Once support for multiple annotations is added, adding support for "point labels" could be another nice feature to add (at some point in the future...maybe even after an initial version is sent to CRAN). I just came across this package which does exactly that: https://github.com/AliciaSchep/gglabeller
Suggested by @wfmackey
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)
Hello Matthew
Love the package! Great job!!
The ggannotate
function works as expected:
But it errors when I use a source window in R Studio (ie I 'pop out' a window):
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
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.
Apologies for my bad choice of screen shot apps. I really should look for a better one.
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
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() )�
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.
Ensure structure()
isn't returned for arrow()
etc
Don't specify param when it's the default
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.
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.
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
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.