I am trying to get figures ready for publication, where the journal requires the figures to be a certain width and height and the font size to be 10.
This is my basic structure:
cairo_ps(file = "plot.eps", width = 6.85, height = 9.213)
dotplot(...)
dev.off()
I've tried to force the font size using trellis.par.set, trellis.device and setting it with pointsize in the device itself, but no luck. I can't get to change the font size to be anything than it defaults to. Any ideas?
You could use Cairo instead. This solution can, however, yield some unexpected results as it seems, for example when changing the dpi argument when calling Cairo.
There seems to be some coding problems involved. For example, I have to re-run the code after changing the font size for changes to take effect. Be warned! You might have to tweak it a bit to get the font size you want.
Contrary to the built-in graphics devices, however, this seems to react to changes made by trellis.par.set. Here is a example with PNG, the same works for me with PDF:
dat <- data.frame(a=1:3, b=1:3)
Cairo(file = "plot.png", type="png", units="in", width = 6, height = 3, dpi=100)
xyplot(b~a, dat)
trellis.par.set("fontsize", list(text=12, points=8))
dev.off()
Compare to:
Cairo(file = "plot.png", type="png", units="in", width = 6, height = 3, dpi=100)
xyplot(b~a, dat)
trellis.par.set("fontsize", list(text=18, points=8))
dev.off()
Related
This may not be the right place to ask this question, but I'm having trouble saving my colors from ggplot. I made this plot (pic below), and used ggsave() to save it as a .png file, and I loved the way it looked. However, when I take that .png file and upload it anywhere (specifically, in this case, to twitter and UpWork), the colors distort. The blue and orange get much darker, and I like the plot much less. Why is this happening? Is it the way I'm saving? Is it a function of file compression on those websites?
Can anyone recommend a better way to save that will not influence the aesthetics of my plots?
Attached here are screenshots of what you can see on the file on my computer (first pic), and a screenshot of the uploaded version of that same exact file (second pic. Darker). Hopefully they both upload as they look to my computer here...
Here is an example of the code/colors I am using:
require(ggplot2)
plot <- ggplot(data=data.frame(x=c(1:3),y=c(1:3)),
aes(x=x,y=y))+
geom_point(col="#E56800", size=3)+
theme_classic()+
theme(panel.background = element_blank(),
plot.background = element_rect(fill = "#354154"),
text= element_text(color="#FCFFF9"),
axis.text = element_text(color="#FCFFF9"))
ggsave(plot, filename = "plot.png",
width = 5, height = 7,
dpi=300)
EDIT: By the way, I'm using RStudio on a Macbook Pro, in case that's relevant. I always get confused by the graphical device options, so I'm guessing they have something to do with this.
You can try to install the CRAN Cairo package, and add a type argument in ggsave like this:
ggsave(plot, filename = "plot.png",
width = 5, height = 7,
dpi=300,
type = "cairo-png") # add this argument
Cairo allows to export anti-aliased images (this is the default on Mac but not on PC), maybe this could help.
I have made a beautiful plot in R to be used in a scientific journal. According to the journal's specifications, I need an eps file format with embedded fonts. Since R does not export eps files with embedded fonts, I am using the base graphics call embedFonts() to convert it. However, this call is changing the bounding box of my figure. In this simple example below, the white space is cropped. In my OCD-adjusted publication-quality plot, white space is added because I've already adjusted it perfectly to the edges.
I want the fonts to be embedded, but everything else to stay the same!
Here is an example:
setEPS()
postscript(file = "~/Desktop/test.eps", family = "Helvetica", colormodel = "srgb", width = 5, height = 3)
plot(x = 1:10, y = 1:10, col = "red", main = "Keep everything the same but embed my fonts!")
dev.off()
embedFonts(file = "/Users/athena/Desktop/test.eps", format = "eps2write", outfile = "/Users/athena/Desktop/stupid.eps")
So far I have:
- installed ghostscript using homebrew: $ brew install ghostscript
- learned that embedFonts needs FULL paths, no tilda's allowed
- specified the format as "eps2write" because the default "ps2write" changes it to a postscript
I spent so much effort on "reproducible research" with open data, open code, open journal, bla bla bla... I really don't want to have to make my final figures using illustrator conversion or something :(
The reason this happens is because embedFonts internally calls Ghostscript which in turn tries to act smart by fitting an "optimal" bounding box by trimming out some of the surrounding white space.
We can prevent that by drawing an invisible box around the perimeter of our 5inx3in drawing area in R. Just add one more line to your code snippet:
setEPS()
postscript(file = "~/Desktop/test.eps", family = "Helvetica", colormodel = "srgb", width = 5, height = 3)
plot(x = 1:10, y = 1:10, col = "red", main = "Keep everything the same but embed my fonts!")
box(which="outer", col="white")
dev.off()
embedFonts(file = "/Users/athena/Desktop/test.eps", format = "eps2write", outfile = "/Users/athena/Desktop/stupid.eps")
Another way to go about this is Jonathan's answer here which basically uses sed to read in Bounding Box info from the input file and writes it to the output file: http://r.789695.n4.nabble.com/eps-file-with-embedded-font-td903387.html as pointed out by #neilfws in a comment above.
I'm trying to make a png in R where I can vary the resolution without changing the apparent height of the text and it is not working.
Here is my code
drawbox <- function(res, ps=12, textcex=1) {
png(file="test.png",width=6,height=3, units="in", res=res, bg="transparent", pointsize=ps)
plot(x=c(0,1),y=c(0,1),type="n",xaxs = 'i',yaxs = 'i',axes=FALSE)
text(x=0.5, y=0.5, adj=c(0.5,0.5), labels="Some text", cex=textcex*6)
dev.off()
}
then this makes a very different size of text
drawbox(res=300, ps=12, textcex=1)
than this
drawbox(res=100, ps=12, textcex=1)
but isn't that the point of the pointsize argument, that this doesn't happen?
Note: I need base graphics here, other packages lack the flexibility to allow me to make what I want to make.
If I mess around with that textcex parameter I can get things to work, but that's annoying.
I create a plot with ggplot and save it as an png in my linux box.
When I insert the plot into a presentation in Windows using MS Powerpoint (2013) it all looks fine until I go full screen with F5: in full screen the axes and the grey background grid disappears (font rendering is also noticeably worse).
In the presenter notes it still looks fine.
The png looks perfectly fine, only once projected in full screen on a secondary screen (monitor/wall) it goes bad. Full screen on primary screen works - this doesn't seem to be a resolution issue.
I tried saving plots in two different ways with the same results:
# 1
ggsave(filename="test.png", p)
# 2
png(file = "test.png", width = 1024, height = 768, units = 'px', dpi=300)
print(p)
dev.off()
Specifieng type (cairo-png) doesn't help either.
Workaround: exporting to pdf and displaying that in fullscreen works completely fine. It is only inconvenient if I want to have my notes along.
You can use the new export package to save your ggplot2 graph to native Powerpoint format- that should work OK, see
https://cran.r-project.org/web/packages/export/index.html and for demo
https://github.com/tomwenseleers/export
Typical syntax is very easy, e.g.:
install.packages("export")
library(export)
library(ggplot2)
qplot(Sepal.Length, Petal.Length, data = iris, color = Species,
size = Petal.Width, alpha = I(0.7))
graph2ppt(file="ggplot2_plot.pptx", width=6, height=5)
You can also use it to export to Word, Excel, Latex or HTML and you can also use it to export statistical output of various R stats objects.
You can bring all of these together with
ggsave(filename="test.png",width=1024,height=768,units='px',dpi=300)
You can adjust the dpi to 600, but usually 300 will suffice.
I've also had success using inches instead of pixels for ppt instead of pixels.
I have several images in a Sweave report that look like this:
They are created by a function with a code akin to this:
<<fig = T>>=
barplot(prop.table(table(x, y), margin = 2) * 100,
border = F, xlab = '', ylab = '', las = 2)
#
I want to compress the y-axis, so my plot looks something like this (without the label distortion):
The only way I was able to get this was by using png() with a custom height parameter and then using the image file on LaTeX, but that kind of ruins the whole purpose of Sweave. How can I achieve this within barplot() (or with par() or some other elegant solution)?
Sweave has chunk options width and height for the dimensions of the images. It won't control how big the plot is in the final rendered PDF as that is something LaTeX controls, but it does control the creation of the figures themselves.
From the Sweave Manual [pdf]:
Attention: One thing that gets easily confused are the width/height parameters of the R
graphics devices and the corresponding arguments to the LaTeX \includegraphics command.
The Sweave options width and height are passed to the R graphics devices, and hence affect
the default size of the produced EPS and PDF files. They do not affect the size of figures in the document, by default they will always be 80% of the current text width. Use \setkeys{Gin} to modify figure sizes or use explicit \includegraphics commands in combination with Sweave option include=FALSE.
Also read
require("utils")
?RweaveLatex
which also has details.
For your quoted example, width and height are both 6, the default. So you could do something like
<<fig=TRUE, width=8, height=5>>=
barplot(prop.table(table(x, y), margin = 2) * 100,
border = FALSE, xlab = '', ylab = '', las = 2)
#
To get the desired proportions.
[Please don't use F and T - you are asking for trouble!]
However, do note what the Manual or ?RweaveLatex say. Once in LaTeX by default the image will be included with a width equal to 0.8\textwidth. Hence you might also wish to set the width for each chunk explicitly to the size of the figure created, e.g.
\setkeys{Gin}{width=8in}
<<fig=TRUE, width=8, height=5>>=
barplot(prop.table(table(x, y), margin = 2) * 100,
border = FALSE, xlab = '', ylab = '', las = 2)
#
%% reset if you want
\setkeys{Gin}{width=0.8\textwidth}
So you have to manage the two settings:
The sweave chunk options width and height control creation of the EPS or PDF file (or both)
\setkeys{Gin} controls the width of the included figure when processed using LaTeX.