6

I have tried to plot a series of points in R, and I use type="b" as a plot option. However, there is a lot of padding (white space) between the points and the lines between them, so much that the line disappears entirely between some points. Her is a picture of how it looks:

example of disappearing lines

I have tried to make the points smaller with the cex plot option, but this does not help, as it only changes the size of the points and not where the lines between the points between these starts and ends. I do not know if this makes a difference, but the symbols I am using are pch=1.

I am interested in knowing if it is possible to reduce this padding, and how you do so. I am not interested in using type=o as a plot option instead.

Steven Beaupré
  • 21,343
  • 7
  • 57
  • 77
Kristian
  • 1,667
  • 2
  • 15
  • 20
  • Just out of curiousity, why aren't you interested in `type="o"`. It seems like the most straightforward way, in base graphics, to get what you're looking for. – eipi10 Jul 19 '16 at 23:55
  • @eipi10: I did not know about the `bg` option, and without this it did not look anything near what I wanted. I find `?par` a bit difficult to find help in as there is an infinite number of parameters, and very little space to describe them. I would not be surprised if one of them just said *Mostly harmless*. – Kristian Jul 20 '16 at 00:18
  • Well, at least it's countably infinite. For the point markers, you can do `?pch` and scroll down to see what's available (you can also use [Unicode symbols](http://stackoverflow.com/a/30743128/496488) to get a much larger palette of marker options). I agree about the help files. When I first started using R, I found the help files somewhat mystifying. They slowly became more useful as I developed intuition about how R works. – eipi10 Jul 20 '16 at 00:27

2 Answers2

5

Any particular reason why you don't want to use type="o"? It seems like the easiest way to get the effect you want:

# Fake data
set.seed(10)
dfs = data.frame(x=1:10, y=rnorm(10))

plot(y~x,data=dfs, type="o", pch=21, bg='white')

pch=21 is a circle marker like pch=1, but with both border and fill. We set the fill to white with bg="white" to "cover up" the lines that go through the point markers.

enter image description here

You can also use cex to change the marker size to avoid overlap and make the lines between nearby points visible:

set.seed(10)
dfs = data.frame(x=1:100, y=cumsum(rnorm(100)))

plot(y~x,data=dfs, type="o", pch=21, bg="white", cex=0.6)

enter image description here

eipi10
  • 91,525
  • 24
  • 209
  • 285
3

Using a dataframe named dfs this seems to deliver a mechanism for adjusting the surrounding "white halo" to whatever size of point an halo you want by adjusting the 'cex' values of the white and black points :

plot(y~x,data=dfs, type="l")
  with(dfs, points(x,y, pch=16,col="white",cex=1.4))
  with(dfs, points(x,y,cex=1) )
IRTFM
  • 258,963
  • 21
  • 364
  • 487