1

I have what seems a very simple question but can't find the answer. I have created this plot, and would simply like to have the two external points closer to the middle.

#Sample code
    x=1:3
    y=c(-50,-70,-120)
plot(x,y)

enter image description here

I have tried this reducing the space between plotted points in plot( x, y) type=n by setting par(mar=c(5.1,9,4.1,9)) but that only changes the ratio of the plot but doesn't change the relative distances. I have the same exact problem with qplot. Please note that I would like to set my own tick labels with axis().

Herman Toothrot
  • 1,463
  • 3
  • 23
  • 53
  • 3
    Show the code you used to make the plot. Show what you tried with `par()` Show how you would like to set the x-axis labels. Try to make a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Feb 08 '18 at 16:42
  • Quick and dirty: just add some dummy values in your dataset (that will extend your x axis) and set color of those values to white (you can give a vector to col in the plot par) – Al3xEP Feb 08 '18 at 16:53
  • How can your negative y axis reflect your sample code `y=c(50,70,100)`? – Rui Barradas Feb 08 '18 at 16:53
  • @RuiBarradas fixed. – Herman Toothrot Feb 08 '18 at 16:54
  • @AP25, it seems overly complicated, isn't there a more proper way to pick spacing between points? ggplot should have something at least – Herman Toothrot Feb 08 '18 at 16:55
  • Absolutely, but its all i could come up with from my cell phone walking to my metro. I ll give it some thoughts in a bit if nobody has an answer – Al3xEP Feb 08 '18 at 16:56
  • Would something like this do? `plot(x, y, type = "n", xaxt = "n", xlim = c(-1, 5)); points(x, y); axis(1, at = -1:5)`. – Rui Barradas Feb 08 '18 at 16:59
  • isn't that just a matter of the plotting device? In an interactive R session you can just stretch or shrink the width of the plotting device. – Tom Feb 09 '18 at 07:51

1 Answers1

1

You could add some padding on either side of the x-axis points. For example, here's a function that takes care of the padding and provides control over the number of axis breaks:

x=1:3
y=c(-50,-70,-120)

# Function to plot with padding on either side of x-axis points.
# Padding is set with pad parameter equal to a fraction of the range of the x values.
# The ... argument allows you to pass additional arguments to plot, such as
#  xlab, main, ylim, col, etc.
pad_plot = function(x, y, pad=0.4, n=5, ...) {

  # Get range of x values
  xrng = diff(range(x))

  # Plot, but don't include axis, so that we can directly control the axis labels.
  # Otherwise, plot will add axis breaks at 0, 4, and other values outside the 
  #  range of the data.
  plot(x,y, xlim = range(x) + c(-1,1)*pad*xrng, xaxt="n", ...)

  # Add axis breaks and labels
  axis(side=1, at=pretty(x, n))
}

par(mfrow=c(2,2))
pad_plot(x,y)
pad_plot(x,y, n=2, main="This is a title", pch=16, col="red")
pad_plot(x,y,pad=0.2, n=8)
pad_plot(x,y, pad=2)

enter image description here

eipi10
  • 91,525
  • 24
  • 209
  • 285
  • Thank you I appreciate the suggestion. It's not completely satisfying, then I have all this white space. Why is this operation so difficult? – Herman Toothrot Feb 08 '18 at 22:17
  • 1
    If you want the points to be physically close together in the graph, it seems like the only options are lots of white space padding in a plot with a "typical" aspect ratio (as in my example) or a very thin plot with no padding. If there's another option I'm missing, can you draw a picture of what you want the plot to look like and add an image of it to your question? – eipi10 Feb 08 '18 at 22:38