1

I have the following table. I need to plot "Area" in primary y-axis as points with "Weeks" in x-axis. For the same x-axis I need to plot "SM9_5" in secondary y-axis. I have my code below but does not plot it correct.

Any idea is appreciated.

Thanks.

YEAR   Week  Area  SM9_5   sum  percent COUNTY         
2002   9-2   250   212.2    250     10.2  125
2002   10-1  300   450.2    550     22.5  125
2002   10-2  100   150.2    650    100.0  125 
2002   9-3   50    212.2    250     10.2   15
2002   10-1  30    450.2    550     22.5   15
2002   10-2  10    150.2    650    100.0   15 
2003   9-2   12    112.2     12     20.2  150
2003   10-1  15    350.2     27     82.5  150
2003   10-2  16    650.2     43    100.0  150

gg <- gg + geom_point(aes(y = Area, colour = "Area"))
gg <- gg + geom_line(aes(y = SM9_1, colour = "Sep_SM_9-1"))
gg <- gg + scale_y_continuous(sec.axis = sec_axis(~., name = "Soil Moisture"))
gg <- gg + scale_colour_manual(values = c("blue","red"))
gg <- gg + facet_wrap(~COUNTY, 2, scales = "fixed")
gg <- gg + labs(y = "Area",
                x = "Weeks",
                colour = "Parameter")
plot(gg)

My plot is shown below.

enter image description here

enter image description here

  • Although people may have hacked ways to do this, in general, ggplot2 tries not to allow this. See this previous answer by [Hadley Wickham](https://stackoverflow.com/a/3101876/4752675) – G5W Nov 08 '17 at 19:25
  • I see ggplot was used to plot secondary axis here. https://rpubs.com/MarkusLoew/226759 – user8848543 Nov 08 '17 at 20:48
  • It seems like your two y variables are relatively on the same scale already, so I'm not sure you need to do the scaling you are doing. Try using `SM9_5` directly in `geom_line` (no dividing) and see how things look. – aosmith Nov 08 '17 at 21:53
  • I did but still the same. – user8848543 Nov 08 '17 at 22:47
  • @user8848543 You also need to not multiply the second axis by 10. Did you change that, as well? – aosmith Nov 09 '17 at 17:08
  • Yes. I did. See the updated code above and the graph below. The red line disappears now. But this not what I want. – user8848543 Nov 09 '17 at 17:44

0 Answers0