如何在不重新缩放数据比例的情况下并排绘制带有杆的2-y轴图 [英] How to plot a 2- y axis chart with bars side by side without re-scaling the data

查看:39
本文介绍了如何在不重新缩放数据比例的情况下并排绘制带有杆的2-y轴图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据如下:

  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.42、1046.096、1084.173、1158.217、802.194,276.773)),row.names = c(NA,-23L),class ="data.frame",.Names = c("Year","Kcalpd","Thtonnes")) 

而且,我的代码如下:

  scaleFactor<-max(wfd $ Thtonnes)/max(wfd $ Kcalpd)ggplot(wfd,aes(x =年))+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"))+主题(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")) 

此代码是从以下站点改编的:

问题根本不是我和NortonGon所引用链接的重复.不重复的原因如下:1)到处都没有绘制两个不同比例的条形图.大多数情况下,这是通过刻面完成的.2)我已经演示了可以绘制两个不同的条形图,而无需在2个不同的y轴上重新缩放.3)如果删除了重复的标签,那么我也将上传代码.

解决方案

使用代码的最终答案如下,

上图的代码如下,

A)数据

  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),吨= c(728364,757467,780423、792792、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)代码

  scaleFactor<-max(wfd $ Thtonnes)/max(wfd $ Kcalpd)ggplot(wfd,aes(x =年,宽度= .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))+主题(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"))+实验室(标题=委内瑞拉的粮食安全,谷物生产和粮食缺口",x = element_blank()) 

不涉及任何出价交易.只需增加条形之间的间距,然后将一种类型移动该间距以使它们并排对齐即可.

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:

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.

解决方案

The final answer with code is as follows,

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.

这篇关于如何在不重新缩放数据比例的情况下并排绘制带有杆的2-y轴图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆