簡體   English   中英

更改R中的多標簽X軸標簽

[英]Change the multi-label X-axis label in R

我有這個代碼

library(dplyr)
library(tidyr)
library(ggplot2)
library(ggprism)

MLE <- c(0.01866, 0.015364, 0.015821, 0.008736, 0.008433,   0.008655,   0.003426,   0.003403,   0.00352)
KS <- c(0.021095,   0.016748,   0.017564,   0.010222,   0.009470,   0.009559,   0.003929,   0.003907,   0.00396)
AD <- c(0.020344,   0.016381,   0.016299,   0.009494,   0.008962,   0.009009,   0.003645,   0.003625,   0.003698)
CS <- c(0.021689,   0.017805,   0.017741,   0.010436,   0.009783,   0.00986,    0.004007,   0.004073,   0.00404)

df <- structure(list(A = c(50, 50, 50, 100, 100, 100, 250, 250, 250),
                     R = c("R1", "R2", "R3", "R1", "R2", "R3", "R1", "R2", "R3"), 
                     MLE = MLE, 
                     KS = KS, 
                     AD = AD, 
                     CS = CS, 
                     `round(2)` = c(2, 2, 2, 2, 2, 2, 2, 2, 2)),
                class = "data.frame", row.names = c(NA, -9L))

df %>% 
  pivot_longer(MLE:CS) %>% 
  mutate(across(where(is.character), as.factor)) %>% 
  ggplot(aes(x = R, y = value, group=name, col=name)) +
  geom_line(linewidth = 1.2) +
  geom_point(size=2.5) +
  xlab("") +
  ylab("Mean Square Errors") +
  scale_color_grey() + 
  theme_classic() +
  facet_wrap(~A, nrow=1, strip.position="bottom")

繪制這樣的圖形初始圖

我想做幾件事:

1- 將矩形中的數字(即 50、100 和 250)分別更改為 m=50、m=100 和 m=250

2- 將 R1、R2 和 R3 全部加粗

關於第一個,我的嘗試是這樣的:

library(dplyr)
library(tidyr)
library(ggplot2)
library(ggprism)

MLE <- c(0.01866, 0.015364, 0.015821, 0.008736, 0.008433,   0.008655,   0.003426,   0.003403,   0.00352)
KS <- c(0.021095,   0.016748,   0.017564,   0.010222,   0.009470,   0.009559,   0.003929,   0.003907,   0.00396)
AD <- c(0.020344,   0.016381,   0.016299,   0.009494,   0.008962,   0.009009,   0.003645,   0.003625,   0.003698)
CS <- c(0.021689,   0.017805,   0.017741,   0.010436,   0.009783,   0.00986,    0.004007,   0.004073,   0.00404)

df <- structure(list(A = c(paste("m", 50, sep = "="), paste("m", 50, sep = "="), paste("m", 50, sep = "="), 
                           paste("m", 100, sep = "="), paste("m", 100, sep = "="), paste("m", 100, sep = "="), 
                           paste("m", 250, sep = "="), paste("m", 250, sep = "="), paste("m", 250, sep = "=")),
                     R = c("R1", "R2", "R3", "R1", "R2", "R3", "R1", "R2", "R3"), 
                     MLE = MLE, 
                     KS = KS, 
                     AD = AD, 
                     CS = CS, 
                     `round(2)` = c(2, 2, 2, 2, 2, 2, 2, 2, 2)),
                class = "data.frame", row.names = c(NA, -9L))

df %>% 
  pivot_longer(MLE:CS) %>% 
  mutate(across(where(is.character), as.factor)) %>% 
  ggplot(aes(x = R, y = value, group=name, col=name)) +
  geom_line(linewidth = 1.2) +
  geom_point(size=2.5) +
  xlab("") +
  ylab("Mean Square Errors") +
  scale_color_grey() + 
  theme_classic() +
  facet_wrap(~A, nrow=1, strip.position="bottom")

結果是錯誤的圖表

問題是即使數據點本身仍然正確,現在順序也亂了,我嘗試重新排列數據框中的值但無濟於事。

關於第二個問題,我什至不知道從何說起。 所以任何關於粗體的幫助將不勝感激

要以正確的順序獲得構面,您需要按所需順序獲得因子變量的水平。 在您的情況下,正確的順序只是unique(A) ,因此mutate(A = factor(A, unique(A)))應該可以解決問題。 對於粗體文本,請使用theme中的axis.text.x元素:

df %>% 
  pivot_longer(MLE:CS) %>% 
  mutate(across(where(is.character), as.factor)) %>%
  mutate(A = factor(A, levels = unique(A))) %>%
  ggplot(aes(x = R, y = value, group=name, col=name)) +
  geom_line(linewidth = 1.2) +
  geom_point(size=2.5) +
  xlab("") +
  ylab("Mean Square Errors") +
  scale_color_grey() + 
  theme_classic() +
  facet_wrap(~A, nrow=1, strip.position="bottom") +
  theme(axis.text.x = element_text(size = 14, face = 'bold'))

在此處輸入圖像描述

第二種選擇是使用自定義labeller功能將前綴添加到條帶文本。 對於軸文本,您可以使用theme選項axis.text.x

prepender <- function(x, prefix = "m=") paste0(prefix, x)

df %>%
  pivot_longer(MLE:CS) %>%
  mutate(across(where(is.character), as.factor)) %>%
  ggplot(aes(x = R, y = value, group = name, col = name)) +
  geom_line(linewidth = 1.2) +
  geom_point(size = 2.5) +
  xlab("") +
  ylab("Mean Square Errors") +
  scale_color_grey() +
  theme_classic() +
  theme(axis.text.x = element_text(face = "bold")) +
  facet_wrap(~A, nrow = 1, strip.position = "bottom", labeller = as_labeller(prepender))

在此處輸入圖像描述

暫無
暫無

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

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