Create flexible ggplot2 theme that 1) makes the legend and titles larger, 2) will look good irrespective of the final dimensions - r

I currently am one of the few R users in my company, which consists predominantly of stata users. One problem I've had with making plots using ggplot2 is that the default (theme_grey()) settings have much smaller axis font and a smaller legend than what is found in stata. Moreover, in presentations I find people have trouble reading the legend and titles from a distance.
I'm aware that ggplot2 has a theming system that allows for relative sizing. What I'd ideally like to do is to create a new default theme that I'd apply to all my plots that would make legends and axis titles larger. Importantly, however, very often the graphs I make have varying dimensions when output to pdf (e.g. 8 inch x 10 inch) or ( 10 inch x 13 inch). Since I'd like to apply this theme globally, I need it to produce good/easy to read output irrespective of the dimensions I specify when outputting to pdf.
I'd really appreciate any suggestions for how to do this/how to approach the problem using ggplot2's theming system.
Thanks!

The theme system can easily scale all the (themed) text, but not in a device sized aware way. The various theme sets, including theme_bw(), have an argument base_size which is the baseline size, in points, of fonts to use for the text. Some text is rendered at that size, and some is rendered at sizes relative to that (for example, axis labels and legend labels are rendered at 80% of the baseline size). So by specifying the base_size argument, you can scale all that text.
However, the base_size is in absolute points, not in a size which is relative to the device size. So the larger the device size, the smaller the text is relative to that.

Related

Change plot window size in Julia notebook

I am using Julia notebook and making plots using basic Plots package.
A plot looks good, except its entire size.
I can change the plot size and font size of labels individually. But it becomes less readable unless I change the font size and line width for every component.
So is there a way to change the size of a plot as a whole? I also hope I can change it by default.
To answer your original question in the title, you can change the size of the Plot Window by specifying the size attribute as you already are in your code. See here: http://docs.juliaplots.org/latest/basics/ for more details.
As pointed out by Rashid, you can use the scalefontsize function to scale the font size. You can also scale the thickness by setting the thickness_scaling attribute, see here for more details: http://docs.juliaplots.org/latest/generated/attributes_plot/
To be clear, there is not currently a unified way to scale a plot in the way you are looking for it right now, it has to be done manually (though it would be great to have this unified scaling). I opened a feature request for this here: https://github.com/JuliaPlots/Plots.jl/issues/3153

Set Legend Spacing in ggplot2

I am trying to create a plot in R using ggplot2. The figure looks great when displayed in R, but when I write it to pdf the labels in the legend slightly overlap the different color lines they are defining. How do I add white space between entries so that this does not happen?
Without code example it is hard to say, but if you don't see this problem in the R display and you see it in the pdf, you can try to increase the pdf output size. When rendering a pdf, the font size is kept and the elements of the graph are more squeezed if the output size is smaller than the displayed one.
p<-ggplot(mpg,aes(cyl,year))+geom_point()
ggsave('yourfile.pdf',p,width=10,height=10) # default is 7 on my install
I don't know if it is possible to change the spacing, but I don't see any parameters for that in the theme() or element_text() documentation

font size confusion in ggplot2

I'm working on a manuscript for a journal and the requirement is font size 10 for graphs. I tried this in R with ggplot2 with a setting like this:
p<-qplot(...)+theme_bw()
ggsave(filename="plot.pdf",plot=p,width=8,height=8,scale=1.3)
The x and y axis text in the figure looks really small.. I checked the R code for theme_bw() and i think the axis text font is the default 12 times 0.8, so roughly font 10. Is font size 10 in ggplot is really equal to font size 10 in Word or other context?
I've read this post: ggplot2 - The unit of size
And I'm also confused by whether the font size 100 used in the above post is actually 100 or 100/0.35277... which one is it?
Lastly, when I put the figure in Illustrator along with others in a panel, the resulting axis font looks pretty small. I kinda feel it's not font size 10 but it was generated with font size 10 specification in R.. Not sure if I should use a larger font to make it look bigger.
Too long for a comment. I'm not sure I understand your question completely, so if this isn't helpful let me know and I'll delete it.
First of all width and height in ggsave(...) refers to the width and height of the image. The default units are inches, but this can be changed via the units=... argument. So in your code the image will be 8" by 8". If you view this in Acrobat Reader at 100%, the font sizes will be whatever you've set them to in ggplot (see below). But if you create an 8 X 8 image (which is pretty big) and then scale it down for inclusion in a paper, everything will get smaller. Said another way, if the image in you paper is physically 3 X 5 (as an example), then the call to gggsave(...) should reflect that.
Second, in ggplot you can set the size of the axis text (the tick mark labels), and the axis title (the axis label) separately. The default in theme_bw() is axis.title=12pt and axis.text=0.8*12pt. So if you want both the axis label and the tick mark labels to be 10pt, you need to specify:
theme_bw() +
theme(axis.text=element_text(size=10),axis.title=element_text(size=10))

R, ggplot2, size of plot area

I use R and ggplot2 to produce graphs for my thesis. I can port them to tikz objects using the tikzdevice or to a pdf using the pdf device very easily, and in each case, specifying the width and height of the overall plot is straightforward.
However, I am actually more interested in specifying the width of the plot AREA (ie the inner box), since differences in this (particularly for plots on the same page) are more easily detected by the eye, even if they are a couple of points different in the final document.
Of course, the source of this issue can be easily put down to the axis labels that vary depending on the content.
My question is, How can I define 'fixed' axis label widths as a global option, or define the width to be exported as the inner plotting area for ggplot2 objects....

How do I increase the size of the points and the text with just one command in ggplot2?

I am plotting some graphs for a poster and a slideshow. I need bigger points and bigger text. I read about ggplot2's theme_set and theme_update. From what I can tell there are only two preset themes and they differ by the color arrangement of the background. However, I want to make all the text bigger and the plotted points bigger.
I learned how to change the font size.
theme_update(axis.text.x=theme_text(size=30))
But that only changes the axis text. I would have to do the same thing for a bunch of other parameters (axis.text.y, axis.title.x etc). Call me "lazy" but I want a single commands that can increase the base size for all text (and preferably the plotted points too). Is there one or two commands that covers all parameters? Alternatively are there any other set themes?
If you are fine with the colors of either of the two default themes, both take an argument of a base size for text. This is carried over to all the text around the plot (with scaling). You can just add theme_gray(30) to your plots. One caveat to that. If you afterward set other parameters of text with them_text, you have to respecify the size.
Alternatively, you can take the code for theme_gray (or theme_bw, whichever is closer) and make any thematic changes directly there. For examples of how to do that, check the ggplot2 wiki: https://github.com/hadley/ggplot2/wiki/Themes
EDIT:
As an example:
library("ggplot2")
qplot(1:2,1:2) + theme_bw(30)

Resources