inlabru-org / inlabru

inlabru
https://inlabru-org.github.io/inlabru/
91 stars 21 forks source link

Change mesh linewidth when plotting an INLA mesh #188

Closed nickschurch closed 1 year ago

nickschurch commented 1 year ago

It would be nice to be able to change the line width when plotting an inla mesh with the gg function. I think all that's required is to change the gg.inla.mesh function to accept a new 'size' parameter (line 732, ggplot.R) and adding size = size to the geom_segment call on line 777:

msh <- ggplot2::geom_segment(data = df, mapping = mp, color = edge.color, size = size")
finnlindgren commented 1 year ago

Line widths in ggplot are nowadays set with linewidth, and I've now added arguments edge.linewidth, int.linewidth, and ext.linewidth arguments for controlling the different types of edge widths, hopefully with more reasonable defaults than the standard defaults.

finnlindgren commented 1 year ago

The gg.inla.mesh interface could use some general improvements, but to allow backwards compatibility, that will likely be done as part of the coming fmesher package instead.

nickschurch commented 1 year ago

Sounds reasonable. In the mean time, if others come across this and want to be able to to this, this is the custom function I'm using to plot the mesh with a variable line width:

plot.inla.mesh <- function(data, edge.color, edge.width){

  # pull out the graph info into a dataframe and rename the columns
  # this code is straight from inlabrus `gg.inla.mesh` 
  df <- rbind(
        data.frame(a = data$loc[data$graph$tv[, 1], c(1, 2)],
                   b = data$loc[data$graph$tv[, 2], c(1, 2)]),
        data.frame(a = data$loc[data$graph$tv[, 2], c(1, 2)],
                   b = data$loc[data$graph$tv[, 3], c(1, 2)]),
        data.frame(a = data$loc[data$graph$tv[, 1], c(1, 2)],
                   b = data$loc[data$graph$tv[, 3], c(1, 2)])
      )
  colnames(df) <- c("x", "y", "xend", "yend")

  # create the aes for the new ggplot geom
  mp <- ggplot2::aes(x = .data$x, y = .data$y,
                     xend = .data$xend, yend = .data$yend)

  # add the geom_segments with adjustable size parameter
  msh <- ggplot2::geom_segment(data = df, mapping = mp, color = edge.color,
                               size = edge.width)

  # return the mesh geom
  return(msh)
}
finnlindgren commented 1 year ago

The updates I made yesterday already implemented that (see https://github.com/inlabru-org/inlabru/issues/188#issuecomment-1505551321), but with separate control for regular edges and for interior/exterior constraint edges. Please check the latest development version (e.g. install from universe) and let me know if there's still something missing. (You version will also generate a warning with recent ggplot versions, since size should be linewidth since last year)

nickschurch commented 1 year ago

I have inlabru installed via conda, but I'll check it out in a different env if/when I get a mo. I'm sure it works though ;)

finnlindgren commented 1 year ago

Reoponed, since solution not on CRAN yet.

finnlindgren commented 1 year ago

Solved by #193