Search code examples
rggplot2unicode

Export ggplot with half filled circles (unicode) as png format


I've been building a plot with half-filled circles using unicode symbols for the shapes, similar to that used in a previously asked question.

Unlike the solution provided by in this question (credit to @skalyan91 and @eipi10), I need to export the figure from R as a png file rather than a pdf.

# Existing solution for pdf
library(ggplot2)

my_plot = ggplot(mtcars) +
  geom_point(aes(wt, mpg), shape="\u25D6", colour="red", size=3) +
  geom_point(aes(wt, mpg), shape="\u25D7", colour="blue", size=3) +
  theme_bw()

cairo_pdf("my_plot.pdf", family="Arial Unicode MS", 4,4)
my_plot
dev.off()

I have tried a few different options, but these do not work. Any suggestions?

# Unsuccessful attempts to export as png
png("my_plot.png", family = "Symbola")
my_plot
dev.off()

png("my_plot.png", family = "Arial Unicode MS")
my_plot
dev.off()

I am using R version 4.2.2 and ggplot version 3.4.2.


Solution

  • Why not use ggsave?

    ggsave(
      filename = "path/to/export/dir/filename.png", 
      plot = my_plot, 
      device = "png", 
      width = 4, 
      height = 4
    )
    

    EDIT: If the unicode characters are causing problems, I suggest trying the emojifont package.