Unicode characters not showing when exporting to pdf in R - r

I am trying to export some unicode characters in the U+1xxxx format from R to pdf using ggplot2 (this is one example: https://unicode-table.com/en/1F321/). In R, the output is great when I have it in the ggtitle as ggtitle("Temperature range \U1F321"):
However, when I export the plot to pdf it appears as these blocks below:
I have tried many things that other people suggested (such as using cairo pdf and the showtext package) but none of it worked. I am using Windows.
Thank you very much!
EDIT: I'm saving as pdf using pdf("example.pdf") or cairo_pdf("example.pdf")
Reproducible code:
ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point() + ggtitle("Temperature range \U1F321")
cairo_pdf("plot_cairo.pdf")
ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point() + ggtitle("Temperature range \U1F321")
dev.off()
pdf("plot_normal.pdf")
ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point() + ggtitle("Temperature range \U1F321")
dev.off()
Also, just want to state my intentions more clearly. I'm just aiming to have a high-resolution ggplot exported from R to Word. When I export it as an image, the characters show well but the resolution is of poor quality. Following suggestions from other users, I'm therefore first exporting it as pdf and then importing it to Word as an object from the "Insert" tab:
Using this method, the plot is of much better resolution in Word, but the unicode characters don't show up well. So if maybe someone else has another suggestion on how to get a high quality ggplot exported from R to Word while still preserving the unicode characters, that would also work! Thank you!!

Related

ggplot axes are garbled, look like Unicode boxes

Running Ubuntu 16.04; R 3.6.2; ggplot2 3.3.0
Running R under --nix
If I run this ..
library(ggplot2)
data("midwest", package = "ggplot2")
ggplot(midwest, aes(x=area, y=poptotal))
I get a plot with little boxes for the axes (Unicode?)
I get the same little boxes if I use 'plot'
But If I run 'plot' add a 'family' attribute,
plot (1:10, family="arial")
I get this (nice axes),
This shows that at least some fonts are there!
Back to ggplot ....
The easy solution would be to figure out (I tried) how to set the family in ggplot.
I tried,
ggplot(heightweight, aes(x= ageYear, y=heightIn, font="ariel")) + geom_point()
ggplot(heightweight, aes(x= ageYear, y=heightIn, family="ariel")) + geom_point()
No help .. Little boxes.
Note: It's happy if I put family="Zombie"
Anyone know how to set the family in ggplot?
A better solution?
The hard solution would be for me to figure out which fonts are missing, install them under --nix, and then make sure R (under --nix) can find them.
After much playing I got something to work!
I now get a beautiful title and axes (no more Unicode).
Here's the snippet
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
ggtitle("Fuel Efficiency of 32 Cars") +
xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
theme_bw() +
theme(text=element_text(family="Garamond", size=14))
Apparently the way to set the family in ggplot is using 'theme',
theme(text=element_text(family="Garamond", size=14))
I know it's not a perfect solution but it gets me going (without meds :-) ).
I think it would be worth renaming this issue because it is a Nix specific issue, and was reasonably hard to find. The core issue is caused by a mismatch between the system fontconfig and the one provided by nix.
https://discourse.nixos.org/t/fonts-in-nix-installed-packages-on-a-non-nixos-system/5871/6
I fixed the issue by adding an explicit fontconfig dependency and adding the following to my mkShell command
shellHook = "export FONTCONFIG_FILE=${pkgs.fontconfig.out}/etc/fonts/fonts.conf";
After that opening R from within a nix-shell and generating plots works as expected.

ASCII ³ displays as 3 in ggplot2

I have plot and I need to add mm³ but I can't use bquote because I need to wrap the text using
paste(strwrap(text, width), collapse="\n")
so I though that I use ASCII ³ character bcause that's the only superscript value I need, but it display as mm3 when rendered, is it the font issue? How to resolve it? I need to run this on any platform becuase other developers use Mac/Windows, I use windows and it run Linux server.
Details on how to render mathematics in plots and elsewhere in R are documented in ?plotmath.
A simple example, plotting the city gas millage by engine squared engine displacement.
library(ggplot2)
ggplot(mpg) +
aes(x = displ^2, y = cty) +
geom_point() +
xlab(expression(displ^2)) +
ylab("City Miles Per Gallon")

Writing printer friendly pdfs with ggplot2 and many (>25000) dots or lines

I am creating a lot of facet plots with ggplot2 and writing those into a multiple page pdf file. I added some sample data as an example.
reps <- 1000
df <- data.frame(id=rep(letters[1:25], each=reps),
group=rep(LETTERS[1:25], each=reps),
x=runif(reps*25),
y=runif(reps*25))
pdf('test.pdf', width=11.69, height=8.27)
for(i in seq(4)) {
p <- ggplot(df, aes(x,y, group=group)) +
geom_line() + geom_point() +
facet_wrap(~ id)
print(p)
}
dev.off()
To view the PDF it is really nice, but most of the time these big files with vector graphics kill my print jobs.
Is there a way to handle this properly? For example, creating PNG images and saving them in multipage PDFs? Or, am I the only one having problems printing such files?
I am aware that the PNG file might be bigger, but I guess the printer can handle it much better than a vector graphic.
The only solution I found so far is the following: https://helpx.adobe.com/acrobat/kb/quick-fix-print-pdf-image.html
I think this might be the best way since the creation of PDF is the same, you just switch to "print as image" in the advance printing dialog in Acrobat Reader...

How can I create ggplot2 titles in Japanese?

I am preparing a presentation in Japanese and would like the titles and legend names of my images to be in Japanese. I can get the text to render just fine in RStudio but when the image is rendered the Japanese characters just appear as boxes.
x=-10:10
y=x*x
df=data.frame(x,y)
ggplot(df, aes(x,y)) + geom_line() + ggtitle("テスト")
Thank you.
It seems that you do something like this. I am using Mac, and I initially did not see the letters you specified in ggplot graph. But the following code is printing the letters.
theme_set(theme_gray(base_size=12, base_family="HiraKakuProN-W3"))
ggplot(df, aes(x,y)) +
geom_line() +
ggtitle("テスト")

issues when using ggsave to save the generated file into PDF

I use ggsave to save the figure generated using ggplot2, this is how I do
figure1<-last_plot()
ggsave(figure1,file="/home/user1/figure1.png",width=15,height=3)
These two lines of code successfully save the figure as png file.
However, when I tried to save it as pdf file,
ggsave(figure1,file="/home/user1/figure1.pdf",width=15,height=36)
The saved pdf file is just a blank page. What's the problem? Thanks a lot.
library(ggplot2)
ggplot(data=mtcars, aes(x=cyl, y=hp))+
geom_point()
figure1<-last_plot()
ggsave(figure1,file="figure1.png",width=15,height=3)
ggsave(figure1,file="figure1.pdf",width=15,height=3)
# works with R 2.15
You can try to save it using pdf() command:
library(ggplot2)
pdf(file="figure.pdf",width=15,height=3)
figure <- ggplot(data=mtcars, aes(x=cyl, y=hp)) + geom_point()
print(figure)
dev.off()
I supose that it also doesn't work.
Solution (workaround) that should work is:
1) save it to svg file
ggplot(data=mtcars, aes(x=cyl, y=hp)) + geom_point()
ggsave("figure.svg")
2) then convert it to pdf. E.g. in Linux
rsvg-convert -f pdf -o figure.pdf figure.svg

Resources