在ggplot中为每个面设置不同的Y轴限制

时间:2017-11-04 18:22:06

标签: r ggplot2

enter image description here我需要修改我的代码,为附图中的每个图表设置不同的Y轴限制。 我想从顶部开始的第一个和第二个图表的Y轴限制从-4到-22,第三个图表需要从0到8的Y限制,第四个从0到90,底部图表从0到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) 

1 个答案:

答案 0 :(得分:0)

我决定使用gridExtra库解决我的问题,因为它允许非常高的灵活性。这是代码

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)