0

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

  1. I want the minus (-) to be removed from the scale of the y-axis.

  2. I want phi to be labelled on the x-axis and Value labelled on the Y-axis.

  3. 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.

enter image description here

Daniel James
  • 1,381
  • 1
  • 10
  • 28
  • FYI, the example data will work fine with just one set.seed() at the beginning, assuming there isn't anything magical about this *particular* set of random data. – Jon Spring Aug 09 '22 at 16:08
  • I want every alternate samples to be different from each other as they have the same parameters – Daniel James Aug 09 '22 at 16:26
  • Every stack of orange and red is a reverse and not negative of blue and red. Not a negative but positive as well. – Daniel James Aug 09 '22 at 16:34
  • The seed advances after each calculation so reseting the seed is not necessary. Try `set.seed(0); runif(1); runif(1)` – Jon Spring Aug 09 '22 at 17:21
  • Ok, I will change it. – Daniel James Aug 09 '22 at 17:24
  • I think you can now face my question? – Daniel James Aug 09 '22 at 17:34
  • It's not clear to me what you want. The code already produces a plot with `phi` labeling the x axis and `value` on the y axis, and it has a legend. To remove the y axis minuses you could use `scale_y_continuous(expand = c(0, 0), label = ~abs(.))`. That would seem to achieve goals 1, 2, and 3... – Jon Spring Aug 09 '22 at 18:48
  • Not as you think, there is no label for the two axis nor legend for the plot which I am seeking soltion for. I also want the y-axis to be without minus for its scaling. – Daniel James Aug 09 '22 at 19:03

1 Answers1

2

I have taken your code and added scale_y_continuous(expand = c(0, 0), label = ~abs(.)) +

enter image description here

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

Jon Spring
  • 55,165
  • 4
  • 35
  • 53