Usually when I use facetwrap and I want to add lines I use: geomhline(aes(yintercept =…
- User
Usually when I use facet_wrap and I want to add lines I use: geom_hline(aes(yintercept = -Inf)) + geom_vline(aes(xintercept = -Inf)) But it's giving this error: Error in FUN(X[[i]], ...) (non_steady_state_model.R#282): subscript out of bounds
Can you suggest a fix or on the other hand what theme() parameters I can use to add the lines to each of the facets?
- Assistant
The error occurs because aes(yintercept = -Inf) tries to map -Inf from your data, but there's no corresponding column. Instead, you should set (not map) the intercept values with yintercept = -Inf outside of aes(). Correct way to add lines to all facets: rCopy codegeom_hline(yintercept = -Inf) + geom_vline(xintercept = -Inf)
This draws lines at the bottom and left edges of every facet.
Alternative: Use theme() to add panel border lines If your goal is to add lines around or between the facets (rather than inside the plot panel), you can use theme(): 1️⃣ Add border around each facet panel: rCopy codetheme( panel.border = element_rect(colour = "black", fill = NA, size = 0.5) )
2️⃣ Add lines between facets (strip background lines): rCopy codetheme( panel.spacing = unit(0.5, "lines"), panel.border = element_rect(colour = "black", fill = NA) )
3️⃣ If you only want bottom/left panel edges (mimicking -Inf lines): rCopy codetheme( panel.border = element_blank(), panel.grid = element_blank(), axis.line = element_line(colour = "black") )
- User
using theme 1 & 2 gives a full rectangle which as you said isn't what I want. 3 doesn't work. I want left/bottom edges
- Assistant
To show only the left and bottom panel edges in every facet (not full rectangle, and not axis lines clipped per facet), you can manually draw infinite lines inside each panel without mapping to data, and using expand_limits() to make sure they appear. Correct solution: rCopy codegeom_hline(yintercept = -Inf) + geom_vline(xintercept = -Inf) + coord_cartesian(clip = "off")
💡 Key points:
Use yintercept = -Inf and xintercept = -Inf outside aes().
Use coord_cartesian(clip = "off") so that the lines are not clipped to panel boundaries (this is often why you don’t see them).
Works with facet_wrap and facet_grid.
Optional touch: Increase line visibility rCopy codegeom_hline(yintercept = -Inf, size = 0.5) + geom_vline(xintercept = -Inf, size = 0.5)
Why theme(axis.line=) didn’t work axis.line only draws lines on the outer axes of the entire plot, not per facet. That’s why it didn’t appear inside each panel.
Final tip If you also want top and right edges, you can add: rCopy codegeom_hline(yintercept = Inf) + geom_vline(xintercept = Inf)
Let me know if you want: 🔹 Only bottom (x=0) or left (y=0) based on actual axis scale 🔹 Partial-length ticks or panel separators 🔹 Lines aligned with zero instead of -Inf Happy to tailor it!
- User
is there no way to acheive this with theme parameters?
- Assistant
theme() cannot draw only left and bottom lines inside every facet panel — and that’s the key limitation. Here’s why: Theme elementWhat it controlsCan it draw per-facet left/bottom edges?axis.lineOuter plot axes only (not inside facets)❌ Nopanel.borderFull rectangle border around each facet⚠️ All 4 sides only (no control per side)panel.gridGridlines (not edges)❌ Nostrip.*Facet label area❌ No So with theme elements, you have only two real options: 1️⃣ Full rectangle around each facet rCopy codetheme(panel.border = element_rect(color = "black", fill = NA))
(But you don’t want this) 2️⃣ Outer bottom and left axes only (not per facet) rCopy codetheme(axis.line = element_line())
(But this only affects the plot edges, not inside facets)
🎯 Conclusion 🚫 There is no theme() option to draw only bottom and left edges INSIDE each facet panel. The only solution is using geom_hline() and geom_vline() with -Inf (or specific values), optionally combined with coord_cartesian(clip = "off"): rCopy codegeom_hline(yintercept = -Inf) + geom_vline(xintercept = -Inf)
💡 Why it works like this theme() controls layout/styling—but cannot draw geometric elements inside the panel. Anything that draws a line inside the plotting area must be done using geom layers, not theme.