RStudio y-axis label displays incomplete letters - r

When I use base R to plot something, then certain characters will not be displayed correctly, e.g.
plot(1:10,1:10, xlab = paste(letters[1:26], collapse =''), ylab = paste(letters[1:26], collapse =''))
produces the following plot:
Characters such as h, n and u are chopped off in the console view - however only on the y-axis. Exporting the plot via png() does not work, either. I have noticed that this problem only occurs when the code is executed in RStudio (Version 2022.07.0 Build 548, Windows 10).

Related

"Plot.new has not been called yet" issue using plot() [duplicate]

Why does this happen?
plot(x,y)
yx.lm <- lm(y ~ x)
lines(x, predict(yx.lm), col="red")
Error in plot.xy(xy.coords(x, y), type = type, ...) :
plot.new has not been called yet
Some action, very possibly not represented in the visible code, has closed the interactive screen device.
It could be done either by a "click" on a close-button, or it could also be done by an extra dev.off() when plotting to a file-graphics device. (The second possibility might happen if you paste in a multi-line plotting command that has a dev.off() at the end of it, but had errored out at the opening of the external device. So the dangling dev.off() on a separate line accidentally closes the interactive device).
Some (most?) R implementations will start up a screen graphics device open automatically, but if you close it down, you then need to re-initialize it.
On Windows that might be window(); on a Mac, quartz(); and on a Linux box, x11(). You also may need to issue a plot.new() command. I just follow orders. When I get that error I issue plot.new() and if I don't see a plot window, I issue quartz() as well. I then start over from the beginning with a new plot(., ., ...) command and any further additions to that plot screen image.
In my case, I was trying to call plot(x, y) and lines(x, predict(yx.lm), col="red") in two separate chunks in Rmarkdown file. It worked without problems when running chunk by chunk, but the corresponding document wouldn't knit. After I moved all plotting calls within one chunk, problem was resolved.
As a newbie, I faced the same 'problem'.
In newbie terms :
when you call plot(), the graph window gets the focus and you cannot enter further commands into R. That is when you conclude that you must close the graph window to return to R.
However, some commands, like identify(), act on open/active graph windows.
When identify() cannot find an open/active graph window, it gives this error message.
However, you can simply click on the R window without closing the graph window. Then you can type more commands at the R prompt, like identify() etc.
plot.new() error occurs when only part of the function is ran.
Please find the attachment for an example to correct error
With error....When abline is ran without plot() above
Error-free ...When both plot and abline ran together
I had the same problem... my problem was that I was closing my quartz window after plot(x,y). Once I kept it open, the lines that previously resulted in errors just added things to my plot (like they were supposed to). Hopefully this might help some people who arrive at this page.
If someone is using print function (for example, with mtext), then firstly depict a null plot:
plot(0,type='n',axes=FALSE,ann=FALSE)
and then print with newpage = F
print(data, newpage = F)
I had the problem in an RMarkdown, and putting the offending line on the previous line of code helped.
Minimal Reproducible Example
This will error if run line by line in an Rmd:
x <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2),
matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2))
colnames(x) <- c("x", "y")
(cl <- kmeans(x, 2))
plot(x, col = cl$cluster)
points(cl$centers, col = 1:2, pch = 8, cex = 2)
but this works:
x <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2),
matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2))
colnames(x) <- c("x", "y")
(cl <- kmeans(x, 2))
plot(x, col = cl$cluster); points(cl$centers, col = 1:2, pch = 8, cex = 2)
The only change is that the offending line (the last one) is placed after the last succeeding line (placing a ; in between). You can do it for as many offending lines as desired.

How to make plots appear in Rmarkdown file instead of viewer pane when working with keras in Rstudio?

I am new in R and trying keras in Rstudio. All the accuracy, loss interactive plots by running fit() are appearing in Viewer pane instead of Rmarkdown file.
All other plots usually plot inside Rmarkdown files but not with keras.
I have also checked Global settings in Rstudio Tools>Global Options>R Markdown>Show output preview in "Window".
How do I make them plot in Rmarkdown instead of Viewer Pane.
How do I make this mandatory for all plots to create inside Rmarkdown files only and now in viewer pane?
have you tried to plot in only one cell ?
for example you have a code block only like this
plot(x = dataframe$x, y = dataframe$y,
xlab = "X",
ylab = "Y",
xlim = c(0,1.5),
ylim = c(0,1.5),
main = "X and Y between 0 and 1",
col = dataframe$color,
pch = 16,
cex = 1.5
)
if you convert the rmarkdown file into html, the output of the plot will be shown
like this, in the browser. the only criteria you need to respect is put the plot function call alone in one cell

Rendering graphics in R Markdown

I have been using R Markdown to reproduce R code and report on some data processing. In terms of plotting, there is a difference between what is output to the RStudio IDE and what is rendered via knitr in html. The follwing is the code that applies to both situations
barplot(top10summariseDamage$totalDamage,
names.arg = c('Hurricane Opal','Heavy Rain/Severe Weather',
'Tornadoes, TSTM Wind, Hail', 'Severe Thunderstorm',
'Wild Fires', 'Hail Storm', 'Major Flood',
'Hurricane Opal/High Winds', 'Winter Storm/High Winds',
'Record Cold'),
ylim = c(0, 5*10^10), las = 2, ylab = 'Dollars Damage ($)',
main = "Economic Damage by Event Type")
The following is a dput of the vector being plotted, where I have added commas to the number for additional clarity
c(31728479000, 2.5e+10, 16000002500, 12053600200, 6.241e+09,
2.41e+09, 1.05e+09, 1000010000, 600005000, 5.6e+08)
Which in RStudio correctly produces the following
As you can see the left most bar is less than 4e10. However, when I use knitr to produce an html report, the following is rendered
As you can see the bar values are literally off the scale, even though I increased the y axis by a factor of 10. Any help appreciated.

Saving plots with "≤" and "≥" symbols in R

I have a strange problem:
I am working on a plot that in a legend text contains "≥" symbol. For example, "x ≥ 2". Interestingly, the symbol appears correctly when I plot it using the R graphic device, but it appears incorrectly when I save it as PDF or EPS.
Any suggestions how can I save it in PDF / EPS correctly?
Are you adding the symbol using an expression and the relevant ?plotmath markup? I suspect the problem is that you've literally used the "≥" glyph in the legend text. That will only work if you set the encoding correctly (see ?pdf), and then that may not work well everywhere.
Doing this va plotmath should be portable:
plot(1:10)
legend("topleft",
legend = c(expression(x >= 2), expression(x <= 1)),
pch = 1:2)
I suspect you're using a mac?
Use cairo_pdf instead of pdf:
> cairo_pdf("tmp.pdf")
> plot(2:10, xlab="x ≥ 2")
> dev.off()
null device
1

Saving a plot in 'R' in 'eps' or 'pdf' format via 'rgl.postscript' (why color is changed?)

I am trying to run this code in "R" in order to plot a density function kernel smoothing and then save the plot as an "eps" file:
library(ks)
library(rgl)
kern <- read.table(file.choose(), sep=",")
hat <- kde(kern)
plot(hat, drawpoints=TRUE, xlab = "x", ylab= "y", zlab= "z")
rgl.postscript("plot1.eps","eps",drawText=TRUE)
The problem is that when I save the plot in eps format, it just shows the plot in one color (yellow) instead of a rage of colors (yellow, orange, red...) which shows different densities... (The plot in R is fine but when I save it as eps, the color is changed)
Do you know what is the problem with this code, or could you introduce me any other function that can save the plot that I generated in R as pdf or eps? (since it's an interactive RGL plot, I cannot use pdf() function. Also I tried rgl.snapshot, but every time it crashes and seems doesn't work...Following is part of my data: (x,y,z locations of points)
163.911642 248.952593 1.428709101
163.930843 249.077891 0.425459167
163.773321 249.288606 2.319076487
162.256416 246.990378 2.658388572
165.300014 247.950225 2.151660061
164.922344 249.017609 0.848590512
163.909127 248.881616 2.466267052
163.91322 249.118025 0.576787314
164.658937 249.547614 1.25279399
378.710211 132.601628 0.109772592
379.402818 132.858292 0.756297975
379.91023 132.912093 0.436653026
379.453418 132.815987 0.349591828
379.46519 132.694182 0.648543879
378.01787 132.615919 0.245000332
56.816299 27.469173 2.587220473
56.958051 27.103275 3.64182119
55.763676 26.59811 2.957732481
55.299606 26.054619 0.924209356
56.505342 27.671528 4.191381437
... ... ...
295.366938 24.360195 2.779443783
291.361501 23.21226 0.975021774
292.26276 22.053726 1.092983265
159.333055 41.087969 0.185656788
154.424175 37.83109 3.327367998
158.723404 40.487105 0.563578901
157.204282 41.678425 1.464830439
What I understand from your question is that the colours become different from what is displayed in X11 (assuming Linux environment) and the postscript (or eps) file.
Gnuplot colours are set for different terminal i.e. colours admissible to different terminals. So, the colours displayed in X11 (the default display term in Linux or Mac OSX) may be different in postscript.
To examine the permissible colours in a particular terminal is the Gnuplot command test. So, inside Gnuplot, the following set of command will produce a test.eps which when displayed will give you an idea what real colours you will be reproducing.
gnuplot> set term post colour eps
gnuplot> se out 'test.eps'
gnuplot> test
gnuplot> se te X11
Look at the test.eps file for true colour reproduction.

Resources