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")
Related
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!!
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")
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.
If I create maps using geom_sf, the axis labels have the wrong symbol for degrees. I get degree symbols that are vertically centred in the text, rather than raised like superscipts.
For example,
library(sf)
library(ggplot2)
nc = st_read(system.file("shape/nc.shp", package="sf"))
ggplot() +
geom_sf(data = nc) +
theme(axis.text = element_text(size=16))
When I see examples online, they typically look correct (e.g. image below, copied from here), so I guess it is related to something in my local setup.
I have tried changing font, using library(extrafont) but this problem remains in every font I tried.
Update
I don't think this is a ggplot-specific issue, as I get the same thing with any graphics that uses the degree keyword in plotmath. For example
par(mar=c(0,0,0,0))
plot.new()
text(0.5,0.5, bquote(120*degree*N), cex=5)
I am on Linux (Kubuntu 19.04), R 3.5.2, ggplot2 v. 3.2.1, sf v. 0.7-7.
Not sure what other information might be relevant, but I can update answer with anything else that is requested.
Finally managed to track down the answer:
From ?X11 it says:
Problems with incorrect rendering of symbols (e.g., of quote(pi) and
expression(10^degree)) have been seen on Linux systems which have the
Wine symbol font installed – fontconfig then prefers this and
misinterprets its encoding. Adding the following lines to
‘~/.fonts.conf’ or ‘/etc/fonts/local.conf’ may circumvent this problem
by preferring the URW Type 1 symbol font.
<fontconfig>
<match target="pattern">
<test name="family"><string>Symbol</string></test>
<edit name="family" mode="prepend" binding="same">
<string>Standard Symbols L</string>
</edit>
</match>
</fontconfig>
Adding these lines to /etc/fonts/local.conf solved the problem for me.
I ran into a similar problem and after reading here and some other related issues I found a solution. I'm working with Rstudio, so I guess that if you are doing R without it you you will easily find your work around (hopefully).
My solution was to Tools -> Global Options
and here I changed my backend to Cairo.
This is how it looks the change:
As a workaround I am creating the axis labels manually. Still looking for a better solution and explanation of the undesired behaviour.
xlabs = seq(-84,-76, 2)
ylabs = seq(34, 36.5, 0.5)
ggplot() +
geom_sf(data = nc) +
scale_x_continuous(breaks = xlabs, labels = paste0(xlabs,'°W')) +
scale_y_continuous(breaks = ylabs, labels = paste0(ylabs,'°N')) +
theme(axis.text = element_text(size=16))
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("テスト")