在ggplot图中使用unicode字符的舒适方法 [英] comfortable way to use unicode characters in a ggplot graph

查看:144
本文介绍了在ggplot图中使用unicode字符的舒适方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ggplot标题中插入unicode字符并保存为pdf是否有一个好习惯?



我在表达式,粘贴和sprintf中挣扎以获得一个好标题...

所以,有效的是

pre $ g $ c $ ggtitle (表达式('''','',''))
丑陋的希腊亩。通过丑陋我的意思是不同的字体,但总体而言,它将被打印为PDF没有问题。但是如果你想在标题中添加新的话,问题就会出现。或者我可能没有找到解决方案。



我的首选解决方案是使用sprintf和unicode编号,例如

  ggtitle(sprintf('5\\\μg'))

它在屏幕上显示出一个不错的结果,但不能用ggsave保存为pdf。 PNG工作正常,但我想使用pdf保存选项。



是否有可能使用ggsave绘制unicode字符?我阅读了关于cairo_pdf设备的信息,但是这会弄乱字体,我无法正确保存这些图。



预先感谢您的帮助。



编辑:
示例PDF



我刚刚上传了一个示例PDF ...因此,也许我的问题在其他地方......

解决方案

试试

  library(ggplot2)
p <-ggplot(df,aes(x = date,y = value))
p <-p + geom_line()
p + ggtitle(sprintf('5\\\μg'))
图书馆(开罗)
ggsave(newfile.pdf,device = cairo_pdf)



data



  set.seed(42)
df < - data.frame(date = 1:10, value = cumsum(runif(10,max = 10)))


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

这篇关于在ggplot图中使用unicode字符的舒适方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆