Giter VIP home page Giter VIP logo

Comments (4)

teunbrand avatar teunbrand commented on June 2, 2024

The rendering of text is controlled by the graphics device.
You can choose a graphics device in ggsave() or in RStudio in the Tools > Global Options > General > Graphics tab.
As an example, the CairoPNG device renders your text horribly:

library(tidyverse)
df_plot <- data.frame(
  stringsAsFactors = FALSE,
  itemnr = c("who5_1", "who5_1", "who5_1", "who5_1", "who5_1"),
  category = c("Ibland",
               "Mindre än halva tiden","Mer än halva tiden","Mestadels","Hela tiden"),
  n = c(4L, 4L, 15L, 42L, 12L),
  Procent = c(5.19, 5.19, 19.48, 54.55, 15.58),
  item = c("Jag har känt mig glad och på gott humör",
           "Jag har känt mig glad och på gott humör","Jag har känt mig glad och på gott humör",
           "Jag har känt mig glad och på gott humör",
           "Jag har känt mig glad och på gott humör")
)

header <- df_plot %>% 
  filter(itemnr == "who5_1") %>% 
  slice(1) %>% 
  pull(item)

plot <- df_plot %>% 
  filter(itemnr == "who5_1") %>% 
  ggplot(aes(x = category, y = Procent, fill = category)) +
  scale_y_continuous(limits = c(0,NA)) +
  scale_x_discrete() + 
  geom_col() +
  geom_text(aes(label = n), color = "darkgrey",
            size = 2.2,
            position = position_dodge(width = 0.9),
            vjust = -0.24) +
  scale_fill_viridis_d(labels = ~ str_wrap(.x, width = 9)) +
  theme(panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank()) +
  labs(title = header)

tmp <- tempfile(fileext = ".png")
ggsave(tmp, plot, device = Cairo::CairoPNG, width = 700, height = 500, limitsize = FALSE, dpi = 100)
knitr::include_graphics(tmp)

While the agg_png device renders the text as intended (I believe, I'm not familiar with Swedish to judge competently).

tmp <- tempfile(fileext = ".png")
ggsave(tmp, plot, device = ragg::agg_png)
#> Saving 7 x 5 in image
knitr::include_graphics(tmp)

Created on 2024-04-12 with reprex v2.1.0

In any case, this is a device issue that ggplot2 cannot solve. The instructions for a plot are sent to {grid} and {grid} interacts with graphics devices, so it is 2 layers of the graphics stack removed from ggplot2's control. For that reason, I'll close this issue.

from ggplot2.

pgmj avatar pgmj commented on June 2, 2024

Ok, thanks, that second one looks good. I render with Quarto, which has worked well previously. But I suppose I need to file this report with them instead. Appreciate the quick and helpful response! I changed the default setting in Rstudio so at least the manually generated figures look good now :)

from ggplot2.

teunbrand avatar teunbrand commented on June 2, 2024

Quarto also doesn't control the rendering of plots directly to the best of my knowledge. I believe knitr chooses the device and you can do something like the following to globally set a device for rendering plots.

```{r, include = FALSE}
knitr::opts_chunk$set(
  dev = "ragg_png"
)
```

from ggplot2.

pgmj avatar pgmj commented on June 2, 2024

Thank you again! That solution works for my Quarto output.

from ggplot2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.