Greek Letters in Ggplot Strip Text

Greek letters in ggplot strip text

Try this:

p + facet_grid(fy ~ fx, labeller = label_parsed)

How to add greek letters to Facet_Grid strip labels?

If you encode your facetting variables as character plotmath expressions you can use label_parsed() as labeller argument to the facet. Example below:

library(ggplot2)

df <- expand.grid(1:3, 1:3)
df$FacetX <- c("'p = 0.1'", "'p = 0.5'", "'p = 0.9'")[df$Var1]
df$FacetY <- c('mu[2]*" = 0.1"', 'mu[2]*" = 1"', 'mu[2]*" = 10"')[df$Var2]

ggplot(df, aes(Var1, Var2)) +
geom_point() +
facet_grid(FacetY ~ FacetX, labeller = label_parsed)

Sample Image

Created on 2020-08-26 by the reprex package (v0.3.0)

EDIT:

Based on your comment that the variables are encoded as numerics, I think the glue package might help you construct these labels.

library(ggplot2)
library(glue)

df <- expand.grid(1:3, 1:3)
df$FacetX <- c(0.1, 0.5, 0.9)[df$Var1]
df$FacetY <- c(0.1, 1, 10)[df$Var2]

ggplot(df, aes(Var1, Var2)) +
geom_point() +
facet_grid(glue('mu[2]*" = {FacetY}"') ~ glue("'p = {FacetX}'"),
labeller = label_parsed)

Sample Image

Created on 2020-08-26 by the reprex package (v0.3.0)

How to place greek alphabet in the naming of facet_grid in ggplot

You can use plotmath expressions in combination with label_parsed() as the facet's labeller argument.

set.seed(1)
df <- data.frame(Type=c(rep("A",1000),rep("B",4000)),
Value=c(rnorm(1000,mean=25,sd=10),rchisq(4000,15)))
library(ggplot2)

df$label <- ifelse(df$Type == "A", "sigma~1", "sigma~2")

ggplot(df, aes(x=Value))+
geom_histogram(aes(y=..density..,fill=Type),color="grey80")+
facet_grid(label~., labeller = label_parsed)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Sample Image

Labelling issues with Greek letters, superscript and expression in facet_wrap

First I created some fake data. You could use the function glue to create these labels. With {} you assign the greek symbols to each value of your specific column in this case "p". You can use the following code:

data <- data.frame(N = runif(20, 200, 600),
alpha = runif(20, 0.03, 0.06),
low = runif(20, 0.03, 0.04),
upp = runif(20, 0.04, 0.05),
p = rep(c(0.6, 0.7, 0.8, 0.9), 5))

library(ggplot2)
library(glue)

ggplot(data,aes("x" = N, "y" = alpha))+
geom_ribbon(aes(ymin = low, ymax = upp), fill = "lightblue")+
geom_point()+
geom_hline(yintercept = 0.05, linetype = "dashed")+
facet_wrap(~glue('Phi[0]*" = {p}"'), nrow = 2, labeller = label_parsed)+
labs("y" = "Type I error rates")

Sample Image

Created on 2022-08-25 with reprex v2.0.2

ggplot greek letter in label within for loop indices

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]]

Sample Image

#> 
#> [[2]]

Sample Image

#> 
#> [[3]]

Sample Image

R: Programmatically changing ggplot scale labels to Greek letters with expressions

Another option to achieve your desired result would be to add a new column to your data which contains the ?plotmath expression as a string and map this new column on y. Afterwards you could use scales::label_parse() to parse the expressions:

set.seed(123)

df <- data.frame(name = rep(c("alpha1","alpha2"), 50),
value = rnorm(100))
df$label <- gsub("^(.*?)(\\d+)$", "\\1[\\2]", df$name)

library(ggplot2)
library(scales)

ggplot(df, aes(x = value, y = label)) + geom_density() +
scale_y_discrete(labels = scales::label_parse())

Sample Image

How to insert Greek letters in lattice conditional labels strips?

Instead of expression, you can just use the unicode for "tau":

# Generate some values
x<-rnorm(100,10,4)
y<-rnorm(100,10,1)
cond1<-rbinom(100,1,0.5)
cond2<-rbinom(100,1,0.5)

groups<-sample(c(0:10),100,replace=TRUE)
dataa<-data.frame(y,x,cond1,cond2,groups)
cond1<-factor(cond1,labels = c("\u03C4","cond1"))
cond2<-factor(cond2,labels = c("\u03C4","cond2"))

# ploting function
xyplot(y~x|cond1*cond2,groups=groups,
col = gray(seq(0.01,0.7,length=length(levels(as.factor(groups))))),
pch = 1:length(levels(as.factor(groups))),
key = NULL)

Output:

> levels(cond1)
[1] "τ" "cond1"

Sample Image



Related Topics



Leave a reply



Submit