3

I'm having trouble figuring out how to change the font family of node labels in geom_net. Here's a toy network graph:

library(geomnet)

net <- data.frame(from = sample(LETTERS, 20, replace=TRUE),
                  to = sample(LETTERS, 20, replace=TRUE),
                  weight = round(runif(20,1,5)))

ggplot(data = net, aes(from_id = from, to_id = to)) +
  geom_net(aes(linewidth = weight), layout.alg = "kamadakawai", 
           labelon = TRUE, ecolour = "grey60",
           directed = FALSE, fontsize = 6, ealpha = 0.5,
           repel = TRUE)

There is a "base_family" parameter in theme_net, but that only seems to change the font family of the legend text (and not the node label text):

+ theme_net(base_family="Times")

I've also tried setting it in the theme function (which is how I usually set it in ggplot), but that also only changes the axis/legend text (and not the node label text):

+ theme(text=element_text(family="Times"))
Runic
  • 139
  • 6
  • Possible duplicate of [Changing fonts in ggplot2](https://stackoverflow.com/questions/34522732/changing-fonts-in-ggplot2) – Kevin Arseneau Nov 08 '17 at 01:01
  • @Kevin I understand how to change fonts in ggplot in general. As I mentioned in the question, I'm able to change fonts in the legend/ticks using two different functions (so it isn't an initialization issue like in the question you linked where I don't have the font loaded). This question is specifically about not being able to change the font in the node labels of geom_net even though I'm able to change the font elsewhere in the same ggplot graph. – Runic Nov 08 '17 at 01:44
  • instead of using the base `theme` you must use `geomnet::theme_net` – Kevin Arseneau Nov 08 '17 at 01:54
  • @Kevin As I mentioned in the question, I tried theme_net. That line of code changes the font in the legend, but not the node label. – Runic Nov 08 '17 at 01:59

1 Answers1

4

This is gnarly b/c the pkg author used either gpar() or hard-coded bits internally with the label grob (https://github.com/sctyner/geomnet/blob/master/R/geom-net.r) and didn't have any way to set the family (technically fontfamily). That means the "family" (yay, inconsistency b/c this is how you specify it in a bit) is picked up from the defaults for the graphics device.

Both:

cairo_pdf("test.pdf", family="Roboto Condensed")
ggplot(data = net, aes(from_id = from, to_id = to)) +
  geom_net(aes(linewidth = weight), layout.alg = "kamadakawai", 
           labelon = TRUE, ecolour = "grey60", labelgeom="text",
           directed = FALSE, fontsize = 10, ealpha = 0.5,
           repel = TRUE)
dev.off()

(I have Roboto Condensed loaded b/c of hrbrthemes but I include it there to show it picks up extra installed fonts.)

and:

cairo_pdf("test.pdf", family="Times")
ggplot(data = net, aes(from_id = from, to_id = to)) +
  geom_net(aes(linewidth = weight), layout.alg = "kamadakawai", 
           labelon = TRUE, ecolour = "grey60", labelgeom="text",
           directed = FALSE, fontsize = 10, ealpha = 0.5,
           repel = TRUE)
dev.off()

both ended up with the correct font for the labels. I didn't try other font-oriented theme changes, but it def worked for the labels (though you can see the font change impacted the axis text, but I didn't explicitly set it either so give that a try as well):

enter image description here

enter image description here

Any real, useful fix is going to require a PR for the package which I'd love to hack on tonight but won't be able to get to for a while (though there are scads of other folks who are more capable than I am to make the PR). At the very least you should file an issue referencing this.

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205