Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Vector of Greek Symbols on Ggplot2 Grid Label

I want to make the symbol to be $\phi_1 = 0.4$ and $\phi_2 = 0.4$ in the first column, $\phi_1 = 0.45$ and $\phi_2 = 0.45$ in the second column and #\phi_1 = 0.35$ and $\theta_1 = 0.6$ in the third column.

library(ggplot2)
library(reshape2)
set.seed(199)
MB_RMSE_sd1_phi0.8 <-  runif(4, min = 0, max = 2)
TMB_RMSE_sd1_phi0.8 <- runif(4, min = 0, max = 2)
MB_RMSE_sd1_phi0.9 <-  runif(4, min = 0, max = 2)
TMB_RMSE_sd1_phi0.9 <- runif(4, min = 0, max = 2)
MB_RMSE_sd1_phi0.95 <-  runif(4, min = 0, max = 2)
TMB_RMSE_sd1_phi0.95 <- runif(4, min = 0, max = 2)

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

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

MB_RMSE_sd10_phi0.8 <-  runif(4, min = 7, max = 16)
TMB_RMSE_sd10_phi0.8 <- runif(4, min = 7, max = 16)
MB_RMSE_sd10_phi0.9 <-  runif(4, min = 7, max = 16)
TMB_RMSE_sd10_phi0.9 <- runif(4, min = 7, max = 16)
MB_RMSE_sd10_phi0.95 <-  runif(4, min = 7, max = 16)
TMB_RMSE_sd10_phi0.95 <- runif(4, min = 7, max = 16)

ID <- rep(rep(c("10", "15", "20", "25"), 1), 1)

AR2_RMSE_df1 <- data.frame(ID, MB_RMSE_sd1_phi0.8, TMB_RMSE_sd1_phi0.8, MB_RMSE_sd1_phi0.9, TMB_RMSE_sd1_phi0.9, MB_RMSE_sd1_phi0.95, TMB_RMSE_sd1_phi0.95, MB_RMSE_sd3_phi0.8, TMB_RMSE_sd3_phi0.8, MB_RMSE_sd3_phi0.9, TMB_RMSE_sd3_phi0.9, MB_RMSE_sd3_phi0.95, TMB_RMSE_sd3_phi0.95, MB_RMSE_sd5_phi0.8, TMB_RMSE_sd5_phi0.8, MB_RMSE_sd5_phi0.9, TMB_RMSE_sd5_phi0.9, MB_RMSE_sd5_phi0.95, TMB_RMSE_sd5_phi0.95, MB_RMSE_sd10_phi0.8, TMB_RMSE_sd10_phi0.8, MB_RMSE_sd10_phi0.9, TMB_RMSE_sd10_phi0.9, MB_RMSE_sd10_phi0.95, TMB_RMSE_sd10_phi0.95)

AR2_RMSE_reshapp1 <- reshape2::melt(AR2_RMSE_df1, id = "ID")
AR2_RMSE_reshapp1 <- unique(AR2_RMSE_reshapp1)

AR2_RMSE_NEWDAT <- data.frame(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(c("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$phi <- with(AR2_RMSE_NEWDAT, factor(phi, levels = phi, labels = paste("varphi ==", phi)))
AR2_RMSE_NEWDAT$n <- factor(AR2_RMSE_NEWDAT$n, levels = AR2_RMSE_NEWDAT$n[1:4])

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

I have this output to show

What I Want

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I want a grid label that looks like this with my data

>Solution :

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

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading