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

Change the multi-label X-axis label in R

i have this code

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

which draws a graph like this
Initial Graph

I would like to do several things:

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

1- change the numbers in the rectangles (i.e. 50, 100, and 250) to m=50, m=100, and m=250, respectively

2- Make the R1, R2, and R3 bolded for all of them

Regarding the first one my attempt is this:

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

And the result is
Wrong graph

The issue is that the order is messed up now even though the data points themselves are still correct, I tried rearranging the values in the data frame but to no avail.

Regarding the second issue, I don’t even know where to start. So any help regarding the bolding would be appreciated

>Solution :

To get the facets in the correct order, you need to have the levels of the factor variables in the desired order. In your case, the correct order would just be unique(A), so mutate(A = factor(A, unique(A))) should do the trick. For the bold text, use the axis.text.x element in theme:

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

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