Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decrease padding between lines and points in R "both" type plots

Tags:

plot

r

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.

like image 848
Kristian Avatar asked Jul 19 '16 22:07

Kristian


People also ask

How do you plot a line instead of dots in R?

The basic plot command Here, we use type="l" to plot a line rather than symbols, change the color to green, make the line width be 5, specify different labels for the x and y axis, and add a title (with the main argument).

How do I increase plot area in R?

Use par(mai = c(bottom, left, top, right)) before the plot. It will create extra space around the plot area.


2 Answers

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

like image 189
eipi10 Avatar answered Sep 21 '22 00:09

eipi10


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) )
like image 28
IRTFM Avatar answered Sep 18 '22 00:09

IRTFM