0

I need to plot MeanDailySteps (range 0 to 30000) and MeanDailySleepHours (0-600)on two different Y axis of the same chart with the column data ID (a unique string of discrete Characters which is common to both the Y axis variables) on the X axis. The data is in a dataframe "Steps_Sleep_Data".

I tried using the ggplot and scale_y_continuous as below, unfortunately this doesn't work. I tried other means using par(new = TRUE) which didn't work. I seem to be wrong somewhere.

Please help.

 ggplot(data=Steps_Sleep_Data, aes(x=Id, y=MeanDailySteps, fill=Id,colour="red", label=MeanDailySteps))+
  geom_bar(stat='identity')+
ggplot(data=Steps_Sleep_Data, aes(x=Id, y=MeanDailySleepMinutes, fill=Id, colour="blue", label=MeanDailySleepMinutes)) +
  geom_bar(stat='identity')+

scale_y_continuous(sec.axis = ~.*500)+
  theme(axis.text.y.left = element_text(color = "red"),
        axis.text.y.right = element_text(color = "blue"))
  • Read `?sec_axis`, it tells you everything you need to know. And searching for it in SO ([`[r] [ggplot2] second axis`](https://stackoverflow.com/search?q=%5Br%5D+%5Bggplot2%5D+second+axis)) shows numerous answers that demonstrate its use as well. – r2evans Nov 23 '22 at 13:44
  • 1
    If none of the dupe-links offered help, and you cannot find it elsewhere on SO, then please: [edit] your question to include sample data and code you've attempted using `sec_axis` and why it isn't right, then @ping me and we can sort it out, reopening if it is distinct from the dupes. Good luck! – r2evans Nov 23 '22 at 13:48

0 Answers0