Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

r - How to plot a 2- y axis chart with bars side by side without re-scaling the data

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...