-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Base R plot support #2
Comments
Hi Kyle. The tl;dr read is that, yes, facets are on the roadmap for One UI thing I'd like feedback on is potential formula method support and implementation. My current thinking is that we could denote facets after a second vertical bar, e.g. I can't make any promises RE timeline. But my goal is to finish this type of feature support in time for a CRAN submission before 2023 is out. I may go ahead and merge the current PR since that's the current bottleneck for further development rn. |
OTOH if you're in a rush, you may only need plot2 if you are worried about a nice legend (potentially irrelevant here?). Quick mockup below using your dataset. I'm still using plot2 here, but only for the ribbon part since I'm lazy I don't feel like going the manual polygon route. dat = data.table::fread('~/Desktop/kyle.csv')
sdat = split(dat, list(dat$var, dat$sample))
par(mfcol = c(length(unique(dat$var)), length(unique(dat$sample))))
invisible(lapply(
seq_along(sdat),
\(i) {
# plot points
with(
sdat[[i]],
plot(
x = x_resid, y = y_resid,
pch = 16,
xlim = range(dat$x_resid), ylim = range(c(dat$upr, dat$lwr)),
frame.plot = FALSE, axes = FALSE, ann = FALSE
)
)
# add grid
grid()
# facet details for some label calcs
facets = par("mfcol")
n_facets = Reduce(`*`, par("mfcol"))
# x axis
if (i %% facets[1] == 0) {
axis(1, lty = 0)
title(xlab = "Residualized x")
}
# y axis
if (i %in% 1:facets[1]) {
axis(2, lty = 0, las = 1)
title(ylab = "Residualized y")
}
# 1st facet labels
if (i %% facets[1] == 1) {
mtext(sub(".*\\.", "", names(sdat))[i], side = 3)
}
# 2 facet labels
if (i %in% c(n_facets, n_facets-1)) {
mtext(sub("\\..*", "", names(sdat))[i], side = 4, crt = 180)
}
# add best fit ribbon (here, using plot2)
with(
sdat[[i]],
plot2::plot2(
x = x_resid, y = fit, ymin = lwr, ymax = upr,
type = "ribbon", col = "darkgreen",
add = TRUE
)
)
}
)) Created on 2023-10-12 with reprex v2.0.2 |
This is really great Grant! I didn't know about |
Completed by 71eb918 |
Just a HU that library(plot2)
# resids dataset obtained by running debugonce(fwlplot:::plot_resids_base_r)
# and writing to disk
resids = read.csv('~/Desktop/resids.csv')
resids |>
with(plot2(
x = x_resid, y = y_resid,
facet = sample ~ var,
facet.args = list(bg = "grey90"),
pch = 21, fill = adjustcolor(1, alpha = 0.1),
frame = FALSE, grid = TRUE
))
resids |>
with(plot2(
x = x_resid, y = fit, ymin = lwr, ymax = upr,
type = "ribbon", col = "dodgerblue",
facet = sample ~ var,
add = TRUE
)) Created on 2024-01-26 with reprex v2.0.2 (Obvs looks a bit silly here b/c of We could probably port that logic here, or I can flag once plot2 hits CRAN. |
Thanks for the heads up. This is great. Since there's going to be basically 0 more features added on this, I'll port over to plot2's feature and then push to CRAN when you do. FYI, with reprex you can use
yields library(fixest)
library(plot2)
#> Warning: package 'plot2' was built under R version 4.3.2
aq = airquality
aq$Month = factor(month.abb[aq$Month], levels = month.abb[5:9])
mod = lm(Temp ~ 0 + Month / Day, data = aq)
aq = cbind(aq, predict(mod, interval = "confidence")) # Plot the original points
with(
aq,
plot2(
x = Day, y = Temp,
by = Month,
facet = "by", facet.args = list(bg = "grey90"),
palette = "dark2",
grid = TRUE, frame = FALSE, ylim = c(50, 100),
main = "Actual and predicted air temperatures"
)
)
# Add the model predictions to the same plot
with(
aq,
plot2(
x = Day, y = fit,
ymin = lwr, ymax = upr,
by = Month, facet = "by",
type = "ribbon",
palette = "dark2",
add = TRUE
)
) Created on 2024-01-26 with reprex v2.1.0 |
Made the switch! I'll try to keep an eye out when |
Now on CRAN :-) |
Congrats on getting it through to CRAN, Grant! |
Congrats on the new CRAN release. We can close, right? |
@grantmcdermott I'm wondering if I could pick your brain about base R plot support (or more likely, using
plot2
). I've added support forfixest_multi
usage (multiple y variables and/orsplit
/fsplit
samples). Now you can replacefeols
withfwlplot
and it will just work :-)I have the following dataset ready to plot from previous calls (see bottom for example of what the dataset would look like). If there is no
split
/fsplit
, then sample is a character column of empty strings""
.For plotting, if there is both I do
facet_grid(rows = var, cols = sample)
if the estimation issplit
and multiple y variables. If there is only 1 of split or multiple y, then I usefacet_wrap
. Otherwise, what we have.I saw your most recent comment here: grantmcdermott/tinyplot#76, and was wondering if this is something that might be easily possible in
plot2
?The text was updated successfully, but these errors were encountered: