0

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.

facets output

Daniel James
  • 1,381
  • 1
  • 10
  • 28
  • I simply put `ggplot2::scale_y_continuous(expand = c(0.0, 0.00)) +` in place of `scale_y_continuous(limits=c(-32,32), breaks=seq(-32,32,15), labels=c(32,15,0,15,32)) +` and the problem is solved. – Daniel James Aug 08 '22 at 21:26
  • The above will help in addition to part of the answer of @jackahall which is `facet_grid(sd ~., scales = "free") +` instead of `facet_grid(sd ~ .) +` – Daniel James Aug 09 '22 at 12:32

1 Answers1

1
facet_wrap(sd ~ ., scales = "free")

May be better to use facet_wrap() with ncol = 1

jpsmith
  • 11,023
  • 5
  • 15
  • 36
jackahall
  • 400
  • 1
  • 7
  • I will not want to use `facet_wrap` for it will take up more space when printed on paper thus, I add `facet_grid(sd ~., scales = "free") +` instead of `facet_grid(sd ~.) +`. It works. – Daniel James Aug 09 '22 at 12:31