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

ggplot second y axis different scale
r plot two y axis
how to make a graph with two y-axis in excel
ggplot2 two y variables
multiple y-axis in r
ggplot secondary axis limits
add secondary axis powerpoint
ggplot 2 second y axis

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.

ggplot with 2 y axes on each side and different scales, It's not possible in ggplot2 because I believe plots with separate y scales (not y-​scales that are See A Study on Dual-Scale Data Charts by Petra Isenberg, Anastasia Bezerianos, discussion on the topic Dual-Scaled Axes in Graphs Are They Ever the Best Solution? (the ranges of both columns differ by about factor 5). Following the below steps, you will find that making two y axes in chart is very easy. 1. Select the data range, and insert a chart first by clicking Insert and selecting a chart you need in the Chart group. 2. Right click a column in the chart, and select Format Data Series in the context menu. See screenshot: 3. Then in Format Data Series dialog, check Secondary Axis in the Plot Series On section, and click the Close button. See screenshot:

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)

How to Add a Second Y-Axis on Excel & Make a Second Series at the, There are many ways to plot two sets of data together in an Excel chart. These series are displayed in parallel on the chart, as side-by-side columns, Your new series may not be measured with the same range of numbers as the old one. plotted on a secondary axis, the chart will automatically try to adjust the scale of​  How to Do a Two-Sided Graph in Excel. Microsoft Excel's graphing capabilities includes a variety of ways to display your data. One is the ability to create a chart with different Y-axes on each side of the chart. This lets you compare two data sets that have different scales. For example, if you were to graph your

Thanks NelsonGon, the very idea that it cannot be done was good enough to provoke a solution in me and here is the result.

GraphPad Prism 7 User Guide, When you plot several kinds of data on one graph, it is often useful to plot some data using a Y-axis on the left side of the graph and other data using a Y-axis on the right All data sets are initially graphed using the left Y-axis. Depending on the scale of the axis, the data points may or may not all be visible. To plot data  I want to plot a bar chart using the given data, I am trying to plot by given code but chart only slightly displaced( I want it to the side by side)

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.

Create a PowerPoint chart/graph with 2 Y-axes and 2 chart types, 2 different Y-axes each with a different scale; 2 different chart like this are very useful when you're comparing 2 very different types of data. On the slide, click the Chart icon, which looks like a column/bar chart. That's not right. on one or both axes to have the same number of divisions on each side. In the right-pane that opens, select the Secondary Axis option. This will add a secondary axis and give you two bars. Right-click on the Profit margin bar and select ‘Change Series Chart Type’. In the Change Chart Type dialog box, change the Profit Margin chart type to ‘Line with Markers’ That’s it!

Statistics with STATA, graph twoway connected y time A basic time plot of y against time. no data-​point markers, use line instead of connected. . graph twoway line y time . graph the right- handy axis, yaxis(2), gives the scale for y2. . graph matrix x1 x2 x3 x4 y a b c Shows the sums of variables a, b, and c as side-by-side bars in a bar chart. How to add secondary axis in Excel Column Chart without overlapping bars? I am trying to make two columns of value show in a Column chart with two bars side-by-side. But whenever I try to move one series of data on secondary axis, the chart automatically overlaps the two bars.

LibreOffice 6.2 Calc Guide, A secondary axis may be helpful when data differ in units or scale, as in Figure 82. In this case, there are significantly more numbers of one data series (kayaks). To plot all three data series on the same chart, the kayak data series is aligned to Show bars side by side When checked, shows no gap in columns or bars in the​  Here are three things that make bar charts a go-to chart type: 1. They’re easy to make. When your data is straightforward, designing and customizing a bar chart is as simple as clicking a few buttons. There aren’t many options, you don’t need to organize your data in a complicated way, and Excel is good at extracting your headings and

255. In an Excel chart, can I have different Y-axis scales (a primary , have two axes. Excel 2013 and Excel 2016 Start by creating a chart with just one axis. Select the data series you wish to place on a secondary . From Series Options category, under Plot Series on, click Secondary Axis radio button and then click Close. Your views are welcome and will help other readers of this page. In the Format Data Series dialog box, enable Series Options in the left bar, check Secondary Axis option, and close the dialog box. See screenshot: 4. Select the left Y axis in the chart, and then click Home > Font Color > White (or other color same to the chart background). See screenshot: Now you have added the Y axis on the right hand in the

Comments
  • 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.
  • 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.
  • Possible duplicate of Plot with 2 y axes, one y axis on the left, and another y axis on the right
  • 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.
  • Either it can be done otherwise it is not duplicate.
  • 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.
  • Can you add your code so others should find this question can see what you did?
  • This question has been tagged duplicate, unfortunately. Once that is removed, I will post the code.
  • Just add it in your answer. Duplicate questions are not removed,no? Or you can add it to the linked question. I can't retract my flag.