3

I have an issue with my labels using **ggtext**, if I use combining characters like ĚŠČŘŽ or ÖÜÄ, element_markdown() will add additional spaces/render whitespace behind my text. My code looks like this:
library(ggplot2)
library(tidyverse)
library(ggtext)

df_x <- c(runif(6, min = 1, max = 2)) 
df_y <- c(rep("A", 3), rep("B", 3))
df_lab <- c("Čžěčh Přóblém", "Gërmän pröbëm töö", as.character(1:4))

tibble(df_x, df_y, df_lab) %>%
  ggplot(aes(x = df_x, y = df_y, fill = df_lab)) +
  geom_bar(position = "stack", stat = "identity") + 
  theme(legend.text = element_markdown())

I believe this to be a very basic issue, but could not find anything on how to handle this problem.

Picture: Text

CZ_8eer
  • 45
  • 4

2 Answers2

1

When I combine ě and ö, there is no problem in the output. I changed your character from Čžěčh Přóblém to Čžěčh Přöblém which has a combination of ě and ö. The output looks still good. Look at this code and output:

library(ggplot2)
library(tidyverse)
library(ggtext)

df_x <- c(runif(6, min = 1, max = 2)) 
df_y <- c(rep("A", 3), rep("B", 3))
df_lab <- c("Čžěčh Přöblém", "Gërmän pröbëm töö", as.character(1:4))

tibble(df_x, df_y, df_lab) %>%
  ggplot(aes(x = df_x, y = df_y, fill = df_lab)) +
  geom_bar(position = "stack", stat = "identity") + 
  theme(legend.text = element_markdown())

Output:

enter image description here

The legend is displayed well.

Quinten
  • 35,235
  • 5
  • 20
  • 53
1

Also you can change your system locale using the below code.

Sys.setlocale("LC_CTYPE", "Czech")

Sample code:

library(ggplot2)
library(tidyverse)
library(ggthemes)

Sys.setlocale("LC_CTYPE", "Czech")

tibble(df_x, df_y, df_lab) %>%
      ggplot(aes(x = df_x, y = df_y, fill = df_lab)) +
      geom_bar(position = "stack", stat = "identity") + 
      theme_par()+
      labs(x="dfx",y="dfy")+
      theme(axis.text.x = element_text(hjust = 1, face="bold", size=12, color="black"), 
            axis.title.x = element_text(face="bold", size=16, color="black"),
            axis.text.y = element_text(face="bold", size=12, color="black"),
            axis.title.y = element_text(face="bold", size=16, color="black"),
            strip.text = element_text(size=10, face="bold"),
            plot.title = element_text(size=20, face="bold"),
            legend.position = "top",
            legend.title = element_blank(),
            legend.text = element_text(color = "black", size = 16,face="bold"))

Plot:

enter image description here

or with the legend on the right side

enter image description here

Sample data:

df_x <- c(runif(6, min = 1, max = 2)) 
df_y <- c(rep("A", 3), rep("B", 3))
df_lab <- c("Čžěčh Přóblém", "Gërmän pröbëm töö", as.character(1:4))
Rfanatic
  • 2,224
  • 1
  • 5
  • 21
  • Is there a solution that does not require changing locale, which can lead to other problems? This still seems like an issue, unless there is a real reason why it can't render these kinds of letters with extra spaces when set in other locales like the US – Jamie May 14 '22 at 02:30