Textbox in gridarrange - r

I am arranging two graphs on a pdf using grid.arrange.
I would like to have a text box between the title and the first plot to add some information on the graphs themselves possibly editing in in rich text.
What is the best solution for that?
I would ideally produce it in R directly and printing it out with the rest of the plot.
Any idea?
To use a standard example of grid.arrange
pl = replicate(3, ggplot(), FALSE)
grid.arrange(grobs = pl)

Related

Vertical vistime timeline and overlapping geom_text labels

I'm trying to draw a vertical timeline using vistime with its ggplot plotting option. My main intention is to better integrate it with text, side by side. This is the code:
library("vistime")
library("plotly")
data <- read.csv("../../data/programming.csv")
g <- gg_vistime(data, col.event="Item", col.start="Start.Date", col.end="End.Date", col.group="Group")
g + theme(axis.text.x = element_text(angle=90, color='blue4',size=14) )+coord_flip()
As you can see, as I'm doing the coord_flip, the labels intersect with each other. I'd like to make the labels go vertical. This is code that draws them:
https://github.com/shosaco/vistime/blob/372da36791cbdb7ad7d6841ed991e55b36f77e06/R/plot_ggplot.R#L83-L91
So that means that it's a geom_text sentence. Is there some way to change the orientation of geom_text-drawn text once it's done? Can I use some theme command to do that? Barring that, is there any way I can change the placement of the labels using vistime?
You can deconstruct any ggplot2 visualization with (surprisingly) ggplot_build (actually, what it does is to create elements that can be rendered using the plain vanilla plot).
data <- read.csv("data/programming.csv")
g <- gg_vistime(data, col.event="Item", col.start="Start.Date", col.end="End.Date", col.group="Group") + theme(axis.text.x = element_text(angle=90, color='blue4',size=14) )+coord_flip()
g.d <- ggplot_build(g)
g.d$data[[4]]$angle <- 90
rebuilt <- ggplot_gtable(g.d)
png(filename="img/timeline.png", width=240, height=960)
plot(rebuilt)
dev.off()
That creates a data frame with different elements of the plot, including g.d$data which contains, effectively, the data and its properties when rendered in its 4th element. g.d$data[[4]]$angle contains the angle of all the geom_text elements that have been rendered. So once you get that, it's just a matter of changing it individually or colectively to what you want. You need to reconstruct the plot using ggplot_gtable and then use the core plot command to plot and render it any way you want, in this case a png.
At any rate, ggplot_build allows you to introspect the data structures and different pieces of the graph created using ggplot, changing any one of its layers, metadata or pieces after they have been created. In our case, it produces the intended effect

How to move the legend to outside the plotting area in Plots.jl (GR)?

I have the following plot where part of the data is being obscured by the legend:
using Plots; gr()
using StatPlots
groupedbar(rand(1:100,(10,10)),bar_position=:stack, label="item".*map(string,collect(1:10)))
I can see that using the "legend" attribute, the legend can be moved to various locations within the plotting area, for example:
groupedbar(rand(1:100,(10,10)),bar_position=:stack, label="item".*map(string,collect(1:10)),legend=:bottomright)
Is there any way of moving the plot legend completely outside the plotting area, for example to the right of the plot or below it? For these kinds of stacked bar plots there's really no good place for the legend inside the plot area. The only solution I've been able to come up with so far is to make some "fake" empty rows in the input data matrix to make space with some zeros, but that seems kind of hacky and will require some fiddling to get the right number of extra rows each time the plot is made:
groupedbar(vcat(rand(1:100,(10,10)),zeros(3,10)),bar_position=:stack, label="item".*map(string,collect(1:10)),legend=:bottomright)
I can see that at there was some kind of a solution proposed for pyplot, does anyone know of a similar solution for the GR backend? Another solution I could imagine - is there a way to save the legend itself to a different file so I can then put them back together in Inkscape?
This is now easily enabled with Plots.jl:
Example:
plot(rand(10), legend = :outertopleft)
Using layouts I can create a workaround making a fake plot with legend only.
using Plots
gr()
l = #layout [a{0.001h}; b c{0.13w}]
values = rand(1:100,(10,10))
p1 = groupedbar(values,bar_position=:stack, legend=:none)
p2 = groupedbar(values,bar_position=:stack, label="item".*map(string,collect(1:10)), grid=false, xlims=(20,3), showaxis=false)
p0=plot(title="Title",grid=false, showaxis=false)
plot(p0,p1,p2,layout=l)

Annotate on grob

I have combined several ggplots with arrangeGrob.
Now i'm searching for a way to plot a image over the whole plot.
For example a watermark which lies over the combined plots.
As the result of arrangeGrob isn't a standard plot or a ggplot object i cant do it the usual way.
Is there a possibility?

line break and subscript in axis title using plotly in R

I just started to use plotly for some interactive scatter plots in R and having a hard time on axis labels. Normally I designed my plots with ggplot2 and then using the ggplotly function to convert them, but this is sometimes very slow for any reason. So I want to create my plots directly in plotly...
I am trying now to change the axis title and want to add line breaks and later I also want to add subscript labels. But I am already failing at the newline in the title. Is there any trick?
library(plotly)
library(dplyr)
plot_ly(mtcars, x = wt, y = mpg, text = rownames(mtcars), mode = "text") %>%
layout(xaxis=list(title='text with\nlinebreak'))
In plotly, you can get linebreaks (and other text formatting) using html tags.
So piping
layout(xaxis=list(title='text with <br> linebreak'))
should work.
Hence, to get subscript labels use the <sub> tag. For example
CO<sub>2</sub>
will give you
CO2.
Note you have to use <br> or <br /> not <br/> (without a space)

Multiple R plots with tooltips in one figure?

I want to create a new figure with interactive R-plots (with tooltips).
To show how I wish to have my plots I give an example using the mtcars data. I am using the package scatterD3 to get tooltips:
library(scatterD3)
data(mtcars)
tooltips <- paste('This is an incredible <strong>',rownames(mtcars),'</strong><br />with',mtcars$cyl,'cylinders !')
scatterD3(x=mtcars$wt,y=mtcars$mpg,tooltip_text=tooltips)
Now I don't want only one plot of this type in one figure. Usually it is possible to use par(mfrow=(i,j)) to create a figure with more than one plot. But it seems not to work for my interactive plots. Is there a way to do this?
Have you given a look to the plotly R package?
It has also a function to render the ggplots in ggplotly
An idea could be to create a multiplot with ggplot2 and than render it with ggplotly.
I've not yet tried it, so I don't know if it's really possible.

Resources