使用变量和希腊字母在 geom_text 中传递表达式
Pass an expression in geom_text using both a variable and greek letter
我正在尝试将文本添加到包含变量和希腊字母的绘图中。我想显示变量 pt 和 P_T 所取的值,但我希望希腊字母用于 alpha。下面的代码给出了以下错误:
Error in f()
: ! Aesthetics must be valid data columns. Problematic
aesthetic(s): label = expression(...). Did you mistype the name of a
data column or forget to add after_stat()?
如有任何帮助,我们将不胜感激!
x <- rep(seq(1,5),3)
value <- runif(15)
alpha <- rep(c(0.5,0.6,0.7),each=5)
my_data <- data.frame(x=x,value=value,alpha=alpha)
pt=6
P_T=0.7
ggplot(data=my_data, aes(x=x, y=value,group=as.factor(alpha),color=as.factor(alpha)))+
geom_line()+
scale_x_continuous(name =paste("True Landmark PFS at", pt, "Months"), breaks=seq(1,5,1))+
theme(plot.margin= unit(c(0.7, 0.7, 0.7,0.7), "cm"))+
theme(plot.title = element_text(hjust = 0.5, size=12))+
scale_y_continuous(name ="Probability of ESOE",breaks=seq(0,1,by=0.25),limits=c(0,1))+
guides(color = guide_legend(title = expression(alpha)))
geom_text(aes(x=3, label=expression(paste("Posterior prob (True RMST at",pt,"month>",alpha,"|data>",P_T)), y=0.75),
angle=0, size=2.5, color="black")
如果你只想显示一个希腊字母,一个选择就是简单地使用 unicode,例如\u03B1
对于 alpha
。此外,由于您只想向绘图添加一个注释,因此我建议使用 annotate
与 geom_text
一样,您可以多次添加注释:
set.seed(123)
library(ggplot2)
ggplot(data = my_data, aes(x = x, y = value, group = as.factor(alpha), color = as.factor(alpha))) +
geom_line() +
scale_x_continuous(name = paste("True Landmark PFS at", pt, "Months"), breaks = seq(1, 5, 1)) +
theme(plot.margin = unit(c(0.7, 0.7, 0.7, 0.7), "cm")) +
theme(plot.title = element_text(hjust = 0.5, size = 12)) +
scale_y_continuous(name = "Probability of ESOE", breaks = seq(0, 1, by = 0.25), limits = c(0, 1)) +
guides(color = guide_legend(title = expression(alpha))) +
annotate(geom = "text", x = 3, label = paste("Posterior prob (True RMST at", pt, "month > \u03B1 | data>", P_T), y = 0.75,
angle = 0, size = 2.5, color = "black"
)
我正在尝试将文本添加到包含变量和希腊字母的绘图中。我想显示变量 pt 和 P_T 所取的值,但我希望希腊字母用于 alpha。下面的代码给出了以下错误:
Error in
f()
: ! Aesthetics must be valid data columns. Problematic aesthetic(s): label = expression(...). Did you mistype the name of a data column or forget to add after_stat()?
如有任何帮助,我们将不胜感激!
x <- rep(seq(1,5),3)
value <- runif(15)
alpha <- rep(c(0.5,0.6,0.7),each=5)
my_data <- data.frame(x=x,value=value,alpha=alpha)
pt=6
P_T=0.7
ggplot(data=my_data, aes(x=x, y=value,group=as.factor(alpha),color=as.factor(alpha)))+
geom_line()+
scale_x_continuous(name =paste("True Landmark PFS at", pt, "Months"), breaks=seq(1,5,1))+
theme(plot.margin= unit(c(0.7, 0.7, 0.7,0.7), "cm"))+
theme(plot.title = element_text(hjust = 0.5, size=12))+
scale_y_continuous(name ="Probability of ESOE",breaks=seq(0,1,by=0.25),limits=c(0,1))+
guides(color = guide_legend(title = expression(alpha)))
geom_text(aes(x=3, label=expression(paste("Posterior prob (True RMST at",pt,"month>",alpha,"|data>",P_T)), y=0.75),
angle=0, size=2.5, color="black")
如果你只想显示一个希腊字母,一个选择就是简单地使用 unicode,例如\u03B1
对于 alpha
。此外,由于您只想向绘图添加一个注释,因此我建议使用 annotate
与 geom_text
一样,您可以多次添加注释:
set.seed(123)
library(ggplot2)
ggplot(data = my_data, aes(x = x, y = value, group = as.factor(alpha), color = as.factor(alpha))) +
geom_line() +
scale_x_continuous(name = paste("True Landmark PFS at", pt, "Months"), breaks = seq(1, 5, 1)) +
theme(plot.margin = unit(c(0.7, 0.7, 0.7, 0.7), "cm")) +
theme(plot.title = element_text(hjust = 0.5, size = 12)) +
scale_y_continuous(name = "Probability of ESOE", breaks = seq(0, 1, by = 0.25), limits = c(0, 1)) +
guides(color = guide_legend(title = expression(alpha))) +
annotate(geom = "text", x = 3, label = paste("Posterior prob (True RMST at", pt, "month > \u03B1 | data>", P_T), y = 0.75,
angle = 0, size = 2.5, color = "black"
)