I am trying to output r plots as .eps files and use them in illustrator. However I don`t seem to get the correct fontsize in the output. An easy example:
library(ggplot2)
data<-data.frame(RLU=c(0.24,384.04), type=c("9 Balalexpression", "Induktion"))
setEPS()
postscript(width=6.5/2.54, height = 4/2.54, file = "pTRELUCReporter.eps")
p<-ggplot(data, aes(x=type, y=RLU))
p+geom_bar(stat="identity", fill="grey")+
xlab("")+
theme(axis.title.y=element_text(size=9),
axis.text.x=element_text(size=9),
axis.text.y=element_text(size=9)
)
dev.off()
This results in an output eps file. I now next import the the file into Illustrator and the fontsize seems to be too big:
Allready the output eps file seems to have fonts bigger than 9 pt so I am tempted to exclude import issues. The R reference for element_text says the size is in pts.. can anywone explain what I am getting wrong here? I would love to understand this thing as I will have to generate many figures with exactly set fontsizes :(
Thanks a lot!
I have reproduced the behaviour and output of your code. Ghostscript is the same.
With the width=6.5/2.54 option in your postscript() function you are setting the width of the graphics region only. This doesn't necessarily mean you are downscaling all included elements in your eps file.
If you downsize your text in the graphics by 2.54 the result looks what you might need.
library(ggplot2)
data<-data.frame(RLU=c(0.24,384.04), type=c("9 Balalexpression", "Induktion"))
setEPS()
postscript(width=6.5/2.54, height = 4/2.54, file = "pTRELUCReporter.eps")
p<-ggplot(data, aes(x=type, y=RLU))
p+geom_bar(stat="identity", fill="grey")+
xlab("")+
theme(axis.title.y=element_text(size=9/2.54),
axis.text.x=element_text(size=9/2.54),
axis.text.y=element_text(size=9/2.54)
)
dev.off()
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
Here is a minimal example of the problem I face:
data.frame(Time=as.factor(c(0,5,10,15,20,25,30,35)),
Value=c(0,2,4,6,8,6,5,6))%>%
ggplot(aes(x=Time,y=Value))+
geom_point()+
ylab("\U0394 O.D")
ggsave("image.pdf", dpi = 1200)
I am able to generate a plot which renders the delta perfectly. However, the last command to save the plot as a PDF gives warnings. When you open the saved file, the y-axis is labelled as ..OD.
Try to use Cairo:
ggsave("image.pdf", dpi = 1200, device = cairo_pdf)
I'm probably too late for this discussion but I had better experience with latex2exp library. I had some issues saving multiple plots with ggsave and cowplot::save_plot I had to look for other solutions. With this method you can use any saving method, your file plot look fine.
Let's say you want to put pi as a label on your y axis.
p <- p + ylab(TeX("$\\pi$"))
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.
Using ggplot2, extrafont and R's pdf device I have built plots in the cmyk colormodel that incorporate certain non-Windows fonts. The pdf-output looks fine and shows that the font has been embedded correctly, for instance "Arial-BoldMT".
Unfortunately, when trying to import the pdf into Adobe InDesign, I get the error message that the font "Arial-BoldMT" is not currently available, which also happens to the non-Windows fonts I mentioned above.
I suppose there might be a problem with the name of the embedded font that cannot be recognized by InDesign, since the font is very well available as "Arial" including all the variations such as "bold".
Any suggestions how to get those fonts working in InDesign by either adjusting the R script or using InDesign?
Thank you!
Here is a sample plot, similar to the plots I need to produce, just leaving out the unnecessary code lines:
library(ggplot2)
library(extrafont)
# define font
font <- "Arial"
# sample data
x <- data.frame(c("Personnel Costs", "Non-Personnel Costs", "Investments"),
c(33, 22, 45))
colnames(x) <- c("costs", "percent")
# plot
plot <- ggplot(x, aes("", y = percent, fill = factor(costs), width = 1.2))+
geom_bar(width = 4, stat="identity")+
# add the text (no font specification here)
geom_text(aes(label=costs),fontface = "bold")+
# no legend
theme(legend.position = "none") +
# pie-chart
coord_polar("y", start = 0.42, direction = 1)
# save plot
pdf("plot.pdf", family=font, colormodel="cmyk")
plot
dev.off()
PS: Using ggsave and embedFonts() with Ghostscript produced the same results.
Note: Using "Calibri" with the pdf device or ggsave and embed_fonts() does not work at all.
It seems if the font was not properly embedded into the pdf.
By running embed_fonts() after saving the plot, the according font got embedded and now works in InDesign.
I just needed:
library(extrafont)
embed_fonts(file="plot.pdf", outfile="plot.pdf")
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.