I have a Mac (running on High Sierra 10.13.4 (17E199)) and use R (R 3.5.0 GUI 1.70) for analysing my data. As a biologist I am using greek letters such as µ really regularly. In my script I can nicely write µ and others but once I plot the graph I get exactly two dots instead of my letter. So for example µg will be written as ..g
I of course checked already my script on other computers. My windows computer does write µg on the axes- using the EXACT same script. My colleague also has a Mac and she is also able to use my script and produce nice greek letters. Can anyone help me or might have an idea on what is wrong with my Mac?
Thats how I my piece of code with µ looks like:
mtext(text=expression('content [µg egg'^-1*']'), side =2, line=5, cex=1)
or
mtext(text=expression('δ'^15*'N'), side =2, line=3, cex=2)
Related
I just upgraded to Ubuntu 20.04. When I use ggplot2 to plot some geographical data, I discover that the degree symbol in the axis labels is messed up (I attached a snip). Does anyone have a solution for that? The same thing occurs if I use a different plotting library (e.g.lattice). I can provide some reproducible code as well although this is not exactly an error to reproduce..
Thanks!
do you guys know how to display an R plot in a terminal/console instead of showing it on plot viewers?
I've recently working to integrate R with external tools, and somehow the only function works is to returns all the value shown up in the console result. Thus, I need some sort of workaround to be able to post a plot in the external tools.
Thanks in advance!
No. That is not possible. The reason is that the plot needs a graphic device.
The terminal (and R console) is a text-based device.
What you can do, is use the image-format files as graphic device. Here, you have a multitude of choices, such as PNG, BMP, JPEG. In R, look up ?png.
Saving the output of one of R's built-in examples for the filled.contour function to PDF:
pdf('test.pdf')
require("grDevices")
filled.contour(volcano, asp = 1)
dev.off()
produces a discretised result (see below). Is there any way to fix this? System info:
> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.1 LTS
Edit after the accepted answer:
The problem is actually reported in the documentation of the pdf function:
If you see problems with PDF output, do remember that the
problem is much more likely to be in your viewer than in R ...
Symptoms for which the viewer has been at fault are apparent
grids on image plots ...
Unfortunately the default viewers on most Linux and macOS
systems have these problems, and no obvious way to turn off
graphics anti-aliasing.
It's an odd place to document the problem because the subdivision into rectangular segments is clearly caused by filled.contour and not by pdf. Otherwise ggplot2's output would also suffer from the same issue.
That is likely a result of antialiasing: when you display the image, it draws squares one at a time. As they are being drawn, the edge is a mix of the square colours and the white background, so it is drawn lighter.
Unfortunately, this isn't something that's really under your control. It's the PDF previewer that is introducing the artifacts. See this page https://codedocean.wordpress.com/2014/02/03/anti-aliasing-and-image-plots/ for a discussion.
The recommendation there worked for me: use the png() device with type = "cairo". This gives bitmapped output rather than the vector output of a pdf().
png('test.png',type="cairo")
filled.contour(volcano, asp = 1)
dev.off()
Edited to add:
I don't think you can do better with filled.contour, but if you are willing to switch to ggplot2 graphics you can. When it draws filled contours it appears to do it using polygons, not the image style plot that filled.contour uses. This still shows the same antialiasing bugs in the previewer, but now the lines appear along the borders between colours, which is much less irritating. For example:
df <- data.frame(x = as.numeric(row(volcano)-1)/(nrow(volcano)-1),
y = as.numeric(col(volcano)-1)/(ncol(volcano)-1),
z = as.numeric(volcano))
pdf('test.pdf')
library(ggplot2)
ggplot(df, aes(x=x, y=y, z=z)) +
geom_contour_filled()
dev.off()
I don't know how to get the same palette as filled.contour uses, i.e. function(n) hcl.colors(n, "YlOrRd", rev = TRUE). Maybe someone else can show us in a comment.
Create a plot with cars dataset and attribute speed along the X axis and distance in y axis .
Label the axes.
I have done this programming still there is some problem:
plot(cars,xlab="speed",ylab="distance")
Even though everything seems correct the terminal doesn't move to the next part
What environment are you using? I was able to execute the following code and produce the plot you want using both RStudio and R directly in the terminal.
library(MASS)
plot(cars, xlab="Distance", ylab="Speed")
If you're running straight from the terminal then you'll need to specify a window to pop up. The following question has an answer that outlines what to do depending on your operating system:
How to pop up the graphics window from Rscript?
So, for example, if you don't have your system configured to automatically open the plot window, and you're running on a mac, the following code will produce what you want directly from running R in the terminal:
library(MASS)
X11()
plot(cars, xlab="Distance", ylab="Speed")
Not sure if this is the appropriate place to ask this question, so please feel free to delete it...
I've been preparing a little introduction to course lately, and in the section about saving plots I noticed something odd. When I created a figure on my work PC, it looked the way it should. But when I ran the exact same code again on my Mac at home, the image looked somewhat blurred. I tried this for the png, tiff, and jpeg formats, the result is the same, the plot looks nicer when produced on a PC. I include two graphs below so you can see for yourself. (The first the the PC plot, the second the Mac plot). Although the PC produces the better quality, the figure produced by the Mac is larger (19 vs 8 KB). Below is another image, where both figures are magnified (Mac left, PC right). While the PC draws a crisp line, the Mac produces some kind of a shading around the lines. Can anyone explain why this is the case and how I can produce better quality plots on the Mac? I played around with the options, in particular the type argument, but couldn't neither find a solution, nor find help on the internet.
Here is the code to generate the plot:
set.seed(2)
BMI<-rnorm(n=1000, m=24.2, sd=2.2)
png(file="fig3_m2.png", width=700, height=700, res=120)
par(mgp=c(1.7,.6,0), mar=c(3,3,2,2), yaxs="i")
x <- hist(BMI, freq=F, main="Distribution of BMI",
xlab="Body Mass Index", col="lightgreen",
ylim=c(0,.2),
breaks=seq(min(BMI), max(BMI), length=18))
box()
dev.off()