My data is as follows:
structure(list(Year = 1994:2016, Kcalpd = c(86L, 91L, 98L, 107L,
116L, 126L, 123L, 112L, 103L, 102L, 103L, 92L, 77L, 59L, 43L,
29L, 19L, 14L, 13L, 12L, 12L, 10L, 9L), Thtonnes = c(728.364,
757.467, 780.423, 792.756, 701.685, 720.71, 677.292, 761.649,
668.218, 679.042, 974.355, 1005.035, 1123.09, 1055.07, 1092.498,
1100.654, 899.767, 1018.462, 1046.096, 1084.173, 1158.217, 802.194,
276.773)), row.names = c(NA, -23L), class = "data.frame", .Names = c("Year",
"Kcalpd", "Thtonnes"))
And, my code is as follows:
scaleFactor <- max(wfd$Thtonnes) / max(wfd$Kcalpd)
ggplot(wfd, aes(x=Year)) +
geom_col(aes(y=Thtonnes), fill="blue") +
geom_col(aes(y=Kcalpd * scaleFactor), fill="red") +
scale_y_continuous(name="Thtonnes", sec.axis=sec_axis(~./scaleFactor, name="Kcalpd")) +
theme(
axis.title.y.left=element_text(color="blue"),
axis.text.y.left=element_text(color="blue"),
axis.title.y.right=element_text(color="red"),
axis.text.y.right=element_text(color="red")
)
This code is adapted from site: Plot with 2 y axes, one y axis on the left, and another y axis on the right given by Megatron. This web link provides a good solution for plotting a bar and line in one graph. But, it does not lead me to plot two differently scaled data as bars in one graph. Therefore to my best have reached the stage below as of now. The problem is that my bars are one behind other just as the pic given below. I want them to be drawn side by side for every Year.
The question is not at all the duplicate to the link referred by me and NortonGon as well. The reason why it is not duplicate are as follows: 1) Nowhere the two bar graphs of different scale have been plotted. Most of the time this is done by faceting. 2) I have already demonstrated that two different bars without rescaling on 2 different y axis can be plotted. 3) If the duplicate tag is removed, then I shall upload the code too.