gpt4 book ai didi

r - 直接标签 : avoid clipping (like xpd=TRUE)

转载 作者:行者123 更新时间:2023-12-02 17:32:05 25 4
gpt4 key购买 nike

在下图中,直接标签位置在垂直方向上进行了一些调整,但它们在左/右边缘被剪切。有什么方法可以避免剪切(类似于xpd=TRUE)或在绘图框架中向内调整剪切的标签?

nested1

以下是此示例的代码:

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

最佳答案

正如@rawr在评论中指出的,您可以使用 linked question 中的代码关闭剪切,但如果您扩大绘图的比例以使标签适合,绘图看起来会更好。我没有使用过直接标签,不确定是否有办法调整各个标签的位置,但这里有其他三个选项:(1)关闭剪切,(2)扩大绘图区域以使标签适合,并且( 3)使用geom_text而不是directlabels来放置标签。

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

enter image description here

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

enter image description here

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

enter image description here

关于r - 直接标签 : avoid clipping (like xpd=TRUE),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30061563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com