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.
Related
I'm trying to save a rather large ggpairs file. For some reason I can't get ggsave to work on my device (the file is created but it's blank no matter the plot, file type etc.) so I have been manually right clicking on save image as on the output.
However the resolution is so poor you can't read the correlation information as I've had to reduce the font size.
Appreciate any suggestions on how to get out a readable output.
TIPI_data<- vms_data[, c("cond","group_aff","personal_exp","outcomes","mechanics", "TIPI_O", "TIPI_C", "TIPI_E","TIPI_A","TIPI_N")]
ggpairs(TIPI_data, ggplot2::aes(colour=cond), upper = list(continuous = wrap("cor", size = 1.5)))
I don't know if this works for you, it worked for me. I have used this code to save as png with high resolution:
corrPlot <- ggpairs(df, diag=list(continuous="density"), axisLabels='show')
png(filename, height=1000, width=1000)
print(corrPlot)
dev.off()
I used this reference: https://github.com/tidyverse/ggplot2/issues/650#issuecomment-65086626
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 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()
I'm trying to make colorbars, as well as raster maps, in R, and the output figures give unsightly lines in them when exported to pdf.
Here is code to generate a colorbar. It looks fine when you run it in R:
color.bar <- function(lut, min, max=-min, nticks=11, ticks=seq(min, max, len=nticks), title='') {
scale = (length(lut)-1)/(max-min)
plot(c(0,10), c(min,max), type='n', bty='n', xaxt='n', xlab='', yaxt='n', ylab='', main=title)
axis(4, ticks, las=1)
for (i in 1:(length(lut)-1)) {
y = (i-1)/scale + min
rect(0,y,10,y+1/scale, col=lut[i], border=NA)
}
}
par(mfrow=c(2,1))
par(mar=c(3,0,3,2.5))
pal = colorRampPalette(c("red","yellow"))
neg = pal(100)
pal = colorRampPalette(c("yellow","darkgreen"))
pos = pal(50)
color.bar(c(neg,pos),min=-75,max=50,ticks=c(-75,-50,-25,0,25,50))
color.bar(colorRampPalette(c("goldenrod","blue"))(25),min=0,max=1)
par(mar=c(5.1,4.1,4.1,2.1))
dev.copy2pdf(file = "colorbar_wood.pdf", height = 8, width = 1)
pdf("colorbar_wood.pdf",width=1,height=8)
par(mfrow=c(2,1))
par(mar=c(3,0,3,2.5))
pal = colorRampPalette(c("red","yellow"))
neg = pal(100)
pal = colorRampPalette(c("yellow","darkgreen"))
pos = pal(50)
color.bar(c(neg,pos),min=-75,max=50,ticks=c(-75,-50,-25,0,25,50))
color.bar(colorRampPalette(c("goldenrod","blue"))(25),min=0,max=1)
par(mar=c(5.1,4.1,4.1,2.1))
dev.off()
And here is what I get out as a pdf:
link
I need to get this up to publication quality. Any ideas on how to fix?
This is invariably an issue with the software used to render the PDF, not with R, and arises because of features such as anti-aliasing and other rendering operations that the PDF Viewer does in order to display the PDF.
This is discussed in ?pdf, notably
Note:
If you see problems with PDF output, do remember that the problem
is much more likely to be in your viewer than in R. Try another
viewer if possible. Symptoms for which the viewer has been at
fault are apparent grids on image plots (turn off graphics
anti-aliasing in your viewer if you can) and missing or incorrect
glyphs in text (viewers silently doing font substitution).
Unfortunately the default viewers on most Linux and Mac OS X
systems have these problems, and no obvious way to turn off
graphics anti-aliasing.
....
I just viewed your PDF in two different PDF viewers on Linux (Evince and Okular) and the degree to which these artefacts affected the file was different across the two viewers, with Okular giving fewer artefacts on the red-green one and none on the blue-yellow one. As such this seems to be an issue with viewing the PDF and not something with R. Your figure should therefore be publication quality.
Instead of having to repeat "height= 4, width= 4, dpi= 72" with every call . . .
I once tried library(defaults) for a problem like this, but it did not behave well. I asked a question about it on R-help, but it seemed like no one uses it.
It looks like par("din") is coming from the X11 device that I have running, but that's not going to be the device that ggsave() uses when I call it, I don't think, because I'm either saving to a .png or a .pdf -- PDFs are no good for my scatter and tile plots. Granted, the PDFs scale much better when I bring them into my LaTeX document, but it seems like this is a desirable level of control in general.
Thanks for your ideas.
This may not be the best way, but you can write a wrapper function with different height and width defaults. For example
my.ggsave <- function(filename = default_name(plot), height= 4, width= 4, dpi= 72, ...) {
ggsave(filename=filename, height=height, width=width, dpi=dpi, ...)
}
Now we can test to see if my.ggsave does what we want it to:
ggplot(data.frame(x=1:10), aes(x=x, y=x)) + geom_point()
ggsave("normal_ggsave.png")
and make sure we can pass additional arguments to ggsave if we need to:
my.ggsave("four_by_four_600.png", dpi=600)