使用ggplot缩放两个Y轴

时间:2020-09-23 15:24:28

标签: r ggplot2

我是R和ggplot的新手。我被要求构建一个具有四个唯一变量的图形-两列和两行。我可以在一个图上绘制全部四个图,但是我很难缩放y轴。我需要我的主要y表示值介于0-150 mm之间的离散事件。在第二个y轴上,我需要0-1300 mm的连续值。

到目前为止,这是我的代码:

ggplot(data = irrigation2 , aes(x = date)) +
  #Events
  geom_col(data = irrigation2, aes(y= iamt , fill = 'Irrigation')) +
  geom_col(data = irrigation2, aes(y = avg, fill = 'Rainfall')) +
  scale_fill_manual(values = c('blue', 'black'), name = 'Event Type') +
  #Cumulative
  geom_smooth(data = irrigation2, color = 'black' , se = F,
              aes(y = NI_sum/10 , linetype = 'Rainfall+Irrigation')) +
  geom_smooth(data = irrigation2, color = 'grey50' , se = F,
              aes(y = cumu/10 , linetype = 'Target (95th Percentile)')) +
  scale_linetype_manual(values = c('dashed', 'dotted'), name = 'Cumulative Amount') +
  labs(title = 'LTBE Precipitation Record (2020)') + 
  scale_x_continuous( name = 'Date (Julian)' , seq(0 , 365 , by = 10)) +
  scale_y_continuous(name = 'Rainfall Depth (mm)' , breaks = seq(0 , 150, by = 10) , sec.axis =(sec_axis(trans = ~.*.6, name = 'Cumulative Amount (mm)'))) +
  theme(legend.position = "right") +
  theme_bw() 

我也可以提供数据集。

0 个答案:

没有答案