I have code like the following:
library(dplyr)
library(ggplot2)
#Calculo unas cuantas medias cualquiera
mean_data <- group_by(df, Duration, Time.elapsed,P_R_coef, R_L_coef) %>%
summarise(mean_A = mean(Number.of.theories.to.be.discovered, na.rm = TRUE), mean_B = mean(Number.of.published.theories.CK, na.rm = TRUE), mean_C = mean(Number.of.refuted.theories, na.rm = TRUE), mean_D = mean(Number.of.true.published.theories, na.rm = TRUE), mean_E = mean(Number.of.false.published.theories, na.rm = TRUE), mean_F = mean(Theta, na.rm = TRUE))
#Hago un gráfico cualquiera
ggplot(mean_data %>% mutate(group = paste(as.numeric(P_R_coef),as.numeric(P_R_coef),Duration, sep="-")),
aes(Time.elapsed)) +
geom_line(aes(y = mean_A, colour = "Nb.T.to.be.discovered")) +
geom_line(aes(y = mean_B, colour = "Nb.T.published.CK")) +
geom_line(aes(y = mean_C, colour = "Nb.T.refuted")) +
geom_line(aes(y = mean_D, colour = "Nb.true.published")) +
geom_line(aes(y = mean_E, colour = "Nb.false.published")) +
geom_line(aes(y = mean_F*200, colour = "Theta")) +
facet_wrap(~group, scales = "free", labeller = label_both)+
theme_classic()+
labs(x = "Time", y = "Mean", color = "Epistemic landscape") +
scale_y_continuous(
"Mean",
sec.axis = sec_axis(~ ./200, name = "Theta")
)
When making the graph, the ordering of the graph that I obtain is incorrect. As seen in the last row of the graph, facet_wrap shows P_R_coef 10 before P_R_coef 2.
I have tried to do a thousand things but I cannot reorder the numerical values in the graph. My goal is that in the last row of the graph, 2 and 3 appear before 10.
If there is any suggestion seeing the code I appreciate it. If it's not obvious, you could upload some minimal data structure.
The problem you have is that the order is not numerical but lexicographical and
2-1-180
it will always be after10-1-160
. Two alternatives occur to me.1. Zero-fill the strings:
Note: I'm assuming the second value is
R_L_coef
even though in your example it's stillP_R_coef
2. Convert the variable to a
factor
and reorder it to your likingUsing criterion 2 and re-interpreting your "batch", I understand that you could solve it this way:
Check
plot_data
what would be the final data to graph. The logic ofgrupo
converting to a factor is to reorder it by the columns that make up the factor label. Then opt to use thesumarize_at()
to keep the column names and thepivot_longer()
to avoid having to define multiplegeom_lines()
.