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)
- Unexpected error about center/boundary in `geom_histogram()` when `bins = 1`
- ggplotly stopped working with version 3.5.1 HOT 3
- geom_tile -- Option to Split Diagonal Tiles. HOT 2
- Default labs where geom when the aesthetic is in both the ggplot and geom_* call HOT 1
- `stat_bin()` should have the area (instead of height) represent the count. HOT 3
- `ggplot2::ggpar()` has a namespace conflict with `ggpubr::ggpar()`
- Fallback for `facet_wrap(as.table)`
- Length check for `after_scale()` aesthetics HOT 1
- `geom_rug()` does not print a warning when `na.rm = FALSE`
- Should the diamonds dataset factor variables be unorderd? HOT 3
- coord_radial() and coord_polar() are not connecting the start and end points anymore (v3.5.1) HOT 3
- Suggestion for Improved Error Message for `geom_area()` with Discrete X-Axis
- `geom_area()` provides unclear error message when discrete x-axis is used HOT 1
- labeller example that uses an optional argument
- `scale_*_continuous` fails to render breaks if axis is reversed HOT 4
- When x values contain 0, using coord_trans("sqrt") removes the axis line HOT 3
- {marquee} integration HOT 1
- Encapsulate or export `{wrap/grid}_as_facets_list()`.
- ggplotly does not work when adding scale_fill_binned HOT 1
- Superfluous guidance in "Creating a new theme" section of ggplot2 packages vignette? HOT 2
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.