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")
I would like to do several things:
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")
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
CodePudding user response:
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'))
CodePudding user response:
A second option would be to use a custom labeller
function to add the prefix to the strip text. For the axis text you could use theme
option 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))