Ggplot2: Facet Grid With Multiple-Line Plot - r

I want to make a multiple-line plot in `facet_grid like this:
I have the following R code that draws column bar plots in the facet grid
library(ggplot2)
library(reshape2)
set.seed(199)
MB_RMSE_sd1 <- runif(12, min = 0, max = 2)
TMB_RMSE_sd1 <- runif(12, min = 0, max = 2)
MB_RMSE_sd3 <- runif(12, min = 2, max = 5)
TMB_RMSE_sd3 <- runif(12, min = 2, max = 5)
MB_RMSE_sd5 <- runif(12, min = 5, max = 10)
TMB_RMSE_sd5 <- runif(12, min = 5, max = 10)
MB_RMSE_sd10 <- runif(12, min = 7, max = 16)
TMB_RMSE_sd10 <- runif(12, min = 7, max = 16)
ID <- rep(rep(c("N10_AR0.8", "N10_AR0.9", "N10_AR0.95", "N15_AR0.8", "N15_AR0.9", "N15_AR0.95", "N20_AR0.8", "N20_AR0.9", "N20_AR0.95", "N25_AR0.8", "N25_AR0.9", "N25_AR0.95"), 2), 1)
df1 <- data.frame(ID, MB_RMSE_sd1, MB_RMSE_sd3, MB_RMSE_sd5, MB_RMSE_sd10, TMB_RMSE_sd1, TMB_RMSE_sd3, TMB_RMSE_sd5, TMB_RMSE_sd10)
reshapp1 <- reshape2::melt(df1, id = "ID")
NEWDAT <- data.frame(value = reshapp1$value, year = reshapp1$ID, n = rep(rep(c("10", "15", "20", "25"), each = 3), 16), Colour = rep(rep(c("RMSE_MB", "RMSE_TMB"), each = 12), 4), sd = rep(rep(c(1, 3, 5, 10), each = 48), 1), phi = rep(rep(c("0.8", "0.9", "0.95"), 16), 4))
NEWDAT$sd <- with(NEWDAT, factor(sd, levels = sd, labels = paste("sd =", sd)))
NEWDAT$year <- factor(NEWDAT$year, levels = NEWDAT$year[1:12])
NEWDAT$n <- with(NEWDAT, factor(n, levels = n, labels = paste("n = ", n)))
library(ggpattern)
ggplot() +
geom_col_pattern(
data = NEWDAT[NEWDAT$Colour %in% c("RMSE_MB", "RMSE_TMB"), ],
aes(x = phi, y = value, pattern = rev(Colour), pattern_angle = rev(Colour)),
fill = 'white',
colour = 'black',
pattern_density = 0.1,
pattern_fill = 'black',
pattern_colour = 'black'
) +
facet_grid(sd ~ n, scales = "free") +
scale_fill_manual(
breaks = c("RMSE_MB", "RMSE_TMB"),
values = c("red", "blue", "orange", "green")
) +
scale_y_continuous(expand = c(0, 0), label = ~ abs(.)) +
guides(fill = guide_legend(reverse = TRUE)) +
labs(fill = "") +
theme_bw() +
theme(axis.text.x = element_text(angle = -90, vjust = 0.5))
#ggsave("AR1.pdf", height = 8, width = 7, device = "pdf", dpi = 700)
EDIT1
What I have after using the answer below:
library(ggplot2)
library(reshape2)
set.seed(199)
MB_RMSE_sd1 <- runif(12, min = 0, max = 2)
TMB_RMSE_sd1 <- runif(12, min = 0, max = 2)
MB_RMSE_sd3 <- runif(12, min = 2, max = 5)
TMB_RMSE_sd3 <- runif(12, min = 2, max = 5)
MB_RMSE_sd5 <- runif(12, min = 5, max = 10)
TMB_RMSE_sd5 <- runif(12, min = 5, max = 10)
MB_RMSE_sd10 <- runif(12, min = 7, max = 16)
TMB_RMSE_sd10 <- runif(12, min = 7, max = 16)
ID <- rep(rep(c("N10_AR0.8", "N10_AR0.9", "N10_AR0.95", "N15_AR0.8", "N15_AR0.9", "N15_AR0.95", "N20_AR0.8", "N20_AR0.9", "N20_AR0.95", "N25_AR0.8", "N25_AR0.9", "N25_AR0.95"), 1), 1)
df1 <- data.frame(ID, MB_RMSE_sd1, MB_RMSE_sd3, MB_RMSE_sd5, MB_RMSE_sd10, TMB_RMSE_sd1, TMB_RMSE_sd3, TMB_RMSE_sd5, TMB_RMSE_sd10)
reshapp1 <- reshape2::melt(df1, id = "ID")
NEWDAT <- data.frame(value = reshapp1$value, year = reshapp1$ID, n =
rep(rep(c("10", "15", "20", "25"), each = 3), 16), Colour =
rep(rep(c("RMSE_MB", "RMSE_TMB"), each = 3), 16), sd = rep(rep(c(1, 3, 5, 10), each = 24), 1), phi = rep(rep(c("0.8", "0.9", "0.95"), 16), 4))
NEWDAT$sd <- with(NEWDAT, factor(sd, levels = sd, labels = paste("sd =", sd)))
NEWDAT$year <- factor(NEWDAT$year, levels = NEWDAT$year[1:12])
NEWDAT$n <- with(NEWDAT, factor(n, levels = n, labels = paste("n = ", n)))
ggplot(NEWDAT, aes(phi, value)) +
geom_line(aes(linetype = Colour)) + geom_point() +
scale_y_continuous(expand = c(0, 0), label = ~ abs(.)) +
guides(fill = guide_legend(reverse = TRUE)) +
#labs(fill = "") +
facet_grid(sd ~ n, scales = "free") +
theme_bw() +
theme(axis.text.x = element_text(angle = -90, vjust = 0.5))
which produces this
WHAT I WANT

Here is a solution.
After reshaping the data, remove the duplicates with unique. Then the plot is easy, map the colour to linetype and the two lines shall be automatically separated.
library(ggplot2)
library(reshape2)
reshapp1 <- reshape2::melt(df1, id = "ID")
reshapp1 <- unique(reshapp1)
NEWDAT <- data.frame(value = reshapp1$value, year = reshapp1$ID,
n = rep(rep(c("10", "15", "20", "25"), each = 3), 16),
Colour = rep(rep(c("RMSE_MB", "RMSE_TMB"), each = 12), 4),
sd = rep(rep(c(1, 3, 5, 10), each = 48), 1),
phi = rep(rep(c(0.8, 0.9, 0.95), 16), 4))
NEWDAT$sd <- with(NEWDAT, factor(sd, levels = sd, labels = paste("sd =", sd)))
NEWDAT$year <- factor(NEWDAT$year, levels = NEWDAT$year[1:12])
NEWDAT$n <- with(NEWDAT, factor(n, levels = n, labels = paste("n = ", n)))
ggplot(NEWDAT, aes(phi, value)) +
geom_line(aes(linetype = Colour)) +
scale_y_continuous(expand = c(0, 0), label = ~ abs(.)) +
guides(fill = guide_legend(reverse = TRUE)) +
#labs(fill = "") +
facet_grid(sd ~ n, scales = "free") +
theme_bw() +
theme(axis.text.x = element_text(angle = -90, vjust = 0.5))
#ggsave("AR1.pdf", height = 8, width = 7, device = "pdf", dpi = 700)
Created on 2022-09-03 by the reprex package (v2.0.1)

Related

Ggplot2: 2 Horizontal Lines in Each Facet of A Facet-Grid And Not 3 Vatical Lines

I want to join the 3 upper points in each face to be a horizontal line and the 3 lower points to be another horizontal line making 2 lines within a facet and not 3 vatical lines in a facet.
WHAT I HAVE
library(ggplot2)
library(reshape2)
set.seed(199)
MB_RMSE_sd1 <- runif(12, min = 0, max = 2)
TMB_RMSE_sd1 <- runif(12, min = 0, max = 2)
MB_RMSE_sd3 <- runif(12, min = 2, max = 5)
TMB_RMSE_sd3 <- runif(12, min = 2, max = 5)
MB_RMSE_sd5 <- runif(12, min = 5, max = 10)
TMB_RMSE_sd5 <- runif(12, min = 5, max = 10)
MB_RMSE_sd10 <- runif(12, min = 7, max = 16)
TMB_RMSE_sd10 <- runif(12, min = 7, max = 16)
ID <- rep(rep(c("N10_AR0.8", "N10_AR0.9", "N10_AR0.95", "N15_AR0.8", "N15_AR0.9", "N15_AR0.95", "N20_AR0.8", "N20_AR0.9", "N20_AR0.95", "N25_AR0.8", "N25_AR0.9", "N25_AR0.95"), 1), 1)
df1 <- data.frame(ID, MB_RMSE_sd1, MB_RMSE_sd3, MB_RMSE_sd5, MB_RMSE_sd10, TMB_RMSE_sd1, TMB_RMSE_sd3, TMB_RMSE_sd5, TMB_RMSE_sd10)
reshapp1 <- reshape2::melt(df1, id = "ID")
reshapp1 <- unique(reshapp1)
NEWDAT <- data.frame(value = reshapp1$value, year = reshapp1$ID, n = rep(rep(c("10", "15", "20", "25"), each = 3), 8), Colour = rep(rep(c("RMSE_MB", "RMSE_TMB"), each = 3), 16), sd = rep(rep(c(1, 3, 5, 10), each = 24), 1), phi = rep(rep(c("0.8", "0.9", "0.95"), 8), 4))
NEWDAT$sd <- with(NEWDAT, factor(sd, levels = sd, labels = paste("sd =", sd)))
NEWDAT$year <- factor(NEWDAT$year, levels = NEWDAT$year[1:12])
NEWDAT$n <- with(NEWDAT, factor(n, levels = n, labels = paste("n = ", n)))
ggplot(NEWDAT, aes(phi, value)) +
geom_line(aes(linetype = Colour)) + geom_point() +
scale_y_continuous(expand = c(0, 0), label = ~ abs(.)) +
guides(fill = guide_legend(reverse = TRUE)) +
facet_grid(sd ~ n, scales = "free") +
theme_bw() +
theme(axis.text.x = element_text(angle = -90, vjust = 0.5))
WHAT I WANT
NOTE
I am comfortable with the pattern and not colour, I only use colour for my hand sketch.

How to Use Texture Fill in Stacked-Bar Chart for Facet Grid

I have this stacked-bar chart with a facet grid. Instead of the four colours used, I want to use the texture of different kinds. I am changing from coloured to texture to make each bar different if printed on black and white paper.
library(ggplot2)
library(reshape2)
set.seed(199)
MB_RMSE_sd1 <- runif(12, min = 0, max = 2)
TMB_RMSE_sd1 <- runif(12, min = 0, max = 2)
MB_RMSE_sd3 <- runif(12, min = 2, max = 5)
TMB_RMSE_sd3 <- runif(12, min = 2, max = 5)
MB_RMSE_sd5 <- runif(12, min = 5, max = 10)
TMB_RMSE_sd5 <- runif(12, min = 5, max = 10)
MB_RMSE_sd10 <- runif(12, min = 7, max = 16)
TMB_RMSE_sd10 <- runif(12, min = 7, max = 16)
MB_MAE_sd1 <- runif(12, min = 0, max = 2)
TMB_MAE_sd1 <- runif(12, min = 0, max = 2)
MB_MAE_sd3 <- runif(12, min = 2, max = 5)
TMB_MAE_sd3 <- runif(12, min = 2, max = 5)
MB_MAE_sd5 <- runif(12, min = 5, max = 10)
TMB_MAE_sd5 <- runif(12, min = 5, max = 10)
MB_MAE_sd10 <- runif(12, min = 7, max = 16)
TMB_MAE_sd10 <- runif(12, min = 7, max = 16)
ID <- rep(rep(c("N10_AR0.8", "N10_AR0.9", "N10_AR0.95", "N15_AR0.8", "N15_AR0.9", "N15_AR0.95", "N20_AR0.8", "N20_AR0.9", "N20_AR0.95", "N25_AR0.8", "N25_AR0.9", "N25_AR0.95"), 2), 1)
df1 <- data.frame(ID, MB_RMSE_sd1, TMB_MAE_sd1, MB_RMSE_sd3, TMB_MAE_sd3, MB_RMSE_sd5, TMB_MAE_sd5, MB_RMSE_sd10, TMB_MAE_sd10)
reshapp1 <- reshape2::melt(df1, id = "ID")
NEWDAT <- data.frame(value = reshapp1$value, year = reshapp1$ID, n = rep(rep(c("10", "15", "20", "25"), each = 3), 16), Colour = rep(rep(c("RMSE_MB", "RMSE_TMB", "MAE_MB", "MAE_TMB"), each = 12), 4), sd = rep(rep(c(1, 3, 5, 10), each = 48), 1), phi = rep(rep(c("0.8", "0.9", "0.95"), 16), 4))
NEWDAT$sd <- with(NEWDAT, factor(sd, levels = sd, labels = paste("sd =", sd)))
NEWDAT$year <- factor(NEWDAT$year, levels = NEWDAT$year[1:12])
NEWDAT$n <- with(NEWDAT, factor(n, levels = n, labels = paste("n = ", n)))
ggplot() +
geom_bar(data=NEWDAT[NEWDAT$Colour %in% c("RMSE_MB", "RMSE_TMB"),],
aes(x = phi, y=value, fill=rev(Colour)), stat="identity") +
geom_bar(data=NEWDAT[NEWDAT$Colour %in% c("MAE_MB", "MAE_TMB"),],
aes(x=phi, y=-value, fill=Colour), stat="identity") +
geom_hline(yintercept=0, colour="grey40") +
facet_grid(sd ~ n, scales = "free") +
scale_fill_manual(breaks=c("MAE_MB", "MAE_TMB", "RMSE_MB", "RMSE_TMB"),
values=c("red","blue","orange","green")) +
scale_y_continuous(expand = c(0, 0), label = ~abs(.)) +
guides(fill=guide_legend(reverse=TRUE)) +
labs(fill="") + theme_bw() +
theme(axis.text.x=element_text(angle=-90, vjust=0.5))
I have visited Filling bars in barplot with textiles in ggplot2 [duplicate] and How to add texture to fill colours in ggplot2 but because of the complexity of my case I am unable to apply the answers therein.
You may want to try the package {ggpattern} which would be a nice way to print a plot on Black and White Paper.
library(ggplot2)
library(reshape2)
library(ggpattern)
set.seed(199)
MB_RMSE_sd1 <- runif(12, min = 0, max = 2)
TMB_RMSE_sd1 <- runif(12, min = 0, max = 2)
MB_RMSE_sd3 <- runif(12, min = 2, max = 5)
TMB_RMSE_sd3 <- runif(12, min = 2, max = 5)
MB_RMSE_sd5 <- runif(12, min = 5, max = 10)
TMB_RMSE_sd5 <- runif(12, min = 5, max = 10)
MB_RMSE_sd10 <- runif(12, min = 7, max = 16)
TMB_RMSE_sd10 <- runif(12, min = 7, max = 16)
MB_MAE_sd1 <- runif(12, min = 0, max = 2)
TMB_MAE_sd1 <- runif(12, min = 0, max = 2)
MB_MAE_sd3 <- runif(12, min = 2, max = 5)
TMB_MAE_sd3 <- runif(12, min = 2, max = 5)
MB_MAE_sd5 <- runif(12, min = 5, max = 10)
TMB_MAE_sd5 <- runif(12, min = 5, max = 10)
MB_MAE_sd10 <- runif(12, min = 7, max = 16)
TMB_MAE_sd10 <- runif(12, min = 7, max = 16)
ID <- rep(rep(c("N10_AR0.8", "N10_AR0.9", "N10_AR0.95", "N15_AR0.8", "N15_AR0.9", "N15_AR0.95", "N20_AR0.8", "N20_AR0.9", "N20_AR0.95", "N25_AR0.8", "N25_AR0.9", "N25_AR0.95"), 2), 1)
df1 <- data.frame(ID, MB_RMSE_sd1, TMB_MAE_sd1, MB_RMSE_sd3, TMB_MAE_sd3, MB_RMSE_sd5, TMB_MAE_sd5, MB_RMSE_sd10, TMB_MAE_sd10)
reshapp1 <- reshape2::melt(df1, id = "ID")
NEWDAT <- data.frame(value = reshapp1$value, year = reshapp1$ID, n = rep(rep(c("10", "15", "20", "25"), each = 3), 16), Colour = rep(rep(c("RMSE_MB", "RMSE_TMB", "MAE_MB", "MAE_TMB"), each = 12), 4), sd = rep(rep(c(1, 3, 5, 10), each = 48), 1), phi = rep(rep(c("0.8", "0.9", "0.95"), 16), 4))
NEWDAT$sd <- with(NEWDAT, factor(sd, levels = sd, labels = paste("sd =", sd)))
NEWDAT$year <- factor(NEWDAT$year, levels = NEWDAT$year[1:12])
NEWDAT$n <- with(NEWDAT, factor(n, levels = n, labels = paste("n = ", n)))
ggplot() +
geom_col_pattern(
data = NEWDAT[NEWDAT$Colour %in% c("RMSE_MB", "RMSE_TMB"), ],
aes(x = phi, y = value, pattern = rev(Colour), pattern_angle = rev(Colour)),
fill = "white",
colour = "black",
pattern_density = 0.1,
pattern_fill = "black",
pattern_colour = "black"
) +
geom_col_pattern(
data = NEWDAT[NEWDAT$Colour %in% c("MAE_MB", "MAE_TMB"), ],
aes(x = phi, y = -value, pattern = Colour, pattern_angle = Colour),
fill = "white",
colour = "black",
pattern_density = 0.1,
pattern_fill = "black",
pattern_colour = "black"
) +
geom_hline(yintercept = 0, colour = "grey40") +
facet_grid(sd ~ n, scales = "free") +
scale_fill_manual(
breaks = c("MAE_MB", "MAE_TMB", "RMSE_MB", "RMSE_TMB"),
values = c("red", "blue", "orange", "green")
) +
scale_y_continuous(expand = c(0, 0), label = ~ abs(.)) +
guides(fill = guide_legend(reverse = TRUE)) +
labs(fill = "") +
theme_bw() +
theme(axis.text.x = element_text(angle = -90, vjust = 0.5))

I Want the Minus(-) Signs Removed on the Y-Axis And Labels In Place for X-Y-Axis of This Stacked Facet Grid

This question is a follow-up question on How Do I Adjust Scale of Each Facet in Ggplot Faceting? I wanted to make all grids equally visible so I made some adjustments to the scaling. See the picture of what I corrected below:
Now, I have this facet grid which I fashion up to minimize print space. The first two colours (green and orange) on each grid are for RMSE criteria as a comparison of two methods (MB & TMB) while blue and red are for MAE criteria as a comparison of the same set of methods.
library(ggplot2)
library(reshape2)
set.seed(199)
MB_RMSE_sd1 <- runif(12, min = 0, max = 2)
TMB_RMSE_sd1 <- runif(12, min = 0, max = 2)
MB_RMSE_sd3 <- runif(12, min = 2, max = 5)
TMB_RMSE_sd3 <- runif(12, min = 2, max = 5)
MB_RMSE_sd5 <- runif(12, min = 5, max = 10)
TMB_RMSE_sd5 <- runif(12, min = 5, max = 10)
MB_RMSE_sd10 <- runif(12, min = 7, max = 16)
TMB_RMSE_sd10 <- runif(12, min = 7, max = 16)
MB_MAE_sd1 <- runif(12, min = 0, max = 2)
TMB_MAE_sd1 <- runif(12, min = 0, max = 2)
MB_MAE_sd3 <- runif(12, min = 2, max = 5)
TMB_MAE_sd3 <- runif(12, min = 2, max = 5)
MB_MAE_sd5 <- runif(12, min = 5, max = 10)
TMB_MAE_sd5 <- runif(12, min = 5, max = 10)
MB_MAE_sd10 <- runif(12, min = 7, max = 16)
TMB_MAE_sd10 <- runif(12, min = 7, max = 16)
ID <- rep(rep(c("N10_AR0.8", "N10_AR0.9", "N10_AR0.95", "N15_AR0.8", "N15_AR0.9", "N15_AR0.95", "N20_AR0.8", "N20_AR0.9", "N20_AR0.95", "N25_AR0.8", "N25_AR0.9", "N25_AR0.95"), 2), 1)
df1 <- data.frame(ID, MB_RMSE_sd1, TMB_MAE_sd1, MB_RMSE_sd3, TMB_MAE_sd3, MB_RMSE_sd5, TMB_MAE_sd5, MB_RMSE_sd10, TMB_MAE_sd10)
reshapp1 <- reshape2::melt(df1, id = "ID")
NEWDAT <- data.frame(value = reshapp1$value, year = reshapp1$ID, n = rep(rep(c("10", "15", "20", "25"), each = 3), 16), Colour = rep(rep(c("RMSE_MB", "RMSE_TMB", "MAE_MB", "MAE_TMB"), each = 12), 4), sd = rep(rep(c(1, 3, 5, 10), each = 48), 1), phi = rep(rep(c("0.8", "0.9", "0.95"), 16), 4))
NEWDAT$sd <- with(NEWDAT, factor(sd, levels = sd, labels = paste("sd =", sd)))
NEWDAT$year <- factor(NEWDAT$year, levels = NEWDAT$year[1:12])
NEWDAT$n <- with(NEWDAT, factor(n, levels = n, labels = paste("n = ", n)))
ggplot() +
geom_bar(data=NEWDAT[NEWDAT$Colour %in% c("RMSE_MB", "RMSE_TMB"),],
aes(x = phi, y=value, fill=rev(Colour)), stat="identity") +
geom_bar(data=NEWDAT[NEWDAT$Colour %in% c("MAE_MB", "MAE_TMB"),],
aes(x=phi, y=-value, fill=Colour), stat="identity") +
geom_hline(yintercept=0, colour="grey40") +
facet_grid(sd ~ n, scales = "free") +
scale_fill_manual(breaks=c("MAE_MB", "MAE_TMB", "RMSE_MB", "RMSE_TMB"),
values=c("red","blue","orange","green")) +
ggplot2::scale_y_continuous(expand = c(0.0, 0.00)) +
guides(fill=guide_legend(reverse=TRUE)) +
labs(fill="") + theme_bw() +
theme(axis.text.x=element_text(angle=-90, vjust=0.5))
WHAT I WANT
I want the minus (-) to be removed from the scale of the y-axis.
I want phi to be labelled on the x-axis and Value labelled on the Y-axis.
I will not mind if I have my legend in place as it was in the first picture.
NOTE
I will not want the facet_wrap function as it will eat up my print space.
I have taken your code and added scale_y_continuous(expand = c(0, 0), label = ~abs(.)) +
WHAT I WANT
I want the minus (-) to be removed from the scale of the y-axis. CHECK
I want phi to be labelled on the x-axis and Value labelled on the
Y-axis. CHECK
I will not mind if I have my legend in place as it was in the first
picture. CHECK

How Do I Adjust Scale of Each Facet in Ggplot Faceting? [duplicate]

This question already has answers here:
Setting individual axis limits with facet_wrap and scales = "free" in ggplot2
(3 answers)
Closed 7 months ago.
I have the following data in a data frame that grows as the standard deviation sd = (1, 3, 5, 10) increases as shown in the plot below. The global scale set has made the details of the first and second facets not visible as I want.
set.seed(199)
MB_RMSE_sd1 <- runif(12, min = 0, max = 2)
set.seed(991)
TMB_RMSE_sd1 <- runif(12, min = 0, max = 2)
set.seed(199)
MB_RMSE_sd3 <- runif(12, min = 2, max = 5)
set.seed(991)
TMB_RMSE_sd3 <- runif(12, min = 2, max = 5)
set.seed(199)
MB_RMSE_sd5 <- runif(12, min = 5, max = 10)
set.seed(991)
TMB_RMSE_sd5 <- runif(12, min = 5, max = 10)
set.seed(199)
MB_RMSE_sd10 <- runif(12, min = 7, max = 16)
set.seed(991)
TMB_RMSE_sd10 <- runif(12, min = 7, max = 16)
set.seed(199)
MB_MAE_sd1 <- runif(12, min = 0, max = 2)
set.seed(991)
TMB_MAE_sd1 <- runif(12, min = 0, max = 2)
set.seed(199)
MB_MAE_sd3 <- runif(12, min = 2, max = 5)
set.seed(991)
TMB_MAE_sd3 <- runif(12, min = 2, max = 5)
set.seed(199)
MB_MAE_sd5 <- runif(12, min = 5, max = 10)
set.seed(991)
TMB_MAE_sd5 <- runif(12, min = 5, max = 10)
set.seed(199)
MB_MAE_sd10 <- runif(12, min = 7, max = 16)
set.seed(991)
TMB_MAE_sd10 <- runif(12, min = 7, max = 16)
ID <- rep(rep(c("N10_AR0.8", "N10_AR0.9", "N10_AR0.95", "N15_AR0.8", "N15_AR0.9", "N15_AR0.95", "N20_AR0.8", "N20_AR0.9", "N20_AR0.95", "N25_AR0.8", "N25_AR0.9", "N25_AR0.95"), 2), 1)
df1 <- data.frame(ID, MB_RMSE_sd1, TMB_MAE_sd1, MB_RMSE_sd3, TMB_MAE_sd3, MB_RMSE_sd5, TMB_MAE_sd5, MB_RMSE_sd10, TMB_MAE_sd10)
reshapp1 <- reshape2::melt(df1, id = "ID")
NEWDAT <- data.frame(value = reshapp1$value, year = reshapp1$ID, Colour = rep(rep(c("RMSE_MB", "RMSE_TMB", "MAE_MB", "MAE_TMB"), each = 12), 4), sd = rep(rep(c(1, 3, 5, 10), each = 48), 1))
NEWDAT$sd <- with(NEWDAT, factor(sd, level = sd, labels = paste("sd =", sd)))
NEWDAT$year = factor(NEWDAT$year, levels=NEWDAT$year[1:12])
ggplot() +
geom_bar(data=NEWDAT[NEWDAT$Colour %in% c("RMSE_MB", "RMSE_TMB"),],
aes(x=year, y=value, fill=rev(Colour)), stat="identity") +
geom_bar(data=NEWDAT[NEWDAT$Colour %in% c("MAE_MB", "MAE_TMB"),],
aes(x=year, y=-value, fill=Colour), stat="identity") +
geom_hline(yintercept=0, colour="grey40") +
facet_grid(sd ~ .) +
scale_fill_manual(breaks=c("MAE_MB", "MAE_TMB", "RMSE_MB", "RMSE_TMB"),
values=c("red","blue","orange","green")) +
scale_y_continuous(limits=c(-32,32), breaks=seq(-32,32,15),
labels=c(32,15,0,15,32)) +
guides(fill=guide_legend(reverse=TRUE)) +
labs(fill="") + theme_bw() +
theme(axis.text.x=element_text(angle=-90, vjust=0.5))
What I want
I want to set the scale of individual facet and not a global range.
facet_wrap(sd ~ ., scales = "free")
May be better to use facet_wrap() with ncol = 1

ggplot2 Facet-Wrap Using More Than One Variable

I want this ggplot-facet to look like this
set.seed(1)
n10_sd1_arma0.5_0.3 <- arima.sim(n = 10, model = list(ar = c(0.5), ma = c(0.3), order = c(1, 0, 1)), sd = 1)
set.seed(1)
n10_sd1_arma0.5_0.4 <- arima.sim(n = 10, model = list(ar = c(0.5), ma = c(0.4), order = c(1, 0, 1)), sd = 1)
set.seed(1)
n10_sd1_arma0.35_0.6 <- arima.sim(n = 10, model = list(ar = c(0.35), ma = c(0.6), order = c(1, 0, 1)), sd = 1)
set.seed(1)
n10_sd3_arma0.5_0.3 <- arima.sim(n = 10, model = list(ar = c(0.5), ma = c(0.3), order = c(1, 0, 1)), sd = 3)
set.seed(1)
n10_sd3_arma0.5_0.4 <- arima.sim(n = 10, model = list(ar = c(0.5), ma = c(0.4), order = c(1, 0, 1)), sd = 3)
set.seed(1)
n10_sd3_arma0.35_0.6 <- arima.sim(n = 10, model = list(ar = c(0.35), ma = c(0.6), order = c(1, 0, 1)), sd = 3)
set.seed(1)
n10_sd5_arma0.5_0.3 <- arima.sim(n = 10, model = list(ar = c(0.5), ma = c(0.3), order = c(1, 0, 1)), sd = 5)
set.seed(1)
n10_sd5_arma0.5_0.4 <- arima.sim(n = 10, model = list(ar = c(0.5), ma = c(0.4), order = c(1, 0, 1)), sd = 5)
set.seed(1)
n10_sd5_arma0.35_0.6 <- arima.sim(n = 10, model = list(ar = c(0.35), ma = c(0.6), order = c(1, 0, 1)), sd = 5)
set.seed(1)
n10_sd10_arma0.5_0.3 <- arima.sim(n = 10, model = list(ar = c(0.5), ma = c(0.3), order = c(1, 0, 1)), sd = 10)
set.seed(1)
n10_sd10_arma0.5_0.4 <- arima.sim(n = 10, model = list(ar = c(0.5), ma = c(0.4), order = c(1, 0, 1)), sd = 10)
set.seed(1)
n10_sd10_arma0.35_0.6 <- arima.sim(n = 10, model = list(ar = c(0.35), ma = c(0.6), order = c(1, 0, 1)), sd = 10)
xx = 1:10
n10_df <- data.frame(xx = 1:10, x1 = n10_sd1_arma0.5_0.3, x2 = n10_sd1_arma0.5_0.4, x3 = n10_sd1_arma0.35_0.6, x4 = n10_sd3_arma0.5_0.3, x5 = n10_sd3_arma0.5_0.4, x6 = n10_sd3_arma0.35_0.6, x7 = n10_sd5_arma0.5_0.3, x8 = n10_sd5_arma0.5_0.4, x9 = n10_sd5_arma0.35_0.6, x10 = n10_sd10_arma0.5_0.3, x11 = n10_sd10_arma0.5_0.4, x12 = n10_sd10_arma0.35_0.6)
n10_df |>
tidyr::pivot_longer(-xx) |>
dplyr:: mutate(id = as.numeric(gsub("x", "", name))) |>
dplyr::arrange(id, xx) |>
dplyr::select(-id) |>
dplyr::mutate(sd = rep(rep(c(sd = 1, sd = 3, sd = 5, sd = 10), each = 10), each = 3),
psi = rep(rep(list(c(0.5, 0.3), c(0.5, 0.4), c(0.35, 0.6)), each = 10), 4)) |>
dplyr::mutate(sd = factor(sd, levels = sd, labels = paste("sd =", sd)),
psi = factor(psi, levels = psi, labels = gsub("c", "", paste("\U03A8 =", psi)))) |>
ggplot2::ggplot(aes(x = xx, y = value)) +
ggplot2::geom_line() +
ggplot2::geom_point() +
ggplot2::scale_y_continuous(expand = c(0.0, 0.00)) +
ggplot2::labs(x = "Time", y = "Series") +
ggplot2::facet_grid(sd ~ psi, scales = "free_y") +
ggplot2::theme_bw() + ggplot2::theme(strip.text.x = ggplot2::element_text(size = 20, face = "bold"), strip.text.y = ggplot2::element_text(size = 16, face = "bold"), axis.title = ggplot2::element_text(size = 20), axis.title.x = ggplot2::element_text(angle = 0, hjust = 0.5,
vjust = 0.5, size = 20), axis.title.y = ggplot2::element_text(angle = 90, hjust = 0.5, vjust = 0.5, size = 20))
I think my problem lies in how to twist this part rep(rep(list(c(0.5, 0.3), c(0.5, 0.4), c(0.35, 0.6)) to be what I want. Please help!
Edit
n10_df |>
tidyr::pivot_longer(-xx) |>
dplyr:: mutate(id = as.numeric(gsub("x", "", name))) |>
dplyr::arrange(id, xx) |>
dplyr::select(-id) |>
dplyr::mutate(sd = rep(rep(c(sd = 1, sd = 3, sd = 5, sd = 10), each = 10), each = 3),
psi = rep(rep(list(c(0.5, 0.3), c(0.5, 0.4), c(0.35, 0.6)), each = 10), 4)) |>
dplyr::mutate(sd = factor(sd, levels = sd, labels = paste("sd =", sd)),
psi = factor(psi, levels = psi, labels = sapply(psi, function(x) sprintf('\U03C6 = %s, \U1D717 = %s', x[1], x[2])))) |>
ggplot2::ggplot(aes(x = xx, y = value)) +
ggplot2::geom_line() +
ggplot2::geom_point() +
ggplot2::scale_y_continuous(expand = c(0.0, 0.00)) +
ggplot2::labs(x = "Time", y = "Series") +
ggplot2::facet_grid(sd ~ psi, scales = "free_y") +
ggplot2::theme_bw() + ggplot2::theme(strip.text.x = ggplot2::element_text(size = 20, face = "bold"), strip.text.y = ggplot2::element_text(size = 16, face = "bold"), axis.title = ggplot2::element_text(size = 20), axis.title.x = ggplot2::element_text(angle = 0, hjust = 0.5, vjust = 0.5, size = 20), axis.title.y = ggplot2::element_text(angle = 90, hjust = 0.5, vjust = 0.5, size = 20))

Resources