簡體   English   中英

為每個 facet_wrap 圖改變 geom_hline

[英]Varying geom_hline for each facet_wrap plot

對於九個圖中的每一個,我都試圖創建兩條與“方法”顏色相同的水平線,其中 y 截距是“圍棋概率”的值(忽略這些不是有效的概率),當“真地標”為 0.65。 例如,左上圖將在大約 y=6.25 處有一條藍色水平線,在大約 y=5.2 處有一條紅色水平線。 有沒有一種簡單的方法可以做到這一點,而無需手動為 9 個圖和 2 種方法中的每一個指定 y 截距? 我現在的代碼如下:

true_landmark=seq(0.15,0.85,by=0.05);pt=6;theta=0.65
L=length(true_landmark)
type <- rep(c(rep("Exponential",L*2),rep("Log-Normal",L*2),rep("Weibull",L*2)),3)
method <- rep(c(rep("Landmark PFS",L),rep("RMST",L)),3*3)
n <- c(rep(20,L*2*3),rep(30,L*2*3),rep(40,L*2*3))
value <- replicate(18,cumsum(runif(15)))
landmark <- rep(true_landmark,2*3*3)  
df <- data.frame(type,method,n,value,landmark)
df$n <- factor(df$n,labels=c(expression(n==20),expression(n==30),expression(n==40)))
df$type <- factor(df$type,
                  labels=c("Exponential",
                           expression(paste("Log-Normal (",sigma==1,")")),
                           expression(paste("Weibull (",k==0.5,")"))))

ggplot(df, aes(landmark,value,color=method)) +
  geom_line(alpha=0.5)+
  geom_point(shape=19,alpha=0.5)+
  geom_blank()+
  scale_x_continuous(name =paste("True Landmark PFS at", pt, "Months"), breaks=seq(true_landmark[1],true_landmark[length(true_landmark)], 0.1))+
  ylab(label="Probability of Go")+
  geom_vline(xintercept = theta, color="black", linetype="dashed")+
  #geom_text(aes(x=theta-0.02, label=paste("Target value =", theta), y=0.30), angle=90, size=4, color="black")+
  facet_grid(n~type,labeller=label_parsed)+
  guides(color=guide_legend(title="Method"))+
  theme(plot.caption=element_text(hjust=0))+
  labs(caption=paste("Go: Posterior prob (True PFS/RMST at", pt, "month > target|data)", ">", P_T,"\nDashed line indicates target landmark PFS/RMST value"))

在此處輸入圖像描述

通過將value作為數組保留在數據框之外,您使事情變得更加困難(請注意,盡管您在制作df時將其包含在內,但它只是創建了一堆稱為 X1、X2 等的列)。 您可以這樣解決問題:

ggplot(df, aes(landmark, value, color = method)) +
  geom_line(alpha = 0.5)+
  geom_point(shape = 19, alpha = 0.5) +
  geom_blank() +
  geom_hline(data = df[df$landmark == 0.65,],
             aes(yintercept = value[df$landmark == 0.65], color = method)) +
  scale_x_continuous(name = paste("True Landmark PFS at", pt, "Months"), 
                     breaks = seq(true_landmark[1], 
                                  true_landmark[length(true_landmark)], 0.1)) +
  ylab(label="Probability of Go") +
  geom_vline(xintercept = theta, color = "black", linetype = "dashed") +
  facet_grid(n~type,labeller = label_parsed)+
  guides(color = guide_legend(title = "Method")) +
  theme(plot.caption = element_text(hjust = 0)) +
  labs(caption = paste("Go: Posterior prob (True PFS/RMST at", pt, 
                      "month > target|data)", ">",
                      "\nDashed line indicates target landmark PFS/RMST value"))

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM