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)
Related
So I have a function that produces 6 graphs into one image and then I have 24 images (all different graphs) from that function. I was wondering if there is a way to have all of those images exported onto one big pdf.
Here is my current code to obtain the graphs:
eemgraphs3<-eem_overview_plot(eem_list3,spp=6,contour=TRUE)
Currently I look at each one individually using this code:
eemgraphs3[2]
eemgraphs3[3]
and so forth until I hit my last data point.
If I could potentially compile them all into one pdf file then I think it would save a lot of time but I do not know how to do that. Thanks for your help!
I have tried these function which I found online to solve this but no results came out of this.
pdf("eemgraphs3.pdf")
eemgraphs3[1:24]
dev.off()
install.packages("ply")
library("ply")
pdf("plots.pdf", width = 7, height = 7)
d_ply(eemgraphs3, .(z), failwith(NA, function(x){plot(x$y,main=unique(z))}), .print=TRUE)
dev.off()
It can be something like this:
pdf("eemgraphs3.pdf")
par(mfrow = c(4,6))
eemgraphs3[1:24]
dev.off()
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'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 am trying to capture all plots a user makes in a script and save it in a folder. I have tried png("C:/path/to/plotfolder/Rplot%03d.png") and it works great for most plots. Unfortunately the department got alot of functions that use the windows() function. Those plots are not captured, and I've been unable to find a function that can capture plots send to those devices.
png("path/to/somewhere/plot.png")
windows()
plot(1:20)
dev.off()
This creates an empty file called plot.png.
I would really like to avoid forcing users to avoid those functions or rewrite them, so is there any way to capture plots send to a windows() device with a single call as in png()? If this is not the case, I guess i will have to just remove the windows() calls from the scripts or something.
Extra information
I need this since i'm trying to create a rscript function that runs a script and saves all output including warnings,errors, prints and plots to a specific folder that can then later be opened by another application. I would like not to interfere with the users code forcing them to make alternate functions when working with this script.
edit
Tried looking into knitr, as it seems to be able to find and save plots in windows() devices, but so far i have been unable to find anything. UPDATE: Not compatible with what i want
edit
I have found something that could work sometimes, but not a solution. Using savePlot in a while loop could save all open windows, but if several plots have been send to the device it will only save the last
I have found somewhat of a solution, though i would like a better solution. Since it's an supposed to run as an rscript i just replaced windows() with a wrapper for png(). As
windows <- function (width = 480, height = 480, pointsize = 12, record, rescale, xpinch,
ypinch, bg = "white", canvas, gamma, xpos, ypos, buffered, title, restoreConsole = TRUE,
clickToConfirm, fillOddEven, family = "sans", antialias = c("default", "none", "cleartype", "grey", "subpixel"),
filename = plotoutput) {
png(filename,width,height,pointsize,bg,restoreConsole,family,antialias)
}
Thanks for the help James.
I would like to print multiple graphs in one pdf file. I know there has been a lot on this, but I would like to print different window/graph sizes for each page, i.e. first page a 8.5x11, second page 11x8.5 and so on. I tried this:
pdf(file="Combined_Graphs.pdf",onefile=TRUE,bg="white",width=8.5,height=11)
hist(rnorm(100))
pdf(file="Combined_Graphs.pdf",onefile=TRUE,width=11, height=8.5, bg="white")
hist(rnorm(100,10,2),col="blue")
dev.off()
I must be using onefile=TRUE wrong as it only generates the last graphic before closing. Is there a better way to size the graphic device without having to call the pdf function twice?
What I would do is produce seperate PDF's and them combine them later. I use the PDF toolkit for this. Wrapping this in an R function using a system call through system even makes it scriptable from R. The call to pdftk will look something like:
pdftk *pdf cat output combined.pdf
or in R:
system("pdftk *pdf cat output combined.pdf")
combine_pdfs = function(path, output_pdf) {
system(sprintf("pdftk %s/*pdf cat output %s"), path, output_pdf)
}
I think what you are trying to do cannot be done in R, i.e., you need to use external tools such as the PDF toolkit as suggested by Paul Hiemstra to combine separate PDF files with varying page dimensions (an alternative tool is PDFjam).
If you set onefile = TRUE in your call to pdf(), each plot that is written to that PDF device will be printed on a separate page, yet with the same page dimensions. In your example, you open a first PDF device, write one plot to it, then you open a second PDF device, write a different plot to it, and then close the second PDF device but leave the first PDF device open. Since you use the same file argument for both pdf() calls, you might not notice that the first PDF device is still open. If you closed it, only the first plot would end up in "Combined_Graphs.pdf".
Here is a modified version of your example that illustrates how PDF devices are opened, filled with content, and closed:
pdf(file = "foo.pdf", onefile = TRUE, width = 8.5, height = 11)
hist(rnorm(100))
hist(rnorm(100, 10, 2), col = "red")
pdf(file = "bar.pdf", width =11, height = 8.5)
hist(rnorm(100, 10, 2), col = "blue")
dev.off()
dev.off()