[英]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.