48238

midpoints returned by barplot function do not actually line up with midpoints of bars

I've been noticing that if I save a barplot object:

y <- seq(10,50,10) mp <- barplot(y)

and overlay a line

par(new=T) plot(mp, y, type="o")

then the points on the new plot do not line up with the middle of the bars in the bar plot.

However, if I don't use par(new=T) and instead do this:

mp <- barplot(y) lines(mp, y) points(mp,y)

then the points line up with the middle of the bars.

I want to plot the new line plot on a second axis, so I'd need to do something like par(new=T). Does anyone know how to have the midpoints line up properly?

Thanks

Answer1:

After brief investigation, I realized that the problem is that the x-axis of the two plots don't match up. See the code & plot below to see the problem.

y <- seq(10, 50, 10) mp <- barplot(y) axis(1) #barplot will use left and bottom axes par(new = T) plot(mp, y * 4, type = "o", axes = F) axis(4) axis(3) # this plot will use top and right axes

<img src="https://i.stack.imgur.com/tgsHF.png" alt="enter image description here">

So to fix this problem, you need to specify common xlim parameter for both plots.

mp <- barplot(y, xlim = c(0, length(y) + 1)) par(new = T) plot(mp,y*4,type='o', axes = F,ylab="", xlim=c(0,length(y)+1) ) + axis(4) + mtext("y*4",4)

<img src="https://i.stack.imgur.com/IIW9T.png" alt="enter image description here">

Recommend

  • pseudocolors in R
  • How to use XSL:Key to get the following-sibling in XSLT 2.0
  • In zend, how to print a mysql query properly? [duplicate]
  • Parallel cosine distance using clusterapply in R
  • Remove PNG plot margins
  • Javascript bubble sort of an array with objects
  • Non alpha character arrowlabels on a diagram
  • Plot ROC curve and calculate AUC in R at specific cutoff info
  • several hexbin plots on one PDF page in R
  • Centering a specific element among others with flexbox [duplicate]
  • How to produce a Code 39 that can be reliably read after faxing
  • How to increase the python speed over loops?
  • Error: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
  • Is there a way to read an Excel file using Dataflow
  • Is there a way to pivot a customer ID and a their most recent order dates?
  • Using Python objects in C++
  • Keep same scale in different graphs ggplot2
  • JFreechart X and Y axis scaling
  • Highcharts - Column chart with empty columns for date in x-axis
  • How to draw a line dynamically in android [duplicate]
  • Android Studio Can't Find tools.jar
  • Linq Merge lists
  • How to detect interior vertices in groups of 2d polygons? (E.g. ZIP Codes to determine a territory)
  • Position: fixed nav does not stay fixed
  • Breaking out column by groups in Pandas
  • Debug.DrawLine not showing in the GameView
  • Using $this when not in object context
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Perl system calls when running as another user using sudo
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Return words with double consecutive letters
  • how to add data labels for bar graph in matlab
  • Traverse Array and Display in markup
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Binding checkboxes to object values in AngularJs
  • How to push additional view controllers onto NavigationController but keep the TabBar?