Home > database >  How to fix ggtext´s rendering of combining characters like Ě or Ö?
How to fix ggtext´s rendering of combining characters like Ě or Ö?

Time:03-29


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

CodePudding user response:

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))

CodePudding user response:

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.

  • Related