1

enter image description hereI need to modify my code to set different Y axis limits for each chart in the picture attached. I would like that the first and second charts from the top have Y axis limits from -4 to -22, the third needs to have Y limits from -4 to 8, the forth from 0 to 90 and the bottom charts from 0 to 1.

myChart <- ggplot() +

  geom_line(data=fileInMeltSub, aes(x=DOY, y=value, color=ROI), size=1.4) +
  geom_point(data=fileInMeltSub, aes(x=DOY, y=value, color=ROI), size=2.2) +

  # facet
  facet_wrap(~ variable, ncol=1, scales = "free_y") +[![enter image description here][1]][1]

  # start X11
  x11(width = 50, height = 50)
  plot(myChart) 
ilFonta
  • 271
  • 3
  • 18
  • See https://stackoverflow.com/questions/18046051/setting-individual-axis-limits-with-facet-wrap-and-scales-free-in-ggplot2. – echasnovski Nov 04 '17 at 21:31
  • 3
    Possible duplicate of [Setting individual axis limits with facet\_wrap and scales = "free" in ggplot2](https://stackoverflow.com/questions/18046051/setting-individual-axis-limits-with-facet-wrap-and-scales-free-in-ggplot2) – Curt F. Nov 05 '17 at 06:15
  • No, I already seen that, but it doesn't help me. I have 5 facets and I have to set 4 different Y axis limits. Maybe the best is to learn how to use grid.arrange – ilFonta Nov 05 '17 at 09:51
  • You need to make a `dummy` data.frame with the same faceting variable(s) as in your `fileInMeltSub`, exactly as described in the question I linked to. – Curt F. Nov 05 '17 at 16:22

2 Answers2

1

Although an old question, for anyone referring back to this, there is a way to custom control the axis limits of each facet using geom_blank:

library(tidyverse)

# Made-up data
df <- tribble(
  ~group, ~x, ~y,
  "a", 1, 1,
  "a", 2, 1.5,
  "a", 3, 2,
  "b", 1, 70,
  "b", 2, 80,
  "b", 3, 90,
  "c", 1, 100,
  "c", 2, 300,
  "c", 3, 500
)

# Custom Y scale using geom_blank
# to control limits for each facet
custom_y <- tribble(
  ~group, ~x, ~y,
  "a", 0, 0,
  "a", 0, 10,
  "b", 0, 0,
  "b", 0, 100,
  "c", 0, 0,
  "c", 0, 500
)

df |> 
  ggplot(aes(x, y)) +
  geom_line() +
  geom_blank(data = custom_y, aes(x, y)) +
  facet_wrap(~ group, scales = "free_y")

Created on 2022-06-05 by the reprex package (v2.0.1)

Carl
  • 4,232
  • 2
  • 12
  • 24
0

I decided to solve my issue using the gridExtra library, because it permits a very high flexibility. Here is the code

dB_HH <- subset(fileInMelt, variable == "mean_HH_dB")
dB_VV <- subset(fileInMelt, variable == "mean_VV_dB")
dB_HH.VV <- subset(fileInMelt, variable == "mean_HH.VV_dB")
dB_alpha <- subset(fileInMelt, variable == "mean_alpha")
dB_entropy <- subset(fileInMelt, variable == "mean_entropy")

# -----------------------------------------------------------------------------------------------------------------------
# charts

myChartHH <- ggplot(data=dB_HH, aes(x=DOY, y=value, color=ROI)) +
  geom_line(size=1.4) +
  geom_point(size=2.2) +
  guides(color=guide_legend(title=NULL)) +
  labs(y = "HH") +
  labs(x = "") +
  scale_y_continuous(limits = c(-24,-4)) +
  theme(legend.position="none")

  # x11(width = 50, height = 50)
  # plot(myChartHH)

  # ---------------------------------------------------------------

myChartVV <- ggplot(data=dB_VV, aes(x=DOY, y=value, color=ROI)) +
  geom_line(size=1.4) +
  geom_point(size=2.2) +
  guides(color=guide_legend(title=NULL)) +
  labs(y = "VV") +
  labs(x = "") +
  scale_y_continuous(limits = c(-24,-4)) +
  theme(legend.position="none")  

  # x11(width = 50, height = 50)
  # plot(myChartVV)

  # ---------------------------------------------------------------

myChartHHVV <- ggplot(data=dB_HH.VV, aes(x=DOY, y=value, color=ROI)) +
  geom_line(size=1.4) +
  geom_point(size=2.2) +
  guides(color=guide_legend(title=NULL)) +
  labs(y = "HH/VV") +
  labs(x = "") +
  scale_y_continuous(limits = c(-4,8)) +
  theme(legend.position="none")

  # x11(width = 50, height = 50)
  # plot(myChartHHVV)

  # ---------------------------------------------------------------

myChartAlpha <- ggplot(data=dB_alpha, aes(x=DOY, y=value, color=ROI)) +
  geom_line(size=1.4) +
  geom_point(size=2.2) +
  guides(color=guide_legend(title=NULL)) +
  labs(y = "alpha") +
  labs(x = "") +
  scale_y_continuous(limits = c(0,90)) +
  theme(legend.position="none")

  # x11(width = 50, height = 50)
  # plot(myChartAlpha)

  # ---------------------------------------------------------------

myChartEntropy <- ggplot(data=dB_entropy, aes(x=DOY, y=value, color=ROI)) +
  geom_line(size=1.4) +
  geom_point(size=2.2) +
  guides(color=guide_legend(title=NULL)) +
  labs(y = "entropy") +
  labs(x = "DOY") +
  scale_y_continuous(limits = c(0,1)) +
  theme(legend.position="none")

  # x11(width = 50, height = 50)
  # plot(myChartEntropy)

  # ---------------------------------------------------------------

totChart <- grid.arrange(myChartHH, 
             myChartVV, 
             myChartHHVV,
             myChartAlpha,
             myChartEntropy,
             ncol=1)

x11(width = 50, height = 50)
plot(totChart)
ilFonta
  • 271
  • 3
  • 18