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.
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:
or with the legend
on the right
side
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:
The legend is displayed well.