This is a simple question.
I am trying to write a legend with text in small caps in R.
I can write the plot using tikzDevice and manually change the plot to small-caps in LaTex, but I want to know if it's possible in R itself?
Thanks.
This is the R code I am using so far:
legend("bottomright", inset=.05, c(expression(Delta*ZRT1), expression(Delta*ZRT2)), lty=1:2, pch=1:2)
This is the LaTex expression I am trying to get into the R legend:
\Delta Z\textsc{rt\oldstylenums{1}}
The Unicode standard does define a number of "small capital" characters in the IPA extensions.
E.g., using this Smallcaps Generator: http://fsymbols.com/generators/smallcaps/
plot(1L:10, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ")
legend("bottomright", expression(\Delta Zʀᴛ1")
As of Unicode 5.1, the only characters missing to allow representation of the full Latin alphabet in small capital Unicode characters are small capital versions of Q and X.
See also here: http://en.wikipedia.org/wiki/Small_caps#Unicode
The small caps generated by Smallcaps Generator do not work for all fonts. For example, the Linux Libertine family (Libertine, Biolinum) have their small caps and Old Style numbers in the Unicode Private Use Area (E000-F8FF), as shown here (last page).
The example plots below use axis labels assembled from the small caps generator, plus the small_caps and small_nums string, assembled from the private use area. The first plot uses Times New Roman, which works with the generated small caps but does not have corresponding glyphs in the private use area. The second plot uses Linux Libertine O, which does not have all Unicode characters from the IPA extensions.
library(ggplot2)
x <- 1L:10
y <- 1L:10
df <- data.frame(x,y)
# assemble strings from Libertine's private use area.
small_caps <- "S\UE05D\UE051\UE05C\UE05C C\UE051\UE060\UE063"
small_nums <- "\UE020\UE021\UE022\UE023\UE024\UE025\UE026\UE027\UE028\UE029"
font <- "Times New Roman"
ggplot(df) +
geom_point(aes(x = x, y = y)) +
labs(x = paste("Sᴍᴀʟʟ Cᴀᴘs /", small_caps),
y = paste("Oʟᴅ Sᴛʏʟᴇ", small_nums)) +
theme(text = element_text(family = font)) +
annotate("text", x = 2, y = 9, label = font)
font <- "Linux Libertine O"
ggplot(df) +
geom_point(aes(x = x, y = y)) +
labs(x = paste("Sᴍᴀʟʟ Cᴀᴘs /", small_caps),
y = paste("Oʟᴅ Sᴛʏʟᴇ", small_nums)) +
theme(text = element_text(family = font)) +
annotate("text", x = 2, y = 9, label = font)
Created on 2018-12-08 by the reprex package (v0.2.1)
Related
I am trying to embed the ≤ sign into the fonts of my pdf plot. The ≤ sign is shown in the plot when viewed within RStudio (p1). However, when I save the plot and embed the fonts the ≤ sign is converted to an = sign.
Using the extrafont package I want to save my plot with the CM Roman font. I have tried alternative methods with the device set as cairo_pdf in ggsave(). This embeds the ≤ sign into the pdf, but the font is no longer CM Roman.
What needs to be done so that the ≤ sign remains in the plot and the font is CM Roman?
library(ggplot2)
library(extrafont)
font_import()
font_install("fontcm")
loadfonts()
df <- data.frame(foo = c(2, 4, 8 , 16),
bar = factor(c(1:3, "4\u2264")))
p1 <- ggplot(df, aes(x = bar, y = foo)) +
geom_bar(stat="identity") +
labs(title = "p1: Equality sign shows in RStudio plot") +
theme(text = element_text(family = "CM Roman", size = 25))
p2 <- ggplot(df, aes(x = bar, y = foo)) +
geom_bar(stat="identity") +
labs(title = "p2: Equality sign not shown in .pdf file") +
theme(text = element_text(family = "CM Roman", size = 25))
print(p1)
[![p1 plot][1]][1] # there should be an image of p1 here...
# Sys.setenv(R_GSCMD = "C:/Program Files/gs/gs9.53.3/bin/gswin64c.exe") #~ run once at start of each R session
ggsave("p2.pdf", p2, width = 15, height = 10, units = "in")
embed_fonts("p2.pdf", outfile="p2.pdf")
[![p2 plot][1]][1] # there should be an image of p2 here...
ggsave losing unicode characters from ggplot+gridExtra
There are several solutions in this post. I think it has to do something with encoding, but I am not 100% sure.
I try to use ggplot2 to plot a chart with the Chinese title but shows Unicode in a square.
I've tried some following command
quartz(family='STKaiti')
par(family='STKaiti')
plot(1, xlab = "你好", family = "Heiti SC Light")
and use "extrafont" font to load my ubuntu fonts into R
The characters plot shows are still Unicode in a square. I want to show the Chinese word properly.
Still, I have no idea how to call this kind of words. "Unicode in a square" is the best I can describe.
Try the showtext package, which was designed for this.
Sample code:
library(ggplot2)
library(showtext)
showtext_auto()
p = ggplot(NULL, aes(x = 1, y = 1)) + ylim(0.8, 1.2) +
annotate("text", 1, 1, size = 15, label = "你好,世界") +
xlab("坐标轴") +
theme_bw(base_family = "wqy-microhei", base_size = 24)
quartz()
print(p)
I'd like to use Unicode shapes in ggplot2 geom_point() (specifically, arrows like ↘, Unicode "\u2198", or LaTeX \searrow), as in shape = "\u2198", that are not in the default font. In this unanswered post, #Laserhedvig commented "it seems that the problem lies in the font. Apparently, the base default fonts don't contain support for these specific glyphs. Now, how to change the font for the shape argument of geom_point()?"
This solution for Unicode in axes.text uses theme(axis.text.x = element_text(family = "FreeSerif")), and this solution uses theme(text=element_text(size=16, family="Comic Sans MS")) for all text, but how can I do this for shape?
Is there a general solution to use Unicode for shape? (must I somehow use cairo and/or a font family argument?)
If not, is there some other set of arrow shapes? (My search for arrow shapes and glyphs, including in the scale_shape documentation came up empty.)
In my case, I need a ggplot2 layer showing qualitative predictions for the direction of change at points in time across discrete categories.
An example:
library(dplyr)
library(ggplot2)
d <- tibble(year = c(1, 1, 2, 2),
policy = rep( c('policy 1', 'policy 2'), 2),
prediction = c(NA, 'increase', 'decrease', NA),
predictionUnicode = c(NA, '\u2197', '\u2198', NA))
ggplot(d) +
geom_point(aes(x = year, y = policy, color = prediction), shape = "\u2198")
shape = "\u2198" (i.e. "↘") does not work
Edit: Thanks to djangodude's comment about ggplot's font usage, I found the family argument of geom_text, which allows different fonts. Thus, Unicode "shapes" can be plotted as characters with geom_text. However, the legend for geom_text is fixed to "a". And themes only control non-data display, so the base_family argument will not work for shape.
ggplot(d) +
geom_tile( aes(x = year, y = policy), color = "black", fill = "white") +
# geom_point does not allow new fonts?
geom_point(aes(x = year, y = policy,
color = prediction), shape = "\u2198") +
# geom_text does allow new fonts, but the legend text is fixed to "a"
geom_text(aes(x = year, y= policy,
color = prediction,
label = predictionUnicode),
family = "Calibri") +
scale_x_continuous(breaks = c(1,2)) +
theme_gray(base_family = "Calibri")
geom_text plots unicode, but not in the legend
It seems the shape argument is really the correct way to do this, right?
I tried setting Sys.setenv(LANG = "en_US.UTF-8") and Sys.setenv(LANG = "Unicode") to no effect, but perhaps some global language setting would affect shape?
Thank you so much for any help!
Note: These solutions for Unicode skull and crossbones and half-filled points do not have legends and will not work without the right font:
To get the right font:
Look for an installed font that contains the Unicode character you seek. I found these instructions helpful.
Import installed fonts into R
library(extrafont)
font_import()
fonts()
sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.3
Use shape="\u2198" instead of \u8600. When specifying a character with \u notation, the value needs to be hexadecimal. 8600 is the decimal value for Unicode character LOWER RIGHT ARROW; the hex value is 2198.
I'd like to use unicode characters as the shape of plots in ggplot, but for unknown reason they're not rendering. I did find a similar query here, but I can't make the example there work either.
Any clues as to why?
Note that I don't want to use the unicode character as a "palette", I want each item plotted by geom_point() to be the same shape (color will indicate the relevant variable).
Running
Sys.setenv(LANG = "en_US.UTF-8")
and restarting R does not help. Wrapping the unicode in sprintf() also does not help.
This is an example bit of code that illustrates the problem:
library(tidyverse)
library(ggplot2)
library(Unicode)
p1 = ggplot(mtcars, aes(wt, mpg)) +
geom_point(shape="\u25D2", colour="red", size=3) +
geom_point(shape="\u25D3", colour="blue", size=3) +
theme_bw()
plot(p1)
And here's what that renders result.
I use macOS Sierra (10.13.6), R version 3.5.1 & Rstudio 1.0.143.
Grateful for any help! I've been scouting several forums looking for a solution and posted to #Rstats, so far nothing has worked. It may be that the solution is hidden in some thread somewhere, but if so I have failed to detect it and I suspect others have also missed it. So, here I am making my first ever post to stack overflow :)
Might it work to use geom_text instead? It allows control of the font, so you can select one with the glyph you need.
library(tidyverse)
ggplot(mtcars, aes(wt, mpg)) +
geom_text(label = "\u25D2", aes(color = as.character(gear)),
size=10, family = "Arial Unicode MS") +
geom_text(label = "\u25D3", colour="blue",
size=10, family = "Arial Unicode MS") +
scale_color_discrete(name = "gear") +
theme_bw()
It's possible to change the font family using par. The problem is that this will affect base R graphics but not ggplot2 graphics, as they use two different graphics devices (grDevices vs. grid). For instance, we can try to plot your example using base R functions, but at first we see the same issue:
plot(mtcars$wt, mtcars$mpg, pch="\u25D2", col = "red", cex = 2)
points(mtcars$wt, mtcars$mpg, pch="\u25D3", col = "blue", cex = 2)
We can get what we want if we call par first (the font should support the symbols):
par(family = "Arial Unicode MS")
plot(mtcars$wt, mtcars$mpg, pch="\u25D2", col = "red", cex = 2)
points(mtcars$wt, mtcars$mpg, pch="\u25D3", col = "blue", cex = 2)
Changing the font family parameter that specifically affects the points in a ggplot geom_point appears to be a bit more complicated. As far as I can tell, it would involve turning the ggplot object into a grob, editing the parameters, and then drawing it. It probably makes more sense to either use Jon Spring's geom_text solution, or use base R.
Is there a good practice to insert unicode characters in a ggplot title and also save it as pdf?
I am struggling with expression, paste and sprintf to get a nice title...
So, what works is
ggtitle(expression(paste('5', mu, 'g')))
This will print an ugly greek mu. By ugly I mean a different font, but overall, it will be printed as pdf without problems. But the problems start, if you want to have new lines in the title. Or maybe I didn't found a solution for this.
My preferred solution would be to use sprintf with the unicode number, so for example
ggtitle(sprintf('5\u03BCg'))
It shows a nice result on the screen but it is not possible to save as pdf with ggsave. PNG works fine, but I would like to use the pdf save option.
Is there a possibility to plot the unicode characters with ggsave? I read about the cairo_pdf device, but this messes up the fonts and I can not save the plot properly.
Thanks in advance for any help.
EDIT:
Example PDF
I just uploaded an example PDF... So maybe my problem is somewhere else...
Try
library(ggplot2)
p <- ggplot(df, aes(x=date, y=value))
p <- p + geom_line()
p + ggtitle(sprintf('5\u03BCg'))
library(Cairo)
ggsave("newfile.pdf", device=cairo_pdf)
data
set.seed(42)
df <- data.frame(date = 1:10 , value = cumsum(runif(10 , max = 10)) )
Using the emojifont package fixes this issue for me.
library(emojifont)
I am sharing the tricks to have Unicode characters properly displayed on PDF files. I am currently running R-4.0.5 for Windows.
library(ggplot2)
library(gridExtra)
library(grid)
library(png)
#--- The trick to get unicode characters being printed on pdf files:
#--- 1. Create a temporary file, say "temp.png"
#--- 2. Create the pdf file using pdf() or cairo_pdf(), say "UnicodeToPDF.pdf"
#--- 3. Combine the use of grid.arrange (from gridExtra), rasterGrob (from grid), and readPNG (from png) to insert the
# temp.png file into the UnicodeToPDF.pdf file
test.plot = ggplot() +
geom_point(data = data.frame(x=1, y=1), aes(x,y), shape = "\u2191", size=3.5) +
geom_point(data = data.frame(x=2, y=2), aes(x,y), shape = "\u2020", size=3.5) +
geom_point(data = data.frame(x=1.2, y=1.2), aes(x,y), shape = -10122, size=3.5, color="#FF7F00") +
geom_point(data = data.frame(x=1.4, y=1.4), aes(x,y), shape = -129322, size=3.5, color="#FB9A99") +
geom_point(data = data.frame(x=1.7, y=1.7), aes(x,y), shape = -128515, size=5, color="#1F78B4") +
ggtitle(sprintf('5\u03BCg'))
ggsave("temp.png", plot = test.plot, width = 80, height = 80, units = "mm")
#--- Refer to http://xahlee.info/comp/unicode_index.html to see more unicode character integers
pdf("UnicodeToPDF.pdf")
grid.arrange(
rasterGrob(
readPNG(
"temp.png",
native=F
)
)
)
dev.off()
file.remove("temp.png")