Plotting Axis Labels with Greek Symbols from a Vector

Plotting axis labels with Greek symbols from a vector

Try this:

xlab = parse(text=paste(k, "*mol/mol"))

plotting axis labels with both Greek symbols from vector and subscripts

This should do it. If you loop through an index i, you don't need a counter.

symbol_list = c("alpha", "beta", "gamma")
for (i in 1:length(symbol_list)){
# random data
x <- runif(10, 0,1);
y <- runif(10, 0,1);
png(paste0("test",i,".png"));
# make graph with different symbol in x label at each iteration
plot(x,y, xlab=parse(text=paste("log~",symbol_list[i],"[R]")))
dev.off();
}

R: creating vectors of latin/greek expression for plot titles, axis labels, or legends

Here's an alternative that avoids parse(), and works with the example mentioned in your first comment to @mnel's nice answer:

greek <- c("alpha", "beta", "gamma", "delta")
cnames <- paste(LETTERS[1:4], letters[1:4])

legend_expressions <-
sapply(1:4, function(i) {
as.expression(substitute(A (B),
list(A = as.name(cnames[i]), B = as.name(greek[i]))))
})

matplot(data)
legend(1,max(data),fill=c("black","red","green","blue"),legend_expressions)

Sample Image

Both mathematical and Greek characters in graph label using R

This is one solution using unicode: plot(0,0, xlab = "AUC (\U03BCM \U00D7 min)")

Sample Image


Another solution using expression: plot(0,0, xlab = expression(AUC~"("*mu*M %*% min*")"))

Adding greek characters with variables to axis title

I'd try using a plotmath list call:

plot(1,1, main= bquote(list( alpha == .(alpha[1]) ,  beta == .(beta[1])) )  )

Vector of Greek Symbols on Ggplot2 Grid Label

One option to achieve your desired result would be to set up you phi vector as a list column instead of as a character vector. Afterwards you could use e.g. purrr::map_chr to create your plotmath labels by looping over the list column:

library(ggplot2)
library(tibble)

# Instead of using a character vector make your phi column a list column.

AR2_RMSE_NEWDAT <- tibble::tibble(
RMSE = AR2_RMSE_reshapp1$value,
year = AR2_RMSE_reshapp1$ID,
n = rep(rep(c("10", "15", "20", "25"), each = 1), 24),
Methods = rep(rep(c("MBB", "MBBR"), each = 4), 12),
sd = rep(rep(c(1, 3, 5, 10), each = 24), 1),
phi = rep(rep(list(c(0.4, 0.4), c(0.45, 0.45), c(0.35, 0.6)), each = 8), 4)
)

AR2_RMSE_NEWDAT$sd <- factor(AR2_RMSE_NEWDAT$sd, levels = AR2_RMSE_NEWDAT$sd, labels = paste("sd ==", AR2_RMSE_NEWDAT$sd))
AR2_RMSE_NEWDAT$year <- factor(AR2_RMSE_NEWDAT$year, levels = AR2_RMSE_NEWDAT$year[1:4])
AR2_RMSE_NEWDAT$n <- factor(AR2_RMSE_NEWDAT$n, levels = AR2_RMSE_NEWDAT$n[1:4])

# Create the labels
AR2_RMSE_NEWDAT$phi <- purrr::map_chr(AR2_RMSE_NEWDAT$phi, function(x) {
paste(paste0("varphi[", seq_along(x), "]==", x), collapse = "*textstyle(',')~", sep = "")
})

ggplot(AR2_RMSE_NEWDAT, aes(x = n, y = RMSE, group = Methods)) +
geom_point(aes(shape = Methods)) +
geom_line(aes(linetype = Methods)) +
xlab("Sample Size(n)") +
ylab("RMSE") +
ggplot2::facet_grid(sd ~ phi, scales = "free_y", labeller = label_parsed) +
scale_y_continuous(expand = c(0.0, 0.0)) +
theme_bw(18) +
theme(axis.text.x = element_text(angle = -90, vjust = 0.5)) +
theme(axis.title.x = element_text(size = rel(1.00), angle = 0)) +
theme(axis.title.y = element_text(size = rel(.55), angle = 90)) +
theme(legend.title = element_text(size = 12), legend.text = element_text(size = 10))

Sample Image

UPDATE To get the desired order you have to convert to a factor. To this end I use unique which will give the order as in the dataset:

# Create the labels

AR2_RMSE_NEWDAT$phi <- purrr::map_chr(AR2_RMSE_NEWDAT$phi, function(x) {
paste(paste0("varphi[", seq_along(x), "]==", x), collapse = "*textstyle(',')~", sep = "")
})

AR2_RMSE_NEWDAT$phi <- factor(AR2_RMSE_NEWDAT$phi, levels = unique(AR2_RMSE_NEWDAT$phi))

ggplot(AR2_RMSE_NEWDAT, aes(x = n, y = RMSE, group = Methods)) +
geom_point(aes(shape = Methods)) +
geom_line(aes(linetype = Methods)) +
xlab("Sample Size(n)") +
ylab("RMSE") +
ggplot2::facet_grid(sd ~ phi, scales = "free_y", labeller = label_parsed) +
scale_y_continuous(expand = c(0.0, 0.0)) +
theme_bw(18) +
theme(axis.text.x = element_text(angle = -90, vjust = 0.5)) +
theme(axis.title.x = element_text(size = rel(1.00), angle = 0)) +
theme(axis.title.y = element_text(size = rel(.55), angle = 90)) +
theme(legend.title = element_text(size = 12), legend.text = element_text(size = 10))

Sample Image

Vector of 2 Different Greek Symbols on Ggplot Grid Label

One option would be to first create separate columns for the phi and the theta values. Afterwards create the plotmath labels via e.g. paste0. To get right order of the panels convert the labels column to a factor:

library(ggplot2)
library(purrr)

ARMA11_MAE_NEWDAT$phi1 <- purrr::map_dbl(ARMA11_MAE_NEWDAT$psi, 1)
ARMA11_MAE_NEWDAT$theta1 <- purrr::map_dbl(ARMA11_MAE_NEWDAT$psi, 2)

ARMA11_MAE_NEWDAT$label <- paste0("varphi==", ARMA11_MAE_NEWDAT$phi1, "*textstyle(',')~", "theta==", ARMA11_MAE_NEWDAT$theta1)

ARMA11_MAE_NEWDAT$label <- factor(ARMA11_MAE_NEWDAT$label, levels = unique(ARMA11_MAE_NEWDAT$label))

ggplot(ARMA11_MAE_NEWDAT, aes(x = n, y = MAE, group = Methods)) +
geom_point(aes(shape = Methods)) +
geom_line(aes(linetype = Methods)) +
xlab("Sample Size(n)") +
ylab("RMSE") +
ggplot2::facet_grid(sd ~ label, scales = "free_y", labeller = label_parsed) +
scale_y_continuous(expand = c(0.0, 0.0)) +
theme_bw(18) +
theme(axis.text.x = element_text(angle = -90, vjust = 0.5)) +
theme(axis.title.x = element_text(size = rel(1.00), angle = 0)) +
theme(axis.title.y = element_text(size = rel(.55), angle = 90)) +
theme(legend.title = element_text(size = 12), legend.text = element_text(size = 10))

Sample Image

DATA

library(reshape2)
library(tibble)

set.seed(199)
ARMA11_MAE_MBB_sd1_psi0.8 <- runif(4, min = 0, max = 2)
ARMA11_MAE_MBBR_sd1_psi0.8 <- runif(4, min = 0, max = 2)
ARMA11_MAE_MBB_sd1_psi0.9 <- runif(4, min = 0, max = 2)
ARMA11_MAE_MBBR_sd1_psi0.9 <- runif(4, min = 0, max = 2)
ARMA11_MAE_MBB_sd1_psi0.95 <- runif(4, min = 0, max = 2)
ARMA11_MAE_MBBR_sd1_psi0.95 <- runif(4, min = 0, max = 2)

ARMA11_MAE_MBB_sd3_psi0.8 <- runif(4, min = 2, max = 5)
ARMA11_MAE_MBBR_sd3_psi0.8 <- runif(4, min = 2, max = 5)
ARMA11_MAE_MBB_sd3_psi0.9 <- runif(4, min = 2, max = 5)
ARMA11_MAE_MBBR_sd3_psi0.9 <- runif(4, min = 2, max = 5)
ARMA11_MAE_MBB_sd3_psi0.95 <- runif(4, min = 2, max = 5)
ARMA11_MAE_MBBR_sd3_psi0.95 <- runif(4, min = 2, max = 5)

ARMA11_MAE_MBB_sd5_psi0.8 <- runif(4, min = 5, max = 10)
ARMA11_MAE_MBBR_sd5_psi0.8 <- runif(4, min = 5, max = 10)
ARMA11_MAE_MBB_sd5_psi0.9 <- runif(4, min = 5, max = 10)
ARMA11_MAE_MBBR_sd5_psi0.9 <- runif(4, min = 5, max = 10)
ARMA11_MAE_MBB_sd5_psi0.95 <- runif(4, min = 5, max = 10)
ARMA11_MAE_MBBR_sd5_psi0.95 <- runif(4, min = 5, max = 10)

ARMA11_MAE_MBB_sd10_psi0.8 <- runif(4, min = 10, max = 16)
ARMA11_MAE_MBBR_sd10_psi0.8 <- runif(4, min = 10, max = 16)
ARMA11_MAE_MBB_sd10_psi0.9 <- runif(4, min = 10, max = 16)
ARMA11_MAE_MBBR_sd10_psi0.9 <- runif(4, min = 10, max = 16)
ARMA11_MAE_MBB_sd10_psi0.95 <- runif(4, min = 10, max = 16)
ARMA11_MAE_MBBR_sd10_psi0.95 <- runif(4, min = 10, max = 16)

library(tibble)

ID <- rep(rep(c("10", "15", "20", "25"), 1), 1)
ARMA11_MAE_df1 <- data.frame(ID, ARMA11_MAE_MBB_sd1_psi0.8, ARMA11_MAE_MBBR_sd1_psi0.8, ARMA11_MAE_MBB_sd1_psi0.9, ARMA11_MAE_MBBR_sd1_psi0.9, ARMA11_MAE_MBB_sd1_psi0.95, ARMA11_MAE_MBBR_sd1_psi0.95, ARMA11_MAE_MBB_sd3_psi0.8, ARMA11_MAE_MBBR_sd3_psi0.8, ARMA11_MAE_MBB_sd3_psi0.9, ARMA11_MAE_MBBR_sd3_psi0.9, ARMA11_MAE_MBB_sd3_psi0.95, ARMA11_MAE_MBBR_sd3_psi0.95, ARMA11_MAE_MBB_sd5_psi0.8, ARMA11_MAE_MBBR_sd5_psi0.8, ARMA11_MAE_MBB_sd5_psi0.9, ARMA11_MAE_MBBR_sd5_psi0.9, ARMA11_MAE_MBB_sd5_psi0.95, ARMA11_MAE_MBBR_sd5_psi0.95, ARMA11_MAE_MBB_sd10_psi0.8, ARMA11_MAE_MBBR_sd10_psi0.8, ARMA11_MAE_MBB_sd10_psi0.9, ARMA11_MAE_MBBR_sd10_psi0.9, ARMA11_MAE_MBB_sd10_psi0.95, ARMA11_MAE_MBBR_sd10_psi0.95)

ARMA11_MAE_reshapp1 <- reshape2::melt(ARMA11_MAE_df1, id = "ID")
ARMA11_MAE_reshapp1 <- unique(ARMA11_MAE_reshapp1)

library(ggplot2)
library(tibble)

# Instead of using a character vector make your psi column a list column.

ARMA11_MAE_NEWDAT <- tibble::tibble(
MAE = ARMA11_MAE_reshapp1$value,
year = ARMA11_MAE_reshapp1$ID,
n = rep(rep(c("10", "15", "20", "25"), each = 1), 24),
Methods = rep(rep(c("MBB", "MBBR"), each = 4), 12),
sd = rep(rep(c(1, 3, 5, 10), each = 24), 1),
psi = rep(rep(list(c(0.5, 0.3), c(0.5, 0.4), c(0.35, 0.6)), each = 8), 4)
)

ARMA11_MAE_NEWDAT$sd <- factor(ARMA11_MAE_NEWDAT$sd, levels = ARMA11_MAE_NEWDAT$sd, labels = paste("sd ==", ARMA11_MAE_NEWDAT$sd))
ARMA11_MAE_NEWDAT$year <- factor(ARMA11_MAE_NEWDAT$year, levels = ARMA11_MAE_NEWDAT$year[1:4])
ARMA11_MAE_NEWDAT$n <- factor(ARMA11_MAE_NEWDAT$n, levels = ARMA11_MAE_NEWDAT$n[1:4])

Add greek letters to axis tick labels in R

You can use the expression without having to use paste

axis(4,at=c(0.75,1.75),labels=c(expression(alpha==0.1),expression(alpha==0.2)),las=1)


Related Topics



Leave a reply



Submit