I have added a font to my ggplot2-plot, and it works perfectly when viewed in RStudio's plot viewer. However, when I try to save the plot as a PDF, NO text at all is printed (see code and pictures below):
df <- data.frame(x = c(1:10), y = c(1:10)) # Dummy data
plot <- ggplot(df, aes(x, y)) + # Dummy plot
geom_point() +
labs(title = "Correct font in R, NO fonts at all in pdf :-(") +
theme(text = element_text(family = "latex"))
Then I try to ggsave() the plot with the following code:
ggsave("df_plot.pdf",
plot = plot,
device = "pdf",
dpi = 320)
But I get an error message:
Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x,
x$y, : invalid font type
Below is the plot with the correct fonts (in RStudio) + the plot that is written to my pdf file (with no fonts at all):
Plot with correct font
Plot witn NO text
What am I missing here? I've tried various stuff with the extrafont package, but the pdfs don't print the fonts there either (if something is printed, its just the default fonts).
Actually, ggsave() appears to work fine for me. The error is actually adding the theme(text = element_text(family = "latex")) to the plot.
Adjusting the example a little bit,
df <- data.frame(x = c(1:10), y = c(1:10)) # Dummy data
plot <- ggplot(df, aes(x, y)) + # Dummy plot
geom_point() +
labs(title = "Correct font in R, NO fonts at all in pdf :-(")
ggsave("df_plot.pdf",
plot = plot,
device = "pdf",
dpi = 320)
#Saving 10.7 x 8.01 in image
But,
plot + theme(text = element_text(size=10, family="LM Roman 10"))
produces the error you've found:
Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
polygon edge not found.
This question has already been answered here:
Error in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : Polygon edge not found
Do these suggestions work for you?
you may consider using the extrafont package:
library(tidyverse)
library(extrafont)
fonts()
df <- data.frame(x = c(1:10), y = c(1:10)) # Dummy data
windowsFonts(Calibri = windowsFont("Calibri"))
plot <- ggplot(df, aes(x, y)) + # Dummy plot
geom_point() +
labs(title = "Correct font in R, NO fonts at all in pdf :-(") +
theme(text = element_text(size=15, family= "Tw Cen MT Condensed Extra Bold"))
ggsave("df_plot.pdf",
plot = plot,
device = cairo_pdf,
dpi = 320)
Related
I plotted a graph using the ggraph function and I have a filter to only label the nodes with a column value more than the 90% percentile based on the dataset. I realised the output inside Rstudio is different from Rshiny when I published my code. And the warnings I received in Rstudio (Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database) shows that the filtered labels were plotted but not seen in the plot. Anyone could share why the output is different from both UI?
my codes for the plot is as such:
new_graph1 <- delete_vertices(social_graph(), V(social_graph())[value < quantile (V(social_graph())$value,0.9)])
filter1 <- quantile (V(new_graph1)$value,0.9)
ggraph(new_graph1, layout = "graphopt") +
geom_edge_link0(edge_colour = "#a46cb7", edge_width = 0.05) +
geom_node_point(aes(size = ifelse (V(new_graph1)$value > filter1, 4, 0.001)),color = ifelse (V(new_graph1)$value > filter1, "#cb6a49", "#7aa457")) +
geom_node_label(aes(label = V(new_graph1)$name, filter = V(new_graph1)$value > filter1), repel = TRUE, family="serif") +
theme_graph() +
labs(title = paste0("Top 1% influential participant based on ",input$network)) +
theme(legend.position = "none", plot.title=element_text(size = 10, hjust=0.5, vjust=0.5, face='bold'),
text=element_text(family="serif"))
I am using a few functions from ggpubr and ggpp to draw my plots.
Specifically, i am using the functions: geom_quadrant_lines(), stat_quadrant_lines(), geom_lm, stat_regline_equation in my plot.
However, when i convert my ggplot2 object into a plotly object using ggplotly(), the above dont seem to be working.
Is there any workaround? Adding the geoms to the plotly object also dont seem to work
Example Code and Errors Below:
library(ggplot2)
library(plotly)
library(ggpubr)
library(ggpp)
library(ggformula)
set.seed(1234)
data <- data.frame(x = -5:4 + runif(10,-1,1), y = seq(-15,12,3)-runif(10,-1,1)*2)
head(data)
g <- ggplot(data = data, aes(x=x, y=y)) +geom_point() +
stat_mean(color = "blue") +
geom_quadrant_lines()+
stat_quadrant_counts()+
stat_regline_equation( formula = y~x, aes(label = paste( ..eq.label.., ..adj.rr.label.., sep="~~")),output.type="expression", label.y.npc = 0.8, label.x.npc = 0.0, colour="black", size = 2.5) +
labs(title= "Example Plot") + #change the number of days here
geom_lm(interval = "prediction", level = 0.95)
g
p <- ggplotly(g)
p
p %>% geom_lm(mapping = aes(x=x, y=y), data = data, interval = "prediction", level = 0.95)
The ggplot2 output is
Once converted into plotly object using ggplotly, it becomes
I also get the below warning messages
> p <- ggplotly(g)
Warning messages:
1: In geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
geom_GeomQuadrantLines() has yet to be implemented in plotly.
If you'd like to see this geom implemented,
Please open an issue with your example code at
https://github.com/ropensci/plotly/issues
2: In geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
geom_GeomTextNpc() has yet to be implemented in plotly.
If you'd like to see this geom implemented,
Please open an issue with your example code at
https://github.com/ropensci/plotly/issues
3: In geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
geom_GeomLm() has yet to be implemented in plotly.
If you'd like to see this geom implemented,
Please open an issue with your example code at
https://github.com/ropensci/plotly/issues
I am trying to embed the ≤ sign into the fonts of my pdf plot. The ≤ sign is shown in the plot when viewed within RStudio (p1). However, when I save the plot and embed the fonts the ≤ sign is converted to an = sign.
Using the extrafont package I want to save my plot with the CM Roman font. I have tried alternative methods with the device set as cairo_pdf in ggsave(). This embeds the ≤ sign into the pdf, but the font is no longer CM Roman.
What needs to be done so that the ≤ sign remains in the plot and the font is CM Roman?
library(ggplot2)
library(extrafont)
font_import()
font_install("fontcm")
loadfonts()
df <- data.frame(foo = c(2, 4, 8 , 16),
bar = factor(c(1:3, "4\u2264")))
p1 <- ggplot(df, aes(x = bar, y = foo)) +
geom_bar(stat="identity") +
labs(title = "p1: Equality sign shows in RStudio plot") +
theme(text = element_text(family = "CM Roman", size = 25))
p2 <- ggplot(df, aes(x = bar, y = foo)) +
geom_bar(stat="identity") +
labs(title = "p2: Equality sign not shown in .pdf file") +
theme(text = element_text(family = "CM Roman", size = 25))
print(p1)
[![p1 plot][1]][1] # there should be an image of p1 here...
# Sys.setenv(R_GSCMD = "C:/Program Files/gs/gs9.53.3/bin/gswin64c.exe") #~ run once at start of each R session
ggsave("p2.pdf", p2, width = 15, height = 10, units = "in")
embed_fonts("p2.pdf", outfile="p2.pdf")
[![p2 plot][1]][1] # there should be an image of p2 here...
ggsave losing unicode characters from ggplot+gridExtra
There are several solutions in this post. I think it has to do something with encoding, but I am not 100% sure.
The plot below was generated in R (without any issue), using this code:
library(tidyverse)
library(extrafont)
loadfonts()
x <- rexp(100)
data.frame(info = x) %>%
ggplot() +
geom_histogram(aes(x = info), col = "red", fill = "red", alpha = 0.5) +
theme_minimal() +
theme(text = element_text(family="LM Roman 10"))
As you can see, the font of the plot is set to "LM Roman 10", which I was able to do thanks to this post and it works perfectly within R.
However when I try to place the image in a LaTeX document using RMarkdown, I get this error:
Quitting from lines 10-22 (min_example.Rmd)
Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
invalid font type
Calls: <Anonymous> ... drawDetails -> drawDetails.text -> grid.Call.graphics
Además: There were 50 or more warnings (use warnings() to see the first 50)
Ejecución interrumpida
Here is the code for min_example.Rmd
---
title: "Untitled"
author: "Javier Rojas"
date: "2/9/2020"
output: pdf_document
---
```{r, echo=FALSE}
library(tidyverse)
library(extrafont)
loadfonts()
x <- rexp(100)
data.frame(info = x) %>%
ggplot() +
geom_histogram(aes(x = info), col = "red", fill = "red", alpha = 0.5) +
theme_minimal() +
theme(text = element_text(family="LM Roman 10"))
```
I am using a Mac computer running macOS High Sierra and R 3.6.1
Usually quite easy to solve. The problem should be, that the font is not installed in your computer.
You have to download the .otf file for the font e.g. (https://fonts2u.com/lmroman10-regular.font) and install it on your Operating System.
If you don't know how to do this, just google it (e.g. "install extra font Windows"), there are plenty of tutorials on it online.
-edit-
I was a little to quick - didn't realize the problem just comes from running it in rmarkdown. Try the following:
```{r, fig.showtext=TRUE, echo=FALSE}
library("tidyverse")
library("showtext")
x <- rexp(100)
font_add("LM Roman 10", regular = "lmroman10-regular.otf")
data.frame(info = x) %>%
ggplot() +
geom_histogram(aes(x = info), col = "red", fill = "red", alpha = 0.5) +
theme_minimal() +
theme(text = element_text(family="LM Roman 10"))
```
It's important that you add fig.showtext=TRUE, library("showtext") and font_add("LM Roman 10", regular = "lmroman10-regular.otf").
I just placed the .otf in my project folder - but I think you can also give it another path.
There exists a current new approach using showtext and showtextdb packages.
On Windows, install manually Latin Modern Roman font (.tff version) following these steps easy instructions https://tex.stackexchange.com/questions/55787/latin-modern-roman-for-ttf. After the installation, you can locate all your fonts in "C:/Windows/Fonts/" just in case.
Once it's already installed, try the following R code to see and example:
install.packages("showtext")
install.packages("showtextdb")
library(showtext)
library(showtextdb)
#set the name and file path
font_add(family = "lmroman10", regular = "C:/Windows/Fonts/lmroman10-regular-webfont.ttf")
showtext_auto()
library(ggplot2)
p = ggplot(NULL, aes(x = 1, y = 1)) + ylim(0.8, 1.2) +
theme(axis.title = element_blank(), axis.ticks = element_blank(),
axis.text = element_blank()) +
annotate("text", 1, 1.1, family = "lmroman10", size = 15,
label = "Text using new font")
I want to generate R-Diagrams using a special font, namely Cormorant-Garamond-Light (Cormorant-Light is also possible). The problem is that it works with every other font, but with this one, all spaces are just ignored.
library(ggplot2)
library(extrafont)
data = data.frame(read.table(file="PATH/TO/FILE"))
p = ggplot(data = data, aes(x = data[1], y = data[2]))
p = p + xlab("Time t/s")
p = p + ylab("Temperature T/°C")
p = p + theme(text = element_text(family = "Cormorant Garamond Light"))
After compiling, there is a warning which contains the following:
1: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x20
The resulting pdf-file looks as follows (the code is shortened to make it faster to read)
compiled pdf of the code above
Thanks for potential help!