Running Ubuntu 16.04; R 3.6.2; ggplot2 3.3.0
Running R under --nix
If I run this ..
library(ggplot2)
data("midwest", package = "ggplot2")
ggplot(midwest, aes(x=area, y=poptotal))
I get a plot with little boxes for the axes (Unicode?)
I get the same little boxes if I use 'plot'
But If I run 'plot' add a 'family' attribute,
plot (1:10, family="arial")
I get this (nice axes),
This shows that at least some fonts are there!
Back to ggplot ....
The easy solution would be to figure out (I tried) how to set the family in ggplot.
I tried,
ggplot(heightweight, aes(x= ageYear, y=heightIn, font="ariel")) + geom_point()
ggplot(heightweight, aes(x= ageYear, y=heightIn, family="ariel")) + geom_point()
No help .. Little boxes.
Note: It's happy if I put family="Zombie"
Anyone know how to set the family in ggplot?
A better solution?
The hard solution would be for me to figure out which fonts are missing, install them under --nix, and then make sure R (under --nix) can find them.
After much playing I got something to work!
I now get a beautiful title and axes (no more Unicode).
Here's the snippet
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
ggtitle("Fuel Efficiency of 32 Cars") +
xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
theme_bw() +
theme(text=element_text(family="Garamond", size=14))
Apparently the way to set the family in ggplot is using 'theme',
theme(text=element_text(family="Garamond", size=14))
I know it's not a perfect solution but it gets me going (without meds :-) ).
I think it would be worth renaming this issue because it is a Nix specific issue, and was reasonably hard to find. The core issue is caused by a mismatch between the system fontconfig and the one provided by nix.
https://discourse.nixos.org/t/fonts-in-nix-installed-packages-on-a-non-nixos-system/5871/6
I fixed the issue by adding an explicit fontconfig dependency and adding the following to my mkShell command
shellHook = "export FONTCONFIG_FILE=${pkgs.fontconfig.out}/etc/fonts/fonts.conf";
After that opening R from within a nix-shell and generating plots works as expected.
Related
I am trying to export some unicode characters in the U+1xxxx format from R to pdf using ggplot2 (this is one example: https://unicode-table.com/en/1F321/). In R, the output is great when I have it in the ggtitle as ggtitle("Temperature range \U1F321"):
However, when I export the plot to pdf it appears as these blocks below:
I have tried many things that other people suggested (such as using cairo pdf and the showtext package) but none of it worked. I am using Windows.
Thank you very much!
EDIT: I'm saving as pdf using pdf("example.pdf") or cairo_pdf("example.pdf")
Reproducible code:
ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point() + ggtitle("Temperature range \U1F321")
cairo_pdf("plot_cairo.pdf")
ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point() + ggtitle("Temperature range \U1F321")
dev.off()
pdf("plot_normal.pdf")
ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point() + ggtitle("Temperature range \U1F321")
dev.off()
Also, just want to state my intentions more clearly. I'm just aiming to have a high-resolution ggplot exported from R to Word. When I export it as an image, the characters show well but the resolution is of poor quality. Following suggestions from other users, I'm therefore first exporting it as pdf and then importing it to Word as an object from the "Insert" tab:
Using this method, the plot is of much better resolution in Word, but the unicode characters don't show up well. So if maybe someone else has another suggestion on how to get a high quality ggplot exported from R to Word while still preserving the unicode characters, that would also work! Thank you!!
When I save a ggplot image with theme_minimal the black and white values are reversed in a photo negative like effect. This does not occur if I do not use a theme nor does it occur with theme_bw. It also does not occur when saving to .pdf or .png. I have tested and this occurs when running in RStudio, R GUI, or through the terminal. I'm running R version 4.0.2 on Mac OS 10.15.7.
I would greatly appreciate any insight into debugging this. The behavior has persisted for several weeks across multiple full system restarts.
library(ggplot2)
ggplot(diamonds, aes(x = cut, y = clarity)) +
geom_point() +
theme_minimal()
ggsave("test_minimal.jpg")
Seems like 'theme_minimal' defaults to black background for jpg files (pdf and png were fine and I used Windows 10). #stefan had proposed two ways to overcome this in the comments above. I did not see that and went searching again. So posting the full solution here:
library(ggplot2)
ggplot(diamonds, aes(x = cut, y = clarity)) +
geom_point() +
theme_minimal()
ggsave("test_minimal.jpg",bg="white")
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 ?
I have plot and I need to add mm³ but I can't use bquote because I need to wrap the text using
paste(strwrap(text, width), collapse="\n")
so I though that I use ASCII ³ character bcause that's the only superscript value I need, but it display as mm3 when rendered, is it the font issue? How to resolve it? I need to run this on any platform becuase other developers use Mac/Windows, I use windows and it run Linux server.
Details on how to render mathematics in plots and elsewhere in R are documented in ?plotmath.
A simple example, plotting the city gas millage by engine squared engine displacement.
library(ggplot2)
ggplot(mpg) +
aes(x = displ^2, y = cty) +
geom_point() +
xlab(expression(displ^2)) +
ylab("City Miles Per Gallon")
I was looking here but I can't figure it out.
How can I change the word "type" to something else?
Add
+ labs(colour = "legend title")
to your ggplot call.
Great resource site is also google group for ggplot2.
edit: this assumes that colour is the aesthetic in the legend, e.g. qplot(x,y,colour=z). If another aesthetic is being shown in the legend, use that as the argument instead, e.g. + labs(fill = "legend title") for a raster/image plot.
More generally, if you specify an explicit scale such as scale_colour_continuous, you can set the scale_name argument (warning: the details of the arguments to scales may have changed in recent releases of ggplot2; this description is of version 0.9.2.1).
Add either:
+ scale_fill_discrete(name="Title", labels=c("1","2","3"))
or
+ scale_colour_discrete(name="Title", labels=c("1","2","3"))
depending on the geom.
I recommend you look into the ggplot2 cheatsheet. https://www.rstudio.com/wp-content/uploads/2015/03/ggplot2-cheatsheet.pdf
None of the others worked for me for some reason. In case they failed for you as well, adding this worked for me:
+ guides(fill=guide_legend(title='MY NEW TITLE'))
Just a word to the wise ... all of these options above can be overridden if you do something dumb like I just did. Earlier in my project, I had simply turned off a legend I didn't like using the "themes" parameter within ggplot:
theme(legend.title = element_blank())
If you do this, no matter what beautiful commands you put in to change the title -- and I tried all of those above -- they won't be overridden by your command to turn the legend off! You have been warned! ;{)