Transforming NULL object into grobs

Hi All,

I've been using the Kohonen package for self-organising map (SOM) visualisation.

However, I've noticed that the plots are saved as a NULL object rather than a grobs. Therefore, it makes plotting challenging.

I've tried packages such as ggplotify to rectify this, but with no success. Would anyone know of a way around this in terms of the Kohonen package and in general?

Any help would be greatly be appreciated.

library(kohonen)
data(wines)
som.wines <- som(wines, grid = somgrid(5, 5, "hexagonal"))
colour1 <- tricolor(som.wines$grid)
plot(som.wines, "mapping", bg = rgb(colour1))

######
#SAVING + PLOTTING DOES NOT WORK
#####

PLOT <- plot(som.wines, "mapping", bg = rgb(colour1))`

Try something like this ?

library(kohonen)
data(wines)
som.wines <- som(wines, grid = somgrid(5, 5, "hexagonal"))
colour1 <- tricolor(som.wines$grid)

jpeg(filename = "myplot.jpg")
plot(som.wines, "mapping", bg = rgb(colour1))
dev.off()

Thanks @HanOostdijk.

I appreciate your help. This certainly allows me to save it conveniently, but it doesn't allow me to work with the plot within the environment.

Is there anyway I can save the plot as a grobs within R studio, so that I can continue to work with it, such as adding it to multiplots/grid.arrange etc.

I think it would have to be transformed to a grobs for that, if I am not mistaken?

I think using {ggplotify} this solution should work:

PLOT <- ggplotify::as.grob(~plot(som.wines, "mapping", bg = rgb(colour1)))

Thanks @jms . This works! I was missing the tilde beforehand. Thank you :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.