7
$\begingroup$

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)

qq = {{a, .261, 93}, {b, .247, 136}, {c, .311, 70}, {d, .336, 80}, {e, .339, 71}, {f, .337, 60}};
q1 = qq[[All, {1, 2}]]; q2 = qq[[All, {1, 3}]]
qw1 = BarChart[q1, BarSpacing -> {0, 1}, Frame -> {Left, Bottom}, 
  ChartStyle -> Darker[Red], FrameStyle -> Darker[Red], 
   ImagePadding -> 30, ImageSize -> 500, ImageMargins -> 4];
   qw2 = BarChart[q2, BarSpacing -> {0, 2}, Frame -> {Right}, 
   FrameTicks -> All, ChartStyle -> Darker[Blue], 
    FrameStyle -> Darker[Blue], ImageMargins -> 4, ImagePadding -> 30, 
     ImageSize -> 500];
     Overlay[{qw1, qw2}]

plot of the  data

$\endgroup$
0

2 Answers 2

6
$\begingroup$

Update: An alternative approach with a minor modification of the code in OP:

  1. Reverse the columns in q1: i.e. use q1 = qq[[All, {2, 1}]];, and
  2. Use BarSpacing -> {0, 1} in both charts

q1 = qq[[All, {2, 1}]]; q2 = qq[[All, {1, 3}]];
qw1 = BarChart[q1, BarSpacing -> {0, 1}, Frame -> {Left}, 
      ChartStyle -> Darker[Red], FrameStyle -> Darker[Red], 
      ImagePadding -> 30, ImageSize -> 500, ImageMargins -> 4];
qw2 = BarChart[q2, BarSpacing -> {0, 1}, Frame -> {Right}, 
      FrameTicks -> All, ChartStyle -> Darker[Blue], 
      FrameStyle -> Darker[Blue], ImageMargins -> 4, 
   ImagePadding -> 30, 
      ImageSize -> 500];

Overlay[{qw1, qw2}]

enter image description here

Use BarSpacing -> {-.5, 1} in both charts to get:

enter image description here

Original answer:

You can use the function modifiedrange from this answer to rescale the second data set and use Charting`FindTicks to relabel the ticks on the right frame:

ClearAll[modifiedrange]
modifiedrange[{{a_, b_}, {c_, d_}}, z_: 0] /; z <= d := {(a d - (a - b + d) z)/(b - z), d}


{data1, data2} = Transpose[qq[[All, {2, 3}]]] ;
data2b = Rescale[data2, modifiedrange[MinMax /@ {data1, data2}], MinMax @ data1];

tickF = Charting`FindTicks[MinMax@data1, modifiedrange[MinMax /@ {data1, data2}]];

BarChart[Transpose[{data1, data2b}], 
   ChartStyle -> (Darker /@ {Red, Blue}), 
   BarSpacing -> {0, 1},
   ImageSize -> 500,
   Axes -> {True, False},
   Frame -> {Left, Right}, 
   FrameStyle -> { Darker /@ {Red, Blue}, {Automatic, Automatic}}, 
   FrameTicks -> {{Automatic, tickF}, {Automatic, Automatic}}]

enter image description here

Use BarSpacing -> {-.5, 1} to get

enter image description here

$\endgroup$
4
  • $\begingroup$ Thanks It is working. $\endgroup$ Oct 8, 2019 at 1:02
  • $\begingroup$ Can suggest me how to add BarLegend at top of the plot. $\endgroup$ Oct 8, 2019 at 9:08
  • $\begingroup$ @GopalVerma, try Legended[Overlay[{qw1, qw2}], Placed[SwatchLegend[Darker /@ {Red, Blue}, {"label 1", "label 2"}, LegendLayout -> {"Row", 1}], Top]] $\endgroup$
    – kglr
    Oct 8, 2019 at 11:34
  • $\begingroup$ Thank, Its work. $\endgroup$ Oct 8, 2019 at 11:48
2
$\begingroup$

You can use ChartElementFunction to manually nudge the bars over by a bit, the following for example moves them 0.5 to the right:

ChartElementFunction -> (Rectangle @@ Transpose[# + {0.5, 0}] &)

For the combining part, you can search this site for ways to overlay charts cleanly (e.g. this question). One way is to use the CombinePlots function from the ForScience`1 paclet:

<<ForScience`
qq = {{a, .261, 93}, {b, .247, 136}, {c, .311, 70}, {d, .336, 
    80}, {e, .339, 71}, {f, .337, 60}};
q1 = qq[[All, {1, 2}]]; q2 = qq[[All, {1, 3}]]
qw1 = BarChart[
  q1,
  BarSpacing -> {0, 1},
  Frame -> Left,
  ChartStyle -> Darker[Red],
  FrameStyle -> Darker[Red],
  ImagePadding -> 30,
  ImageSize -> 500,
  ImageMargins -> 4
  ]
qw2 = BarChart[
  q2,
  BarSpacing -> {0, 1},
  Frame -> Left,
  ChartStyle -> Darker[Blue],
  FrameStyle -> Darker[Blue],
  ImageMargins -> 4,
  ImagePadding -> 30,
  ImageSize -> 500,
  ChartElementFunction -> (Rectangle @@ Transpose[# + {0.5, 0}] &)
  ]
CombinePlots[
 qw1, qw2,
 "AxesSides" -> "TwoY", (* move the second plot to the right axis *)
 PlotRange -> All, (* ensure that all the bars are shown *)
 Frame -> {Left, Right}
 ]

enter image description here

1You can install the current version using

PacletInstall["https://github.com/MMA-ForScience/ForScience/releases/download/v0.88.45/ForScience-0.88.45.paclet"]
$\endgroup$
1
  • $\begingroup$ @Lank, Thanks, It is working. $\endgroup$ Oct 8, 2019 at 1:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.