R - 绘图标签文本的文本格式 - 删除线 [英] R - Text Formatting of Plot Label Text - Strikethrough

查看:184
本文介绍了R - 绘图标签文本的文本格式 - 删除线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让一部分标签文字在情节标签中有删除线?例如,要让y轴标签读取标签中的 删除线文本?

  ggplot(mpg,aes(x = displ,y = hwy))
+ geom_point()
+ ylab(~~ strikethrough ~~标签中的文本?)

很小的问题我认为找到一个解决方案也很简单,但一段时间后却无济于事。

解决方案

您可以为 axis.text.y 。我试图得到一个通用的解决方案,但我认为我的解决方案有点棘手,并不是很干净,因为我必须手动设置某个视口的y位置(请参阅代码以获得更好的解释)。

自定义axis.text.y有两个参数:轴标签和文本以穿透它。它找到文本的位置,用轴标签打开并添加一个段(如果文本被定义了两次,则只需要第一次出现)。

到您可以使用这样的解决方案:

  library(ggplot2)
library(grid)
ggplot(mpg,aes(x = displ,y = hwy))+
geom_point()+ theme(axis.title.y = element_blank())+
theme(axis.text.y = axis .strike(strike =label,
lab =删除标签中的文字?))



自定义axis.text.y元素的代码:

 #用户界面:用户调用的元素
轴。罢工=功能(罢工,实验室){
结构(
列表(罢工=罢工,实验室=实验室),
##继承,因为它应该是一个element_text
class = c (element_custom,element_blank)

}

element_grob.element_custom< - function(element,x,y){
##轴标签
gX< - textGrob(元素$ lab,rot = 90 ,vjust = -0.25)
##我在这个视口中使用grob文本尺寸(高度,宽度,位置)到
##创建一个视口vp
##我创建一个段
unit.H< - grobHeight(gX)
unit.W< - grobWidth(gX)
rate< - nchar(element $ strike)
##文本的位置以触发
pos< - as.numeric(gregexpr(element $ strike,element $ lab)[[1]])
vp = viewport(just =center,
## BAD OFFSET HERE !!
## TODO:找到更好的方法来定义视口y位置
y = grobY(gX,'south')+ unit(5,'line'),
yscale = c(0,nchar (元素$ lab)),
width = unit.W,height = unit.H)
g.seg < - segmentsGrob(vp = vp,x0 = 0,x1 = 0,
y0 = unit(pos-1,'native'),
y1 = unit(pos-1 + rate,'native'))
gTree(children = gList(g.seg,gX,g .seg),cl =custom_axis)
}


How does one make a portion of label text have strikethrough in a plot label?

For example, to get the y-axis-label to read "strikethrough text in a label?"

 ggplot(mpg, aes(x=displ, y=hwy)) 
        + geom_point() 
        + ylab("~~strikethrough~~ text in a label?")

Pretty small question that I thought would be pretty trivial to find a solution too, but no avail after a while of looking.

解决方案

You can create a custom element function for axis.text.y. I tried to get a general solution but I think my solution is a little bit tricky and not very clean since I had to set manually the y position of some viewport(see the code for better explanation)

The custom axis.text.y had 2 arguments : the axis label and the text to strike through it. It finds the position of the text to strike with the axis label and add a segment.(If the text is defined twice it would take only the first occurrence).

To use the solution you can do something like this:

library(ggplot2)
library(grid)
ggplot(mpg, aes(x=displ, y=hwy)) +
    geom_point() + theme(axis.title.y=element_blank())+
    theme( axis.text.y = axis.strike(strike = "label",
               lab="strikethrough text in a label?"))

the code of the custom axis.text.y element:

# user interface : element called by the user
axis.strike = function(strike,lab) {
    structure(
        list(strike=strike,lab=lab),
        ## inheritance since it should be a element_text
        class = c("element_custom","element_blank")  
    )
}

element_grob.element_custom <- function(element, x,y)  {
    ## the axis label
    g.X <- textGrob(element$lab,rot=90,vjust=-0.25)
    ## I use the grob text dimensions(height,width,position) to 
    ## create a viewport vp
    ## within this viewport I create a segment 
    unit.H <- grobHeight(g.X)
    unit.W <- grobWidth(g.X)
    rate <- nchar(element$strike)
    ## search of the position of the text to strike
    pos <- as.numeric(gregexpr(element$strike,element$lab)[[1]])
    vp=viewport(just="centre",
          ##BAD OFFSET HERE!!
          ## TODO: find better way to define viewport y position
           y = grobY(g.X,'south')+unit(5,'line'), 
           yscale=c(0,nchar(element$lab)),
          width =unit.W,height=unit.H)
    g.seg <- segmentsGrob(vp=vp,x0=0,x1=0,
                           y0=unit(pos-1,'native'),
                           y1=unit(pos-1+rate,'native'))
    gTree(children=gList(g.seg,g.X,g.seg),cl = "custom_axis")
}

这篇关于R - 绘图标签文本的文本格式 - 删除线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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