可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答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)
回答2:
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.
回答3:
Thanks NelsonGon, the very idea that it cannot be done was good enough to provoke a solution in me and here is the result.
回答4:
If you go through my previous answer, there is a minor flaw in the positioning of the bars. They are not sitting as centred with the x-axis ticks. I mean Blue bars have been shifted to the right and the Red bars appear centred to the x-axis ticks in previous solutions.
Ideally speaking they both must be centred on x-axis in such a manner that left-side of Red bar must sit with the right-side of the Blue bar on the x-axis. Just like the picture below.