簡體   English   中英

Ggplot2 網格 Label 上的希臘符號向量

[英]Vector of Greek Symbols on Ggplot2 Grid Label

我想讓符號在第一列中為 $\phi_1 = 0.4$ 和 $\phi_2 = 0.4$,在第二列中為 $\phi_1 = 0.45$ 和 $\phi_2 = 0.45$ 和 #\phi_1 = 0.35$第三列中的 $\theta_1 = 0.6$。

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

我有這個輸出 以顯示

我想要的是

我想要一個網格標簽 我的數據看起來像這樣

實現所需結果的一種選擇是將phi向量設置為列表列而不是字符向量。 之后,您可以使用例如purrr::map_chr通過遍歷列表列來創建您的plotmath標簽:

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

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM