R:geom_smooth降至特定值以下的点

嗨,堆栈溢出社区,

我希望我要问的两个相互关联的问题不要太笨拙。我尝试了几次Google搜索,但找不到解决方案。

我用R绘制语言“实验”的结果,在其中检查了两种语法结构对事件的接受程度,具体取决于事件的表现方式。我的数据看起来像这样:

event,PFV.alone,PFV.and.PART
0.01,1
0.01,1
0.05,1
0.1,1
0.25,1
0.3,1
0.33,1

....

0.67,1,0.5
0.75,0
0.75,0
0.8,1
0.8,0
0.85,1
0.85,0
0.9,0
0.95,0

如您所见,对于这两种构造中的每一种,都存在“高原”,其中可接受性为0或1,然后是“过渡”区域。为了说明“高原”,我使用geom_smooth,并使用geom_segment为它们之间的分散数据创建了平滑的“过渡”。这是我的代码:

#after loading datafile into "Daten":
p <- ggplot(data = Daten,aes(x=event,y=PFV.and.PART,xmin=0,ymin=0,xmax=1,ymax=1))
    p + geom_blank() +
    coord_fixed()+
    xlab("Progress of the event") + 
    ylab("acceptability") +
    geom_segment(x=0,xend=1,y=0.5,yend=0.5,linetype="dotted") +
    geom_smooth(data=(subset(Daten,event==0.33 | event ==0.9)),aes(color="chocolate"),method="loess",fullrange=FALSE,level=0.95,se=FALSE) +
    geom_segment(x=0,xend=0.33,y=1,yend=1,color="chocolate",size=1) +
    geom_segment(x=0.9,y=0,yend=0,size=1) +
    geom_smooth(data=(subset(Daten,event==0.33 | event==0.67)),aes(x = event,y = PFV.alone,color="cyan4"),method="lm",se=FALSE) +
    geom_segment(color="cyan4",x=0,size=1) +
    geom_segment(color="cyan4",x=0.67,size=1) +
    scale_x_continuous(labels = scales::percent) +
    scale_y_continuous(breaks = c (0,0.5,1),labels = scales::percent)+
    labs(color='Construction')+
    scale_color_manual(labels = c("PFV + PART","PFV alone"),values = c("chocolate","cyan4")) +
    theme(legend.position=c(0.05,0.8),legend.justification = c("left","top"),legend.background = element_rect(fill = "darkgray"))

这段代码生成了一个漂亮的图形,但是我需要帮助进行一次计算和一个与绘图有关的问题。

R:geom_smooth降至特定值以下的点

  1. 首先,也是最重要的是,我想找出“ PFV.and.PART”的geom_smooth(黄土)曲线在什么时候下降到0.5,即达到50%的可接受度。我担心这可能会涉及一些安静的复杂代码吗?

  2. 关于上一点,我想标记两条曲线都在0.5以上(可接受性50%)的区域/线,或者说我要说明的内容:两种结构产生的描述至少可以接受50%的情况。当然,这将基于第1点,因为必须确定右极限,而左极限似乎不存在问题,因为它似乎位于x = 0.5,y = 0.5。

我非常感谢您的帮助,希望我已提供了所有必要的信息。如果这个问题在其他地方已经解决,请原谅。

Bamboo5290 回答:R:geom_smooth降至特定值以下的点

这是一种方法,涉及在ggplot之外拟合黄土模型

# Generate some data
set.seed(2019)
my_dat <- c(sample(c(1,0.5,0),33,prob = c(0.85,0.15,replace = TRUE),sample(c(1,prob = c(0.1,0.7,0.1),34,prob = c(0,0.85),replace = TRUE))

df <- tibble(x = 1:100,y = my_dat)

# fit a loess model
m1 <- loess(y~x,data = df)

df <- df %>% 
  add_column(pred = predict(m1)) # predict using the loess model

# plot
df %>% 
  ggplot(aes(x,y))+
  geom_point() +
  geom_line(aes(y = pred))

# search for a  value of x that gives a prediction of 0.5

f <- function(x) { 0.5 - predict(m1)[x]}
uniroot(f,interval = c(1,100))

# $root
# [1] 53.99997

enter image description here

本文链接:https://www.f2er.com/3103961.html

大家都在问