Drewby
Drewby

Reputation: 43

How can I eliminate extra space in a stripchart in base R?

I have created a stripchart in R using the code below:

oldFaithful <- read.table("http://www.isi-stats.com/isi/data/prelim/OldFaithful1.txt", header = TRUE)
par(bty = "n")              #Turns off plot border
stripchart(oldFaithful,     #Name of the data frame we want to graph
    method = "stack",       #Stack the dots (no overlap)
    pch = 20,               #Use dots instead of squares (plot character)
    at = 0,                 #Aligns dots along axis
    xlim = c(40,100))       #Extends axis to include all data

The plot contains a large amount of extra space or whitespace at the top of the graph, as shown below.

Stripchart of oldFaithful data

Is there a way to eliminate the extra space at the top?

Upvotes: 3

Views: 1434

Answers (1)

DanY
DanY

Reputation: 6073

Short Answer

Add the argument offset=1, as in

stripchart(oldFaithful, offset=1, ...)

Long Answer

You really have to dig into the code of stripchart to figure this one out!

When you set a ylim by calling stripchart(oldFaithful, ylim=c(p,q)) or when you let stripchart use its defaults, it does in fact set the ylim when it creates the empty plotting area.

However, it then has to plot the points on that empty plotting area. When it does so, the y-values for the points at one x-value are specified as (1:n) * offset * csize. Here's the catch, csize is based on ylim[2], so the smaller you make the upper ylim, the smaller is csize, effectively leaving the space at the top of the chart no matter the value of ylim[2].

As a quick aside, notice that you can "mess with" ylim[1]. Try this:

stripchart(oldFaithful, ylim=c(2,10), pch=20, method="stack")

OK, back to solving your problem. There is a second reason that there is space at the top of the plot, and that second reason is offset. By default, offset=1/3 which (like csize) is "shrinking" down the height of the y-values of the points being plotted. You can negate this behavior be setting offset closer or equal to one, as in offset=0.9.

Upvotes: 3

Related Questions