在百分号(%)中使用ggplot2中的plotmath [英] Using plotmath in ggplot2 with percent sign (%)

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

问题描述

我希望在ggplot2条形图的facet标签中使用希腊字符,拉丁字符和百分号。可以用'facet_grid(。〜variable,labeller = label_parsed)'完成希腊字符:

$ p $ a< -c(Delta 〜V,VarcoV,Delta〜V,VarcoV)
b< -c(1,2,3,4)
d< -c(one,one ,two,two)
mydata <-data.frame(cbind(b,a,d))
ggplot(mydata,aes(x = d,y = b))+ facet_grid(。〜a,labeller = label_parsed)+ geom_bar(stat =identity)

还需要添加包含%和拉丁字符的facet标签:

  a< -c(Delta〜V, VarcoV,%V,Delta〜V,VarcoV,%V)
b <-c(1,2,3,4,5,6)
d < ; -c(one,one,one,two,two,two)
mydata <-data.frame(cbind(b,a,d))
ggplot(mydata,aes(x = d,y = b))+ facet_grid(。〜a,labeller = label_parsed)+ geom_bar(stat =identity)
pre>

这会产生以下错误:

 解析错误( text = x):< text>:1:1:意外输入
1:%V
^

任何想法如何包含百分号?

解决方案

拉丁字符不需要任何特殊处理,您可以在 a 的第一个元素中看到它。试试这个:

  a< -c(Delta_V,VarcoV,'%'* V, Delta〜V,VarcoV,'%'* V)

标志很特别,所以你需要引用它。您可以刚刚完成'%V',但是我扔了*(星号)来显示如何分隔 plotmath 标记而不显示空格。 (您已经知道如何使用间距分隔符分隔标记,〜。)



关键的一课是混合引号类型。第一个引用类型将指示用于终止字符标记/字符串的类型。您也可以使用转义字符:\。这也成功了:

  a< -c(Delta〜V,VarcoV,\%\ * V,Delta〜V,VarcoV,\%\* V)


I would like to use Greek characters, Latin characters and the percent sign in the facet labels of a ggplot2 bar chart. Greek characters can be done with 'facet_grid(.~variable, labeller=label_parsed)':

    a<-c("Delta~V","VarcoV","Delta~V","VarcoV")
    b<-c(1,2,3,4)
    d<-c("one","one","two","two")
    mydata<-data.frame(cbind(b,a,d))
    ggplot(mydata,aes(x=d,y=b))+facet_grid(.~a, labeller=label_parsed)+geom_bar(stat="identity")

Now I also want to add a facet label that includes % and a Latin character:

    a<-c("Delta~V","VarcoV","%V","Delta~V","VarcoV","%V")
    b<-c(1,2,3,4,5,6)
    d<-c("one","one","one","two","two","two")
    mydata<-data.frame(cbind(b,a,d))
    ggplot(mydata,aes(x=d,y=b))+facet_grid(.~a, labeller= label_parsed)+geom_bar(stat="identity")

This produces the following error:

    Error in parse(text = x) : <text>:1:1: unexpected input
    1: %V
       ^

Any ideas how to include the percent sign?

解决方案

Latin characters do not need any special treatment and you can see this in the first element of a. Try this:

a<-c("Delta~V","VarcoV","'%'*V","Delta~V","VarcoV","'%'*V")

The "%" sign is special so you need to quote it. You could have just done '%V' but I threw in the "*" (asterisk) to show how to separate plotmath tokens with no displayed space. (You already appear to know how to separate tokens with the spacing-separator, "~".)

The key lesson is to mix type of quotes. The first quote type will signal which type is used to terminate the character token/string. You can also use the escape character: "\". This also succeeds:

a<-c("Delta~V","VarcoV","\"%\"*V","Delta~V","VarcoV","\"%\"*V")

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

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