Edit style of grid.arrange title. Bold, italic etc. R - r

I am creating a multi plot in R using grid.arrange, and wanted to change my title so that it is bold (and italic if possible).
As this is a general question, I will not include the code for my plots, but the code I am using to make my multi plot is:
grid.arrange(g1, g3, g4+theme(legend.position="none"),mylegend, top="Test title",
layout_matrix=matrix(c(1,1,2,3,4,4), ncol=2, byrow=TRUE),heights=c(1,1.5,0.3))
Are there any additional arguments which can be passed to the top argument to change the font face?

I've worked it out myself..
You can use the text_grob function to create a text element, which can then be passed to the top function of grid.arrange.
For example,
##title1=textGrob("Test title", gp=gpar(fontface="bold")) ## this does not work anymore
title1=text_grob(main, size = 15, face = "bold") #### this worked for me
grid.arrange(g1, g3, g4+theme(legend.position="none"),mylegend, top=title1, ncol=2, byrow=TRUE),heights=c(1,1.5,0.3))
Just a small update:
Instead of using text
textGrob
use text_grob from ggpubr package

Related

Problem in making multiple PDF pages with gridExtra in R

I used grid.table from the package gridExtra in R to make a PDF report of my Stat tests outputs.
I use this code:
library(gridExtra)
my_text = readLines("Dunnett.txt")
my_text2 = readLines("Shapiro-Wilk.txt")
pdf(sprintf("Results.pdf"), width=11, height=8)
grid.table(my_text)
grid.table(my_text2)
dev.off()
#cleanup
file.remove("Dunnett.txt")
file.remove("Shapiro-Wilk.txt")
But the problem is that my grid.table1 and 2 overlay on each other in one page. I want them be saved in 2 different pages.
Does anybody know how to do that?
library(grid)
grid::grid.newpage()

Inline text formatting options with output to grid graphics object in R

I am using ggplot/R for report generation and want more fine-grained control over text formatting.
It's trivial to write some text, apply global formatting parameters, and output to a grid-graphics compatible object - just use textGrob.
t <- textGrob(
label = "SOME TEXT"
,gp=gpar(fontsize=20, col="grey")
)
print(arrangeGrob(t))
Problem is that those formatting options only apply to the entire text string. What I'm looking for is something that would offer basic inline formatting options (font size, bold, italic, etc) - ideally something lightweight like Markdown/CSS/HTML. If I have to learn LaTeX, so be it, but that seems like overkill for what I'm trying to accomplish here.
Any thoughts?
You can use a "text cursor" alongside grid's functions for interrogating grobs. Say you had the phrase: "I want this text bold!!!!!!!, and you want the exclamation points in red:
grid.text("I want ",
name="notboldtext",
hjust=0)
text.cursor<-convertWidth(grobWidth("notboldtext") # Adds textGrob width & to location
+ unit(.5, "npc"), "npc")
grid.text("this text bold",
x=text.cursor,
gp=gpar(fontface="bold"),
name="boldedtext",
hjust=0)
text.cursor<- text.cursor + convertWidth(grobWidth("boldedtext"), "npc")
grid.text("!!!!!!!",
gp=gpar(col="red"),
x=text.cursor,
name="maptextnat",
hjust=0)

ggplot2 plot fill page in landscape (pdf)

I'm trying to render a plot to a PDF using the following approach:
pdf('~/Desktop/test.pdf', bg = "white", paper="USr")
p <- ggplot(df, aes(something)) + geom_bar();
print(p)
# I'm actually printing a bunch of graphs to the PDF
dev.off()
The "USr" in the PDF function is setting up the PDF to print in landscape mode. The plot is produced and is centered on the page but there is a large right/left margin and the plot isn't scaling out to take up the full 11" available to it.
I've tried some tweaks to the pdf(...) command and to the ggplot itself. Is there a solution this way or do I need to use a dedicated reporting/pdf package like sweave or knitr?
See this discussion; bottom-line is you probably want to use paper=special and set width and height explicitly.
Edit:
Here's a lazy trick to use ggsave with multiple pages,
library(ggplot2)
plots = replicate(8, qplot(1,1), simplify=FALSE)
library(gridExtra)
p <- do.call(marrangeGrob, c(plots,ncol=1,nrow=1))
ggsave("multipage.pdf", p, width=11, height=8.5)
(but otherwise, pdf(...) followed by a for loop is just fine and sometimes clearer)

How can I change the title of a ggplot2 legend?

I was looking here but I can't figure it out.
How can I change the word "type" to something else?
Add
+ labs(colour = "legend title")
to your ggplot call.
Great resource site is also google group for ggplot2.
edit: this assumes that colour is the aesthetic in the legend, e.g. qplot(x,y,colour=z). If another aesthetic is being shown in the legend, use that as the argument instead, e.g. + labs(fill = "legend title") for a raster/image plot.
More generally, if you specify an explicit scale such as scale_colour_continuous, you can set the scale_name argument (warning: the details of the arguments to scales may have changed in recent releases of ggplot2; this description is of version 0.9.2.1).
Add either:
+ scale_fill_discrete(name="Title", labels=c("1","2","3"))
or
+ scale_colour_discrete(name="Title", labels=c("1","2","3"))
depending on the geom.
I recommend you look into the ggplot2 cheatsheet. https://www.rstudio.com/wp-content/uploads/2015/03/ggplot2-cheatsheet.pdf
None of the others worked for me for some reason. In case they failed for you as well, adding this worked for me:
+ guides(fill=guide_legend(title='MY NEW TITLE'))
Just a word to the wise ... all of these options above can be overridden if you do something dumb like I just did. Earlier in my project, I had simply turned off a legend I didn't like using the "themes" parameter within ggplot:
theme(legend.title = element_blank())
If you do this, no matter what beautiful commands you put in to change the title -- and I tried all of those above -- they won't be overridden by your command to turn the legend off! You have been warned! ;{)

How can I add a subtitle and change the font size of ggplot plots in R?

I tried adding a subtitle using +opts(subtitle="text") but nothing showed up. The main title does work (+opts(title="text")).
I would also like to use a larger font for the axis (labels and coordinates), but I can't tell how to do that.
theme_get() will show you the "hidden" options that you can use in opts(), post 0.91 it's theme()
Current:
theme(axis.text.x=element_text(size=X))
theme(axis.text.y=element_text(size=X))
Pre 0.91:
opts(axis.text.x=theme_text(size=X))
opts(axis.text.y=theme_text(size=X))
Change size, to your desired size.
wrt the title, you can use "\n" to move the remaining text to a new line:
Current:
labs(title="text \n more text")
Pre 0.91:
opts(title="text \n more text")
ggplot2 doesn't have "subtitle" functionality. But you can use the \n term in any of the labels to drop down a line.
Update: ggplot version 2.2.0 can do subtitles, as demonstrated e.g. in this blog post.
Example:
library(ggplot2)
packageVersion("ggplot2") ## 2.2.0
d <- data.frame(x=1:5,y=1:5)
ggplot(d,aes(x,y))+
labs(title="abc",subtitle="def")+
## default left-aligned: moved them to center alignment
theme(plot.title=element_text(hjust=0.5),
plot.subtitle=element_text(hjust=0.5))

Resources