0

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.

See the picture below: bar chart image

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.

5
  • may not be what you want, but one approach would be to melt() your data and then use facet_wrap() to plot the two variables in different panes. You can allow scales = "free_y" to "zoom" in on each pane individually. Commented Jan 29, 2019 at 4:30
  • I don't want to take that route. Actually, the problem comes with different scale of data. I can easily plot them side by side if I do not have to bother about scales. But, then one class of bars would be almost invisible. Commented Jan 29, 2019 at 5:42
  • 1
    Possible duplicate of Plot with 2 y axes, one y axis on the left, and another y axis on the right Commented Jan 29, 2019 at 5:59
  • The topic is extensively tackled in the question you linked. Unless otherwise, as you can read from Hadley's answer,the short answer is it can't be done. Try going through all the answers on there. Commented Jan 29, 2019 at 6:00
  • Either it can be done otherwise it is not duplicate. Commented Jan 29, 2019 at 7:37

2 Answers 2

6

The final answer with code is as follows,

A 2-y axis bar plot without rescaling

The code for the above diagram is as follows,

A) Data

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), Tonnes = c(728364, 757467, 780423, 792756, 701685, 720710, 677292, 761649, 668218, 679042, 974355, 1005035, 1123090, 1055070, 1092498, 1100654, 899767, 1018462, 1046096, 1084173, 1158217, 802194, 276773)), row.names = c(NA, -23L), class = "data.frame", .Names = c("Year", "Kcalpd", "Tonnes" )) 

B) Code

scaleFactor <- max(wfd$Thtonnes) / max(wfd$Kcalpd) ggplot(wfd, aes(x=Year, width=.4)) + geom_col(aes(y=Thtonnes), fill="blue", position = position_nudge(x = -.4)) + geom_col(aes(y=Kcalpd * scaleFactor), fill="red") + scale_y_continuous(name="Thtonnes (Rice + Wheat)", sec.axis=sec_axis(~./scaleFactor, name="Kcal per day")) + scale_x_continuous(breaks = seq(1994, 2016, 4)) + 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") ) + labs(title = "Food Security in Venezuela, Cereals Production and Food Gap", x = element_blank()) 

There is no bid deal involved. Just increase the gap between the bars and shift one type by that margin to get them aligned side by side.

Sign up to request clarification or add additional context in comments.

Comments

1

Here's the solution I referenced in my comment:

library(ggplot2) library(data.table) wfd <- 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")) wfd.m <- melt(wfd, id.vars = 1) ggplot(wfd.m, aes(Year, value, fill = variable)) + geom_col(position = "dodge") + scale_fill_manual(values = c("Thtonnes" = "blue", "Kcalpd" = "red")) + facet_wrap(~variable, ncol = 1, scales = "free_y") + theme(legend.position = "none") + ylab("your label here") 

Created on 2019-01-28 by the reprex package (v0.2.1)

1 Comment

Thanks, I am afraid this is not what I am looking for, faceting is not the solution of my choice. I want them to be in one plot.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.