specifying font size in R figures - r

Is there a method for specifying the font size in when producing figures in R. This seems like a really basic requirement but I can't seem to find any references to somewhere that specifies the font size. I can save a figure to a pdf as follows:
setwd("C:\\")
pdf(file="Plot.pdf",family="Times")
plot(x,y);
dev.off()
Where R basically generates the figure in the pdf not in the figure window. When I look for ways of altering the font size all I see is people referring to cex=1.5 argument to scale fonts 150 percent, and cex.lab, cex.axis, etc ... Although not being an immediate issue now, I do wonder what will happen when I publish some results and the journal requires font size between 9 and 11. How do I control these in R? Any suggestions would be appreciated.

You control the font size with the ps (point size) parameter. The default is typically 12 (but can be controlled globally for a PDF file by the pointsize parameter) so if you want, let's say, fonts of the size 10 for a particular text you would use par(ps=10); text(...). Since you mentioned cex: note that cex is relative to the current pointsize and also applies to symbols whereas ps specifically applies to text. Obviously, the size will only match as long as you don't resize the resulting figure.

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

embed_fonts() changes paper size on encapsulated postscript (.eps, R)

I am trying to create an (encapsulated) Postscript graphic with fonts embedded in the file. For embedding, I use the extrafont package.
While the actual graphic created by the postscript() device is completely fine, embed_fonts() seems to change the paper size to something like A4 or Letter. Why is that? Is there a way to maintain the paper size set by the postscript() device?
Here is a minimal example:
library(extrafont)
postscript( "test.eps", family="Times New Roman", width=4, height=4,
horizontal = FALSE, onefile=FALSE, paper = "special")
plot(1:10)
dev.off()
embed_fonts("test.eps")
Running this example without the call to embed_fonts() results in a figure with appropriate dimensions (i.e., the desired output). Here is a screenshot from my document viewer:
However, as soon as embed_fonts() is run on this file, the paper size is changed to a fixed paper size:
Is there a way around this? Both embedding the fonts and using .eps is necessary. I suspect it may have to do with the information which is saved in the Postscript file when paper="special" (or the lack thereof). It seems as if embed_fonts() would reset this information on its own.
If you post an eample file, then I can look and tell you what's different, I can't tell you anything about controlling it from R
For EPS (as opposed to PostScript) the program must not request a media size, though it can (in fact must) contain comments which describe the Bounding Box.
So either the file isn't an EPS, or the comments describe it as A4 or 'something'. You haven't explained what it is you are doing with the PostScript program that exhibits a difference between the two sets. By that I mean 'what is your dcument viewer?'

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

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.

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))

How to fix clipped output from R postscript function?

The output from the postscript function is generating clipped output. See the legend on the right side of the figure below, the last legend entry should read SBW-25. I've tried changing the width argument to postscript but this has had no effect. I've also tried manually editting the bounding box in generated postscript but this also has had no effect.
Wihtout a reproducible example, it is hard to diagnose, but here are some things to try.
If you are viewing the postscript in GSView, you probably just need to increase paper size. Click Media -> A0 (or something suitably big).
If you save to a different format (PDF, PNG, whatever), do you get the same effect?
If all else fails, try manually changing the legend position with
p + opts(legend.position = whatever)

Resources