Skip to content

Some functions for bbplot!#3

Merged
juniperlsimonis merged 3 commits intodapperstats:mainfrom
mfidino:master
Jun 3, 2022
Merged

Some functions for bbplot!#3
juniperlsimonis merged 3 commits intodapperstats:mainfrom
mfidino:master

Conversation

@mfidino
Copy link
Copy Markdown
Contributor

@mfidino mfidino commented Apr 7, 2022

Hey Juniper!

I kept forgetting to show you the functions I've added to bbplot that I've been finding useful. They mostly are wrapper functions. For example, axis_blank() replaces axis() and axis_text() replaces mxtext(). There are a couple of quality of life improvements with the functions:

For axis_blank():

  1. This has a a new arguement (minor), which defaults to TRUE. This will add minor tick marks evenly spaced between the major tick marks that are specified (half the length of the major tick marks). Likewise, tck has been added as an argument so you can specify this (something we did anyways).
  2. If at = NULL (the default), this function will use axTicks() to generate the axis numbering.

For axis_text():

This is for both numbering the tick marks on an axis and adding the axis title.

  1. When text = NULL and at = NULL, this will use axTicks() to determine what numbers to place (and where to place them) on the given axis.
  2. When specifying an axis title (i.e,. text = ''something" and at = NULL), then the function finds the middle of the axis and plots it there (though you still need to specify line.

The final function, which I spun up an issue for a long while ago, was to add a ribbon for 95% CI. It's a wrapper for the polygon() function with a bunch of improvements. Here is a quick run through of how plotting would work:

data(cars)
# fit model
m1 <- lm(
  dist ~ speed,
  data = cars
)
# make predictions
preds <- predict(
  m1, 
  newdata = data.frame(speed = 10:25),
  interval = "confidence"
)
# base plot
blank(
  xlim = c(10,25),
  ylim = c(10,130),
  bty = "l"
)
axis_blank(1)
axis_text(side = 1, line = 0.5, cex = 1.2)

axis_text("Speed (mpg)", side = 1, line = 2.5, cex = 1.5)
axis_blank(2)
axis_text(side = 2, line = 0.5, cex = 1.2, las = 1)
axis_text("Distance travelled", side = 2, line = 2.5, cex = 1.5)

# add 95% confidence interval
ribbon(
  x=10:25,
  y=preds[,c("lwr","upr")],
  col = "purple",
  alpha = 0.5
)
# add mean prediction
lines(
  x=10:25,
  y = preds[,"fit"],
  lwd =2,
  col = "purple"
)
# add data
points(
  x = cars$speed,
  y = cars$dist,
  pch = 16
)

Which generates this figure:

image

@juniperlsimonis
Copy link
Copy Markdown
Collaborator

oooo awesome! sorry i kept forgetting to cricle back to this!

@juniperlsimonis juniperlsimonis merged commit 97d1731 into dapperstats:main Jun 3, 2022
@mfidino
Copy link
Copy Markdown
Contributor Author

mfidino commented Jun 6, 2022

No worries Juniper! I'm thinking up a decent way to create a multi-plot figure as well (using outside margins). When I get a chance I'll make a PR for that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants