I need to add the theta greek letter in the x-axis label of this plot(s):
var=c("a", "b", "c")
df=data.frame(x=c(1:20),y=c(41:60))
df_plot=list()
for (i in 1:length(var)) {
df_plot[[i]]=ggplot()
geom_line(data=df, aes(x=x, y=y))
xlab(paste("theta ", var[i]))
}
How can I do it?
If I use expression()
I get the letter but not the index i.
CodePudding user response:
Using bquote
you could do:
Note: Instead of using a for
loop I switched to lapply
(and would suggest to so) as sooner or later you will run in issues related to tidy evaluation when using a for
loop with ggplot
. And there are plenty of questions on SO related to that. (:
var <- c("a", "b", "c")
df <- data.frame(x = c(1:20), y = c(41:60))
library(ggplot2)
lapply(var, function(x) {
ggplot()
geom_line(data = df, aes(x = x, y = y))
xlab(bquote(theta*.(x)))
})
#> [[1]]
#>
#> [[2]]
#>
#> [[3]]