直接标签:避免裁剪(如xpd = TRUE) [英] directlabels: avoid clipping (like xpd=TRUE)

查看:212
本文介绍了直接标签:避免裁剪(如xpd = TRUE)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的图中,直接标签位置垂直调整了一些,但是它们在左/右边缘处被裁剪。有没有办法避免剪裁(类似于 xpd = TRUE )或者在剧情框架中向内调整剪辑标签?





以下是此代码例如:

  library(car)
library(reshape2)
library(ggplot2)
库(直接标签)
库(nnet)

## Sec。 8.2(嵌套二分法)

#变换数据

女性< - 女性内部({女性,{
工作< - recode(partic,'not.work '='no'; else ='yes')
fulltime< - recode(partic,
''fulltime'='yes';'parttime'='no';'not.work '= NA)})

mod.working< - glm(working〜hincome + children,family = binomial,
data = Womenlf)
mod.fulltime< - glm(全日制〜hincome +儿童,family =二项式,
data =女装)

预测器< - expand.grid(hincome = 1:50,
children = c (缺席,当前))
fit< - data.frame(预测变量,
p.working = predict(mod.working,predictors,type =response),
p.fulltime = predict(mod.fulltime,predictors,type =response),
l.working = predict(mod.working,predictors,type =link),
l.fulltime =预测(mod.fulltime,预测,类型=链接)


fit< - within(fit,{
`full-time`< - p.working * p.fulltime
`part-time` < - p.working *(1 - p.fulltime)
`not working`< - 1 - p.working
})

#图8.1​​0
fit2 = melt(fit,
measure.vars = c(全职,兼职,不工作),
variable.name =参与,
value.name =概率)

gg < - ggplot(fit2,
aes(x = hincome,y = Probability,color = Participation))+
facet_grid (〜children,labeller = function(x,y)sprintf(%s =%s,x,y))+
geom_line(size = 2)+ theme_bw()

direct.label(gg,list(top.bumptwice,dl.trans(y = y + 0.2)))


正如@rawr在评论中指出的,你可以使用链接问题关闭裁剪,BU如果您扩大绘图的比例以使标签贴合,情节看起来会更好。我没有使用过直接标签,也不确定是否有办法调整各个标签的位置,但这里有三个选项:(1)关闭裁剪,(2)扩大绘图区域,使标签适合,( 3)使用geom_text代替直接标签来放置标签。

 #1.关闭剪辑,以便标签可以被看到如果它们是情节区域外的
#。
gg = direct.label(gg,list(top.bumptwice,dl.trans(y = y + 0.2)))

gg2 < - ggplot_gtable(ggplot_build(gg) )
gg2 $ layout $ clip [gg2 $ layout $ name ==panel]< - off
grid.draw(gg2)

 #2.展开x和y限制,使标签符合
gg < - ggplot(fit2,$ b $ (x,y)sprintf(%s =%s,x,y))+ b bes(x = hincome,y =概率,颜色=参与))+
facet_grid
geom_line(size = 2)+ theme_bw()+
scale_x_continuous(limits = c(-3,55))+
scale_y_continuous(limits = c(0,1))

direct.label(gg,list(top.bumptwice,dl.trans(y = y + 0.2)))

 #3.为标签位置和标签创建一个单独的数据框使用geom_text 
#(而不是直接标签)来定位标签。我已经设置好了,所以
#标签会出现在每条曲线的右端,但您可以更改
#以满足您的需求。
library(dplyr)
labs = fit2%>%group_by(children,Participation)%>%
summary(Probability = Probability [which.max(hincome)],$ b $ (b = hincome = max(hincome))

gg < - ggplot(fit2,
aes(x = hincome,y = Probability,color = Participation))+
facet_grid 〜child,labeller = function(x,y)sprintf(%s =%s,x,y))+
geom_line(size = 2)+ theme_bw()+
geom_text(data =实验室,aes(label = Participation),hjust = -0.1)+
scale_x_continuous(limits = c(0,65))+
scale_y_continuous(limits = c(0,1))+
guides(color = FALSE)


In the plot below, direct label positions were tweaked a bit vertically, but they get clipped at the left/right edges. Is there any way to avoid clipping (similar to xpd=TRUE) or adjust the clipped labels inwards in the plot frames?

Here's the code for this example:

library(car)
library(reshape2)
library(ggplot2)
library(directlabels)
library(nnet)

## Sec. 8.2 (Nested Dichotomies)

# transform data

Womenlf <- within(Womenlf,{
  working <-  recode(partic, " 'not.work' = 'no'; else = 'yes' ")
  fulltime <- recode(partic,
    " 'fulltime' = 'yes'; 'parttime' = 'no'; 'not.work' = NA")})

mod.working <- glm(working ~ hincome + children, family = binomial,
                   data = Womenlf)
mod.fulltime <- glm(fulltime ~ hincome + children, family = binomial,
                    data = Womenlf)

predictors <- expand.grid(hincome = 1:50,
                          children = c("absent", "present"))
fit <- data.frame(predictors,
    p.working = predict(mod.working, predictors, type = "response"),
    p.fulltime = predict(mod.fulltime, predictors, type = "response"),
    l.working = predict(mod.working, predictors, type = "link"),
    l.fulltime = predict(mod.fulltime, predictors, type = "link")
)

fit <- within(fit, {
  `full-time` <- p.working * p.fulltime
  `part-time` <- p.working * (1 - p.fulltime)
  `not working` <- 1 - p.working
  })

# Figure 8.10
fit2 = melt(fit,
            measure.vars = c("full-time","part-time","not working"),
            variable.name = "Participation",
            value.name = "Probability")

gg <- ggplot(fit2,
             aes(x = hincome, y = Probability, colour = Participation)) + 
        facet_grid(~ children, labeller = function(x, y) sprintf("%s = %s", x, y)) + 
        geom_line(size = 2) + theme_bw()

direct.label(gg, list("top.bumptwice", dl.trans(y = y + 0.2)))

解决方案

As @rawr pointed out in the comment, you can use the code in the linked question to turn off clipping, but the plot will look nicer if you expand the scale of the plot so that the labels fit. I haven't used directlabels and am not sure if there's a way to tweak the positions of individual labels, but here are three other options: (1) turn off clipping, (2) expand the plot area so the labels fit, and (3) use geom_text instead of directlabels to place the labels.

# 1. Turn off clipping so that the labels can be seen even if they are 
# outside the plot area.
gg = direct.label(gg, list("top.bumptwice", dl.trans(y = y + 0.2)))

gg2 <- ggplot_gtable(ggplot_build(gg))
gg2$layout$clip[gg2$layout$name == "panel"] <- "off"
grid.draw(gg2)

# 2. Expand the x and y limits so that the labels fit
gg <- ggplot(fit2,
             aes(x = hincome, y = Probability, colour = Participation)) + 
  facet_grid(~ children, labeller = function(x, y) sprintf("%s = %s", x, y)) + 
  geom_line(size = 2) + theme_bw() +
  scale_x_continuous(limits=c(-3,55)) +
  scale_y_continuous(limits=c(0,1))

direct.label(gg, list("top.bumptwice", dl.trans(y = y + 0.2)))

# 3. Create a separate data frame for label positions and use geom_text 
# (instead of directlabels) to position the labels. I've set this up so the
# labels will appear at the right end of each curve, but you can change
# this to suit your needs.
library(dplyr)
labs = fit2 %>% group_by(children, Participation) %>%
  summarise(Probability = Probability[which.max(hincome)],
            hincome = max(hincome))

  gg <- ggplot(fit2,
             aes(x = hincome, y = Probability, colour = Participation)) + 
    facet_grid(~ children, labeller = function(x, y) sprintf("%s = %s", x, y)) + 
    geom_line(size = 2) + theme_bw() +
    geom_text(data=labs, aes(label=Participation), hjust=-0.1) +
    scale_x_continuous(limits=c(0,65)) +
    scale_y_continuous(limits=c(0,1)) +
    guides(colour=FALSE)

这篇关于直接标签:避免裁剪(如xpd = TRUE)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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