在ggplot2中使用Unicode字符(加减号) [英] Using Unicode character (plus minus sign) inside ggplot2

查看:104
本文介绍了在ggplot2中使用Unicode字符(加减号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个包含Unicode字符的情节(正负号

有没有一种方法可以将+-更改为Unicode字符U + 00B1

我尝试使用反斜杠进行转义:粘贴("\ U + 00B1",回合(错误,2))但是我得到了错误:错误:字符串'"\ U"中以不带十六进制数字的形式使用'\ U'

任何帮助将不胜感激.最好的问候

解决方案

这里有两种方法:

  ggplot(df,aes(x =额定运动,y =评级者,填充=错误))+geom_tile(color ="black",size = 0.5,alpha = 0.8)+geom_text(aes(label = paste(±",round(Error,2)))))+scale_fill_gradientn(颜色= terrain.colors(10))+主题(panel.grid.major = element_blank(),panel.grid.minor = element_blank())ggplot(df,aes(x =额定运动,y =评分器,填充=错误))+geom_tile(color ="black",size = 0.5,alpha = 0.8)+geom_text(aes(label = paste("\ u00B1",round(Error,2))))+scale_fill_gradientn(颜色= terrain.colors(10))+主题(panel.grid.major = element_blank(),panel.grid.minor = element_blank()) 

I am trying to build a plot that includes a unicode character (The plus-minus sign U+00B1)

Here is some fake data:

library(tidyverse)
set.seed(1)
df <- crossing(
  Rated_Movement = c("Running", "Jumping"),
  Rater = c("John Snow", "Batman", "Hulk")) %>% 
  mutate(
    Error = runif(n = 6, min = 0, max=2))

Here is my code for the plot so far:

ggplot(df, aes(x = Rated_Movement, y = Rater, fill = Error)) + 
  geom_tile(color = "black", size = 0.5, alpha = 0.8)+
  geom_text(aes(label = paste("+-", round(Error,2))))+
  scale_fill_gradientn(colours = terrain.colors(10))+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

Here is the plot:

Is there a way that I can change the +- to unicode character U+00B1

I tried using the backslash to escape: paste("\U+00B1", round(Error,2)) But I get the error: "Error: '\U' used without hex digits in character string starting ""\U""

Any help will be much appreciated. Best regards

解决方案

here are two ways:

ggplot(df, aes(x = Rated_Movement, y = Rater, fill = Error)) + 
  geom_tile(color = "black", size = 0.5, alpha = 0.8)+
  geom_text(aes(label = paste("±", round(Error,2))))+
  scale_fill_gradientn(colours = terrain.colors(10))+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

ggplot(df, aes(x = Rated_Movement, y = Rater, fill = Error)) + 
  geom_tile(color = "black", size = 0.5, alpha = 0.8)+
  geom_text(aes(label = paste("\u00B1", round(Error,2)))) +
  scale_fill_gradientn(colours = terrain.colors(10))+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

这篇关于在ggplot2中使用Unicode字符(加减号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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