Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Imports:
ggforce (>= 0.3.1),
grid,
igraph (>= 1.0.0),
scales,
scales (>= 1.4.0),
MASS,
ggrepel,
utils,
Expand Down Expand Up @@ -51,7 +51,7 @@ Suggests:
sfnetworks
LinkingTo:
cpp11
RoxygenNote: 7.3.2
RoxygenNote: 7.3.3
Depends:
R (>= 2.10),
ggplot2 (>= 3.5.0)
Expand Down
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ importFrom(ggforce,StatLink2)
importFrom(ggforce,StatVoronoiTile)
importFrom(ggforce,interpolateDataFrame)
importFrom(ggforce,radial_trans)
importFrom(ggplot2,aes)
importFrom(ggplot2,from_theme)
importFrom(ggplot2,update_geom_defaults)
importFrom(ggrepel,GeomLabelRepel)
importFrom(ggrepel,GeomTextRepel)
importFrom(graphlayouts,layout_as_backbone)
Expand Down Expand Up @@ -396,11 +399,13 @@ importFrom(rlang,.data)
importFrom(rlang,as_quosure)
importFrom(rlang,enquo)
importFrom(rlang,eval_tidy)
importFrom(rlang,on_load)
importFrom(rlang,quo_is_null)
importFrom(rlang,quo_is_symbol)
importFrom(rlang,quo_text)
importFrom(rlang,quos)
importFrom(rlang,sym)
importFrom(scales,col_mix)
importFrom(scales,identity_pal)
importFrom(scales,muted)
importFrom(scales,rescale_pal)
Expand Down
105 changes: 63 additions & 42 deletions R/theme_graph.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#' @param border Logical. Should border be drawn if a foreground colour is
#' provided?
#'
#' @param ink,paper The colour for foreground and background respectively,
#' permeating to layer defaults when possible.
#'
#' @param family,base_family,title_family,subtitle_family,strip_text_family,caption_family The font to use for the different elements
#'
#' @param base_size,size,text_size,title_size,subtitle_size,strip_text_size,caption_size The size to use for the various text elements. `text_size` will be used as geom defaults
Expand All @@ -51,12 +54,14 @@
theme_graph <- function(
base_family = 'Arial Narrow',
base_size = 11,
background = 'white',
ink = "black",
paper = "white",
background = paper,
foreground = NULL,
border = TRUE,
text_colour = 'black',
bg_text_colour = text_colour,
fg_text_colour = text_colour,
text_colour = ink,
bg_text_colour = ink,
fg_text_colour = ink,
title_family = base_family,
title_size = 18,
title_face = 'bold',
Expand All @@ -78,7 +83,17 @@ theme_graph <- function(
caption_colour = bg_text_colour,
plot_margin = margin(30, 30, 30, 30)
) {
style <- theme_bw(base_size = base_size, base_family = base_family)
if (all(c("ink", "paper") %in% names(formals(theme_bw)))) {
style <- theme_bw(
base_size = base_size,
base_family = base_family,
ink = ink,
paper = paper
)
} else {
style <- theme_bw(base_size = base_size, base_family = base_family)
}

style <- style +
theme(
text = element_text(colour = text_colour),
Expand Down Expand Up @@ -213,72 +228,78 @@ th_no_axes <- function() {
#'
#' @param ... Parameters passed on the `theme_graph`
#'
#' @importFrom ggplot2 aes from_theme
#' @export
set_graph_style <- function(
family = 'Arial Narrow',
face = 'plain',
size = 11,
text_size = 11,
text_colour = 'black',
ink = "black",
text_colour = ink,
...
) {
style <- theme_graph(
base_family = family,
base_size = size,
text_colour = text_colour,
ink = ink,
...
)
theme_set(style)
text_size <- text_size / .pt
fontsize <- text_size / .pt

update_geom_defaults(
GeomEdgePath,
list(
family = family,
fontface = face,
label_size = text_size
)
)
update_geom_defaults(
GeomText,
list(
family = family,
fontface = face,
size = text_size
)
)
update_geom_defaults(
GeomTextRepel,
list(
if (exists("element_geom", asNamespace("ggplot2"))) {
# Set family/fontsize globally
style <- style + theme(geom = element_geom(
family = family,
fontface = face,
size = text_size
fontsize = fontsize
))
new_settings <- aes(
family = from_theme(family),
size = from_theme(fontsize),
fontface = {{ face }}
)
)
update_geom_defaults(
GeomLabel,
list(
family = family,
fontface = face,
size = text_size
} else {
new_settings <- aes(
famly = {{ family }},
size = {{ fontsize }},
fontface = {{ face }}
)
)
}

theme_set(style)

update_geom_defaults(
GeomLabelRepel,
list(
family = family,
fontface = face,
size = text_size
GeomEdgePath,
aes(
!!!new_settings[c("family", "fontface")],
label_size = !!new_settings[["size"]]
)
)
update_geom_defaults(GeomText, new_settings)
update_geom_defaults(GeomTextRepel, new_settings)
update_geom_defaults(GeomLabel, new_settings)
update_geom_defaults(GeomLabelRepel, new_settings)
}
#' @rdname theme_graph
#'
#' @export
unset_graph_style <- function() {
# replace by ggplot2#6629 mechanism at some point
style <- theme_gray()
theme_set(style)

if (exists("element_geom", asNamespace("ggplot2"))) {
# From 4.0.0 onward, updating with `NULL` will reset defaults
update_geom_defaults(GeomEdgePath, NULL)
update_geom_defaults(GeomText, NULL)
update_geom_defaults(GeomTextRepel, NULL)
update_geom_defaults(GeomLabel, NULL)
update_geom_defaults(GeomLabelRepel, NULL)
update_geom_defaults(GeomAxisHive, NULL)
return(invisible())
}

update_geom_defaults(
GeomEdgePath,
list(
Expand Down
52 changes: 52 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.onLoad <- function(...) {
rlang::run_on_load()
register_s3_method("gganimate", "layer_type", "GeomEdgePath")

invisible()
Expand Down Expand Up @@ -27,3 +28,54 @@ register_s3_method <- function(pkg, generic, class, fun = NULL) {
}
)
}

#' @importFrom ggplot2 update_geom_defaults aes from_theme
#' @importFrom rlang on_load
#' @importFrom scales col_mix
on_load(
if (exists("element_geom", asNamespace("ggplot2"))) {
edge_line_aes <- aes(
edge_colour = from_theme(colour %||% ink),
edge_width = from_theme(linewidth),
edge_linetype = from_theme(linetype)
)
update_geom_defaults(GeomAxisHive, aes(
!!!edge_line_aes,
label_size = from_theme(fontsize %||% 3.88),
family = from_theme(family %||% "")
))
update_geom_defaults(GeomEdgePath, aes(
!!!edge_line_aes,
label_size = from_theme(fontsize %||% 3.88),
family = from_theme(family %||% "")
))
update_geom_defaults(GeomEdgeSegment, edge_line_aes)
update_geom_defaults(GeomEdgePoint, aes(
edge_shape = from_theme(pointshape),
edge_colour = from_theme(colour %||% ink),
edge_size = from_theme(pointsize ),
edge_fill = from_theme(fill %||% NA),
stroke = from_theme(borderwidth)
))
update_geom_defaults(GeomEdgeTile, aes(
edge_fill = from_theme(fill %||% col_mix(ink, paper, 0.2)),
edge_colour = from_theme(colour %||% NA),
edge_width = from_theme(0.2 * borderwidth),
edge_linetype = from_theme(bordertype)
))
update_geom_defaults(GeomEdgeBezier, edge_line_aes)
update_geom_defaults(GeomEdgeBspline, edge_line_aes)
update_geom_defaults(GeomEdgeDensity, aes(
edge_fill = from_theme(fill %||% col_mix(ink, paper, 0.664))
))
update_geom_defaults(GeomEdgeSf, aes(
edge_colour = from_theme(colour %||% ink),
edge_width = from_theme(borderwidth),
edge_linetype = from_theme(bordertype)
))
update_geom_defaults(GeomNodeTile, aes(
fill = from_theme(fill %||% NA),
colour = from_theme(colour %||% ink)
))
}
)
16 changes: 11 additions & 5 deletions man/theme_graph.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/pathAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ cpp11::writable::data_frame pathAttr(cpp11::integers group, cpp11::doubles alpha
for (i = 1; i < group.size(); ++i) {
if (group[i] == currentGroup) {
if (solid[currentIndex] == TRUE) {
solid[currentIndex] = lty[i] == "solid" && lty[i] == lty[i-1];
solid[currentIndex] = ((lty[i] == "solid") || (lty[i] == "1")) && lty[i] == lty[i-1];
}
if (constant[currentIndex] == TRUE) {
constant[currentIndex] =
Expand Down
Loading