How can I create ggplot2 titles in Japanese? - r

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("テスト")

Related

Unicode characters not showing when exporting to pdf in 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!!

`ggsave` jpg reversed black-white values with `theme_minimal()`

When I save a ggplot image with theme_minimal the black and white values are reversed in a photo negative like effect. This does not occur if I do not use a theme nor does it occur with theme_bw. It also does not occur when saving to .pdf or .png. I have tested and this occurs when running in RStudio, R GUI, or through the terminal. I'm running R version 4.0.2 on Mac OS 10.15.7.
I would greatly appreciate any insight into debugging this. The behavior has persisted for several weeks across multiple full system restarts.
library(ggplot2)
ggplot(diamonds, aes(x = cut, y = clarity)) +
geom_point() +
theme_minimal()
ggsave("test_minimal.jpg")
Seems like 'theme_minimal' defaults to black background for jpg files (pdf and png were fine and I used Windows 10). #stefan had proposed two ways to overcome this in the comments above. I did not see that and went searching again. So posting the full solution here:
library(ggplot2)
ggplot(diamonds, aes(x = cut, y = clarity)) +
geom_point() +
theme_minimal()
ggsave("test_minimal.jpg",bg="white")

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")

rCharts not displaying and no errors. Just a blank screen

I have a graph that I can create with this ggplot2 code:
ggplot(total.new, aes(x=day, y=new.total)) + geom_line() +
facet_wrap("practice", scales="free_y", ncol=2)
Now I wanted to try out rCharts, so I tried the following:
rPlot(new.total ~ day | practice, data=total.new, type='line')
and all I get is a blank screen in the viewer with no errors of any kind. Any ideas what might be wrong?

Resources