text in ggplot2 plots became rectangles with code [duplicate] - r

I have got the following problem: When I plot anything with ggplot2 like this
# Libraries
library(ggplot2)
# create data
xValue <- 1:10
yValue <- cumsum(rnorm(10))
data <- data.frame(xValue,yValue)
# Plot
ggplot(data, aes(x=xValue, y=yValue)) +
geom_line()
The resulting graph looks like this where the text is shown in weir unicode blocks:
ggplot2 graph with text issue
These unicode blocks look like boxes with four numbers starting with two 0s like:
# Example block
----
|00|
|2C|
----
I already tried to update and reinstall the tidyverse package, I reopened R-Studio and only called the library ggplot2 in order to have no conflicting packages open, I could not find any similar issue on the internet whatsoever. I hope you can help me out and please do not hesitate if you need further information from me.
R version: 3.6.1 (2019-07-05)
platform: linux mint x86_64
conda environment
EDIT: For anybody who is interested in solving this issue permanently look here. I had to upgrade to R Version 4.0.3 in order to make ggplot work properly again.

This looks a lot like a font issue. Maybe the default ggplot font is not installed or damaged? Try querying installed fonts (for Linux):
system("fc-list")
Output should be a list of entries like this:
/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf: DejaVu Serif:style=Book
Then you can set the font as a default for your ggplot theme (name of the ttf file is the correct way to name it as far as I can tell):
theme_set(theme_gray(base_family = "DejaVuSerif"))
If this returns the same result, try another one.

try to solve the problem by changing the text size and the color and theme of you're text? maybe that will work... sth like this:
ggplot(data, aes(x=xValue, y=yValue)) +
geom_line() + theme(text = element_text(size=20))

Related

ggplot2 issue: graph text shown with weird unicode blocks

I have got the following problem: When I plot anything with ggplot2 like this
# Libraries
library(ggplot2)
# create data
xValue <- 1:10
yValue <- cumsum(rnorm(10))
data <- data.frame(xValue,yValue)
# Plot
ggplot(data, aes(x=xValue, y=yValue)) +
geom_line()
The resulting graph looks like this where the text is shown in weir unicode blocks:
ggplot2 graph with text issue
These unicode blocks look like boxes with four numbers starting with two 0s like:
# Example block
----
|00|
|2C|
----
I already tried to update and reinstall the tidyverse package, I reopened R-Studio and only called the library ggplot2 in order to have no conflicting packages open, I could not find any similar issue on the internet whatsoever. I hope you can help me out and please do not hesitate if you need further information from me.
R version: 3.6.1 (2019-07-05)
platform: linux mint x86_64
conda environment
EDIT: For anybody who is interested in solving this issue permanently look here. I had to upgrade to R Version 4.0.3 in order to make ggplot work properly again.
This looks a lot like a font issue. Maybe the default ggplot font is not installed or damaged? Try querying installed fonts (for Linux):
system("fc-list")
Output should be a list of entries like this:
/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf: DejaVu Serif:style=Book
Then you can set the font as a default for your ggplot theme (name of the ttf file is the correct way to name it as far as I can tell):
theme_set(theme_gray(base_family = "DejaVuSerif"))
If this returns the same result, try another one.
try to solve the problem by changing the text size and the color and theme of you're text? maybe that will work... sth like this:
ggplot(data, aes(x=xValue, y=yValue)) +
geom_line() + theme(text = element_text(size=20))

R ggplot not displaying

I know this question has been asked a number of times, but the solutions for those answers did not work for me. I am using R version 4.0.0 in R studio. I have been able to use ggplot before I updated, so I am not sure if that is related to the issue or not.
I am trying with one of the ggplot examples:
library("ggplot2")
p1<- ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point()
show(p1)
I have also tried
print(p1)
Neither have shown the plot nor thrown an error. I am putting this directly into the console, so from what I've read, the print/show shouldn't be necessary, but it still does not show. P1 is created in my Global Environment and is a List of 9. Does anyone have any ideas??? Thanks!
Maybe you have redirected the output by opening a pdf() or jpeg() device that you have forgotten to close ?

degree symbol incorrect in map axis labels

If I create maps using geom_sf, the axis labels have the wrong symbol for degrees. I get degree symbols that are vertically centred in the text, rather than raised like superscipts.
For example,
library(sf)
library(ggplot2)
nc = st_read(system.file("shape/nc.shp", package="sf"))
ggplot() +
geom_sf(data = nc) +
theme(axis.text = element_text(size=16))
When I see examples online, they typically look correct (e.g. image below, copied from here), so I guess it is related to something in my local setup.
I have tried changing font, using library(extrafont) but this problem remains in every font I tried.
Update
I don't think this is a ggplot-specific issue, as I get the same thing with any graphics that uses the degree keyword in plotmath. For example
par(mar=c(0,0,0,0))
plot.new()
text(0.5,0.5, bquote(120*degree*N), cex=5)
I am on Linux (Kubuntu 19.04), R 3.5.2, ggplot2 v. 3.2.1, sf v. 0.7-7.
Not sure what other information might be relevant, but I can update answer with anything else that is requested.
Finally managed to track down the answer:
From ?X11 it says:
Problems with incorrect rendering of symbols (e.g., of quote(pi) and
expression(10^degree)) have been seen on Linux systems which have the
Wine symbol font installed – fontconfig then prefers this and
misinterprets its encoding. Adding the following lines to
‘~/.fonts.conf’ or ‘/etc/fonts/local.conf’ may circumvent this problem
by preferring the URW Type 1 symbol font.
<fontconfig>
<match target="pattern">
<test name="family"><string>Symbol</string></test>
<edit name="family" mode="prepend" binding="same">
<string>Standard Symbols L</string>
</edit>
</match>
</fontconfig>
Adding these lines to /etc/fonts/local.conf solved the problem for me.
I ran into a similar problem and after reading here and some other related issues I found a solution. I'm working with Rstudio, so I guess that if you are doing R without it you you will easily find your work around (hopefully).
My solution was to Tools -> Global Options
and here I changed my backend to Cairo.
This is how it looks the change:
As a workaround I am creating the axis labels manually. Still looking for a better solution and explanation of the undesired behaviour.
xlabs = seq(-84,-76, 2)
ylabs = seq(34, 36.5, 0.5)
ggplot() +
geom_sf(data = nc) +
scale_x_continuous(breaks = xlabs, labels = paste0(xlabs,'°W')) +
scale_y_continuous(breaks = ylabs, labels = paste0(ylabs,'°N')) +
theme(axis.text = element_text(size=16))

Using plotly to create an R plot that displays a title for each point when the mouse scrolls over it

According to this post it seems like I should just be able to to put the variable name holding the plot into the ggplotly function and then the graph should have mouseover points. The data points do not display anything when I scroll the mouse over in adobe reader nothing displays. I would like to be able to write my own custom labels for each point.
install.packages(c("devtools"))
library(devtools)
dev_mode(on=T)
install_github("hadley/ggplot2")
install.packages(c("ggplot2"))
library(ggplot2)
install.packages(c("plotly")) #Select mirror ("USA (WA)")
library("plotly")
install.packages(c("Cairo"))
library("Cairo")
all_rates = read.csv("file.txt")
for(i in unique(gsub('_[0-9]+', '', all_rates[,1]))){
rates <- all_rates[grep(i, all_rates[,1]),]
freq = unlist(rates[2])
temp = unlist(rates[3])
exn = unlist(rates[4])
p = ggplot(rates, aes(freq,temp)) + geom_point(data=rates, aes(freq,temp,group=factor(exn),colour=factor(exn))) + geom_smooth(data=rates, aes(freq,temp,group=factor(exn),colour=factor(exn)),method = "lm",fill=NA)
ggplotly(p)
ggsave(paste0(i, ".pdf"))
}
I still get
We recommend that you use the dev version of ggplot2 with `ggplotly()`
Install it with: `devtools::install_github('hadley/ggplot2')`
As a warning. I was under the impression that I was using this version though based on this post
This is the output I get after all the checks and creates for the packages
The downloaded source packages are in
'/tmp/Rtmpo0GRLE/downloaded_packages'
We recommend that you use the dev version of ggplot2 with `ggplotly()`
Install it with: `devtools::install_github('hadley/ggplot2')`
Saving 7 x 7 in image
We recommend that you use the dev version of ggplot2 with `ggplotly()`
Install it with: `devtools::install_github('hadley/ggplot2')`
Saving 7 x 7 in image
file.txt
clothing,freq,temp,exn
coat_1,0.3,10,1
coat_1,0.9,0,1
coat_1,0.1,20,1
coat_2,0.5,20,2
coat_2,0.3,15,2
coat_2,0.1,5,2
scarf,0.4,30,1
scarf,0.2,20,1
scarf,0.1,10,1

How to convert text within EPS figures to vector outlines in R?

I am preparing a paper for submission to a journal which requires vector graphics to be submitted in EPS format, with all text within figures converted to outlines. Converting text to vector outlines helps avoid font problems in the final layout. The journal requirements are available here.
EPS figures can easily be created in R using ggplot2:
library(ggplot2)
ggsave(filename = "file.eps")
or using setEPS() and the postscript device, as explained previously here:
setEPS()
postscript("filename.eps")
plot(1:10)
dev.off()
My question is: how can you convert text within the figures into vector outlines? I have found information on how to do this in other software (e.g. Illustrator or InDesign), but I would like to know if there is a way to do it directly in R.
Install ghostscript using your distro's package manager if you're under linux or download it for windows (there's also a portable version), for mac you can probably use brew. Then invoke gs or gs.exe from within R like in this SO question.
library(ggplot2)
data(diamonds)
gs.cmd <- "gs" # ghostscript executable
# gs.cmd <- "C:/path/to/gs.exe" # Windows
p <- qplot(x=carat, y=price, color=clarity, data=diamonds)
ggsave(p, filename="test.eps")
system(paste(gs.cmd,"-o test_outline.eps -dNoOutputFonts -sDEVICE=eps2write test.eps"))

Resources