Comments (4)
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.
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.
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.
Thank you again! That solution works for my Quarto output.
from ggplot2.
Related Issues (20)
- `summary()` is verbose about facet
- Rendering book fails at scales-other.qmd HOT 7
- ggplot object gets updated without formal assignment HOT 2
- `position_dodge` not working well with `preserve = "single"` and `geom_text` (or `geom_point`) HOT 1
- `drop = FALSE` does not maintain unused factors HOT 1
- Could polygons be drawn, so that the outline does Not exceed the value it represents? HOT 4
- Feature request: `geom_elbow` HOT 4
- In `facet_grid`, could the `switch` argument be deprecated, and replaced with a `strip.position` argument? HOT 2
- Could `position_dodge` have a `padding` argument? HOT 2
- is it possible to let `ggsave` utilize `ggplot_build` method? HOT 8
- Default label is ignored when a mapping is overwritten by a parameter HOT 1
- scale_color_stepsn() will not accept expression vector for scale labels HOT 3
- Should `alpha` in geoms that have polygons and points only affect the interior of polygons? HOT 4
- Regressions related to #5879 HOT 2
- Regression related to #5807
- suppress "Each group consists of only one observation" HOT 5
- Hebrew and digits don't render ("Failed to get emoji face") HOT 7
- Arrangement of multiple legends inside legend box HOT 3
- Change in scale_color_manual behavior in 3.5.x, adds NA to legend HOT 4
- guide_axis_logticks() does not coordinate with minor axis breaks? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ggplot2.