-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Adding new theme elements to ggplot2 #5521
Comments
I suppose it all depends on how customised you want your plot to be. Here are some thoughts to consider though.
library(ggplot2)
ggplot(mtcars, aes(cyl, disp)) +
geom_point() +
labs(title = "Title") +
theme(
plot.background = ggh4x::element_part_rect(
side = "t", colour = "#fdd306", linewidth = 5
),
plot.title = element_text(margin = margin(t = 1.9, unit = "mm"))
) Here is also a quick sketch of a custom element if you want to draw the line in the title cell only: library(ggplot2)
my_title <- function(..., linecolour = "#fdd306") {
elem <- element_text(...)
elem$linecolour <- linecolour
class(elem) <- c("my_title", class(elem))
elem
}
element_grob.my_title <- function(element, ...) {
grob <- NextMethod()
gp <- grid::gpar(col = element$linecolour, lwd = 5)
height <- grid::convertHeight(grid::grobHeight(grob), "cm")
grid::gTree(
children = grid::gList(
grob,
grid::linesGrob(y = height, gp = gp)
),
width = unit(1, "npc"), height = height,
cl = "absoluteGrob"
)
}
ggplot(mtcars, aes(cyl, disp)) +
geom_point() +
labs(title = "Title") +
theme(
plot.title = my_title(margin = margin(t = 5))
) |
Oh and perhaps I should have said it first, but there is already a mechanism for declaring new theme elements, see |
Ah, those custom elements are super useful. I didn't know you could make them! Thank you for the examples, they are very useful. I also wasn't aware that the plot layout was so crucial for compatibility with other packages. Which I guess goes against the main idea behind this issue, which is to add flexibility to that layout. I guess it's not a very good idea, then. Feel free to close the issue :) |
I'm wondering what's the best way of adding new theme elements to ggplot2 plots.
As motivation, I wanted to make this plot, which includes a yellow line at the top of the plot:
I think this is not possible with vainilla ggplot2 as there is no element in the plot layout for a line in that place. My solution was to subclass the plot and create a new ggplot_gtable method that adds the line to the regular gtable.
Reprex here:
This is a similar to what I did with in my tagger package, which adds a new panel tag element.
Now I'm wondering if this can be generalised. Create plots that have custom theme elements similar to title, subtitle, strip, etc. This would be useful, for example, for adding a "logo" element.
So a few questions. Is this the best way to do this? Could it be possible to extend ggplot2 extendability to also include adding custom elements like this natively?
The text was updated successfully, but these errors were encountered: