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)
- Documentation: relate `width`/`height` to position aesthetics
- MIssing axis when breaks = NULL
- User friendly breaks and labels for scale_*_time HOT 2
- Error bar line ends are not centered and have variable width when using geom_errorbar with position_jitterdodge HOT 1
- When setting "scales" in facet_grid, the horizontal justification of the axis labels shifts among the panels HOT 7
- More informative names for gradient scales
- Should `coord_trans` be renamed to `coord_transform` for API consistency? HOT 2
- `geom_sf_text()` and `geom_sf_label()` forcing the display of x and y axis labels HOT 2
- `stat_bin()` calculates wrong numbers when a `geom_col()` with custom `data` is present HOT 4
- geom_segment and geom_curve discrepancy HOT 1
- Problem of hjust in legend.text in ggplot2 v3.5.0 HOT 2
- Error using `after_stat(ecdf)` in `geom_ribbon()` HOT 1
- random mismatches from `join_keys` HOT 10
- Should all scales expose the `aesthetics` parameter?
- Could support the use of function factories like `scales::pal_hue()` and `scales::pal_viridis()`? HOT 1
- Error when using scales::breaks_extended() in minor_breaks: "the condition has length > 1" HOT 3
- Could the NA default colour be set for a discrete scale where it is wanted to set colours for multiple levels? HOT 2
- Feature request: reassemble the ggplot object after `ggplot_build` HOT 1
- partial match (vp$just --> 'justification') 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.