Labels cannot be placed in second boxplots in R? - r

If you run the following simple code, you will notice that although in the first boxplot there are labels, this is not the case with the second boxplot where there are no labels. How to fix this? Is it an internal R bug?
I have tried to store boxplots in a working directory just in case this is an R studio graphics problem, but still... the same issue arised even then so this is not an Rstudio problem.
df=data.frame("A"=rnorm(10),"B"=rnorm(10),"C"=rnorm(10),"D"=rnorm(10))
for (i in 1:2){
boxplot(df[,1:2],xaxt='n')
axis(1,at=c(1,2))
boxplot(df[,3:4],xaxt='n')
axis(1,at=c(3,4))}

Related

par() function acting up in Rstudio - plotting weird results or no results at all

I've found a couple of similar posts but unfortunately not one reply seems to fix my problem.
Data = working on the uscrime dataset.
Essentially, I'm having trouble my plots when using par() in RStudio. It's not the first time it happens that all of the sudden after re-rerunning a chunk in R, the plot is no longer displayed inline nor in the Plots window.
My code is the following:
par(mfrow=c(3,5))
for(i in colnames(crime[,1:15])){
plot(crime[,i],crime[,16], main=print(paste("Crime vs", i)), xlab=i, ylab="Crime")
lines(lowess(crime[,i],crime[,16]), col="red")
abline(lm(crime[,16]~crime[,i]), col="blue")
}
The first time I ran the chunk the plots showed up:
Now, as soon as I tried to plot something different (same data but with a transformed column) using the same approach, nothing appeared inline. Here's my code:
par(mfrow=c(3,5))
for(i in colnames(crime2[,1:15])){
plot(crime2[,i],crime2[,16], main=print(paste("Crime vs", i)), xlab=i, ylab="Crime")
lines(lowess(crime2[,i],crime2[,16]), col="red")
abline(lm(crime2[,16]~crime2[,i]), col="blue")
}
I tried restarting R and running everything again and ow I'm getting a weird result where some of the information I passed to my first par() call is getting displayed in my charts (see how I have two blue curves when I should have a blue and a red one).
Now, I've been reading about 'devices' and plots but since I'm an R noob I can't figure out what's wrong on my own. It looks like I have to call dev.off() at some point but I don't quite get it. My expectation is that there's something wrong (or an additional step to take) with my par() calls but I haven't found the replies in R Documentation.
Here's a view of my Markdown config

degree symbol for labels in R is messed up

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!

R plot symbols on axis not shown correctly

Hi guys,
I have set up a new conda R-environment (on my ubuntu 20.04) and oddly, the output of the base plot function does not show symbols on the axes or axis labels but little squares with numbers instead (see attached picture).
When I use the plot function in an R-Markdown code chunk and export it as PDF, the plot is displayed correctly. However, in RStudio or when exporting the Rmd to html, the symbols appear like in the picture.
I realize this could have to do with the encoding, but don't know how to solve it. Has anyone encountered this problem before or can give me advice how to troubleshoot it?
Many thanks in advance!
Simply adding the package r-cairodevice to my R environment solved the problem.

Cleared all plots, now ggplot2 will not plot

I've been having issues with ggplot2 for a few days. First, when I would try to run different codes, the p value from my previous plot would show up on the new one. This would happen even when I knew they should have different p values and the dots from the plots were visibly different. I then tried to clear all my plots using the "Clear all plots" broom button. Since I've done this, I have not been able to plot anything. The plots don't automatically pull up like they used to and when I call the plot, it doesn't come up because it appears that the plot isn't saved to my directory and isn't a part of my environment. I've tried running older codes that I know work and still haven't been able to produce any plots.
Try running
dev.off()
and replotting your graph.

Difference in plot when shown in R Studio and exported with ggsave()

I want to add an additional tick to my plot in ggplot2, and used Solution 2 as described in the post Annotate ggplot with an extra tick and label.
It worked fine for me, giving the following result in R Studio:
But when I try to save the result using ggsave() to create the a .pdf, .ps, or .png file, the red number is cut off half like this:
I have the feeling that the inner plot is printed first and later the margins are plotted on top of this.
Anybody has a hint?
Thank you Z. Lin! I just had a grid.draw(g) instead of g <- grid.draw(g). This dot in R always activates my python brain region :)

Resources