no more geom_label( ) in ggplot2 1.01? - r

I was following this ggplot2 docs try to reproduce text labels with geom_label. But I got an error message,
"could not find function geom_label"
. ?geom_label also says no such function. I checked on another two computers and got the same error message. All are with R 3.22, in RStudio 0.99.489 or in pure R command. ggplot2 is version 1.01 installed with install.packages(ggplot2).I did not find a clue from Google. So it seems that geom_label has been removed from the latest ggplot2 before any documentation can be made.
My question is: what is used to replace geom_label, which produces nice text labels in a boxed background?
Here is the code from the ggplot2 docs that suppose to produce the figure below.
p <- ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars)))
p + geom_label()

geom_label was implemented on 2015-07-24. The current version on CRAN (1.0.1) was published on 2015-03-17. You'll need to install the development version from GitHub if you want to use geom_label or wait until it is uploaded to CRAN (which might take a while).

Related

bgroup does not render properly on ggplot

The results of bgroup from plotmath do not seem to render properly as a ggplot2 annotation. For example ...
library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) +
annotate("text", x=2.5, y=25, label="bgroup('(',atop(x,y),')')", parse=TRUE) +
annotate("text", x=3.5, y=25, label="group('(',atop(x,y),')')", parse=TRUE)
... produces the following graph for me ...
Note how the bgroup rendering on the left does not have large parentheses like I would expect (and worked in some previous version of R), whereas the reqular group rendering to the right does seem to work.
Am I missing a font? Something else?
I am using R 4.2.2 and ggplot2 3.4.0.
EDITS:
The code above worked properly in R versions 4.0.5, 4.1.0, 4.1.2, and 4.2.0 (and 3.6.3 according to Jamie in the comments .. but not on Linux according to an answer on R-help).
The issue does not seem related to ggplot2 as the same problem occurs with base graphics.
The same issue occurs with R version 4.2.2 whether the code is run in RStudio or the RGui.
The same issue occurs (in R version 4.2.2) when the plot is directed to a PNG but not when it is directed to a PDF.
plot(0,xlim=c(0,1),ylim=c(0,1))
text(0.5,0.5,expression(bgroup('(',atop(x,y),')')))
It's working fine for me on Linux; I guess you guys are all using Windows?
Note that "rendering" strongly depends on the graphics device.
What is your .Device {after plotting}?
I suggest a Windows graphapp font problem, similar to the one just fixed yesterday, also Windows-only:
https://bugs.r-project.org/show_bug.cgi?id=18440

Text not displaying in ggplot2 plot using rpy2 magic

When I generate any ggplot2 plot using R magic in Jupyter Notebook, all text in the plot is rendered as little empty boxes.
My environment is running the notebook via Jupyter Hub on Ubuntu server. R magic is working great in general, with the goal of re-using some existing R code I have to output a Word Cloud via wordcloud package. However, when I generate any plot using ggplot2 any text is missing and all I see are small empty boxes.
library(ggplot2)
ggplot(mtcars, aes(x = drat, y = mpg)) +
geom_point()
see image here
The plot should show text on the axis labels but no text is displayed, just empty boxes.
Generating the same plot when running R from ssh terminal and saving it to disk (it's on a headless server) looks fine. Generating plot via pandas data frame in a different cell looks fine. So it seems related to rpy2? I'm stumped.
[EDIT]: in fact I see the same behavior using R kernel on this server, so it seems to not be specific to rpy2 / R magic.
I was able to solve this by installing all the needed packages via R install.packages() rather than conda. Next time I think I will avoid using conda for R packages altogether; this isn't the first time it has caused issues.

Flag to print plots [duplicate]

I have a strange issue with Rstudio: If a script calls ggplot2 functions to display a plot, then using Source to run the script does not produce the plots. If I select the whole script with Ctrl+A, then Run the current line or selection (Ctrl+Enter), then the plot does display. Likewise, typing plotting commands into the console produces correct output.
For example:
library(ggplot2)
p = ggplot(mtcars, aes(wt, mpg))
p + geom_point()
Will only produce output if pasted into console, not if sourced.
There are other questions about this, but neither is helpful:
ggplot2 ggsave function causes graphics device to not display plots falsely claims the issue is fixed in newer versions, it has not.
RStudio - ggplot not saving first plot when printing and saving multiple plots in a script was closed as a duplicate, yet not only is it not a duplicate, but the dev.off() workaround doesn't work ("Error in dev.off() : cannot shut down device 1 (the null device)")
How can I get Rstudio to display plots when a script is sourced? I am using Rstudio 0.98.1062 and R 3.1.1.
The solution is to explicitly call print() on ggplot object:
library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg))
p <- p + geom_point()
print(p)
ggplot function returns object of class ggplot; ggplot2 works by overloading print function to behave differently on objects of class ggplot - instead of printing them to STDOUT, it creates chart.
Everything is working well in interactive mode, because R assumes that most of commands are run through print() function. This is for our convenience and allows us to type rnorm(1) and get any visible output. When Run current selection command is used (Ctrl+Enter), RStudio behaves as if each selected line was typed in interactive mode and run. You can verify that by checking your command history in Console pane after running few selected lines.
But this convenient mode is abandoned when file is read by source(). Since this function is intended to run (potentially long and computationally-expensive) R scripts, it is undesirable to pollute STDOUT with low-priority messages. That's why source() by default will output only error message. If you want anything else, you have to explicitly ask for that.
though it's a quite old question. I had the same problem and found a quick solution, if you want to use "source" button on R studio edit box.
you can simply turn on "source with echo" (Ctrl + Shift + Enter) and the plot shows as expected
I recently happened on this question and realized that the most up to date way is to call show(p) after creating the plot.
I found this question when searching a similar problem (plots not showing up in RStudio). I was trying to troubleshoot a complicated ggplot2 block by running it in parts, but couldn't get anything to show up in the plot window.
Reason: the tiff() function I opened earlier had not closed.
Solution: I ran dev.off() a few times until all my earlier tiff() functions completed, then I was able to create plots in RStudio and view the results in the plot window.
Another option is simply using plot(). When clicking on "Source" in Rstudio, it show the plot in the window like this:
library(ggplot2)
p = ggplot(mtcars, aes(wt, mpg))
p = p + geom_point()
plot(p)
# This pops when clicking on Source
source("~/.active-rstudio-document")
Output:

Error: ScalesList was built with an incompatible version of ggproto

I'm doing a presentation in slidfy, using the deckjs framework.
Everything was ok, but suddenly this chunk of code:
ggplot(cars, aes(x = speed, y = dist)) + geom_point(color = 'red') + stat_smooth(method = "lm", formula = y ~ x, size = 0.5, se = F)
stopped working and shows this error instead:
## Error: ScalesList was built with an incompatible version of ggproto.
## Please reinstall the package that provides this extension.
The code works perfectly when executed from source or console... But it doesn't work anymore from the R markdown. The function that fails is the stat_smooth(). The rest visualizes ok if executed without the smooth.
Here and here a similar error is reported, and the solution offered is to reinstall ggplot and the packages from github, but I'm not sure which packages should I install, and, besides, the code only fails from Rmarkdown, and not when executed from console or source.
Thanks
I had a similar problem in RMarkdown after updating ggplot2. I was loading a workspace with plots created with the older version of ggplot2. The solution was to recreate that workspace with the updated version, and now RMarkdown works.
This is very strange; now things are back to normal.
I did what #baptiste suggested, and the R source code just worked ok.
After that, I knitted the Rmarkdown again and the error had disappeared!
in Feb 2017 the same error was resolved by re-installing all the packages (RStudio - Tools - Check for package updates)
I had similar problem with geom_tufteboxplot . I first updated ggplot2 & ggthemes package, then detached all ggplot related packages and reloaded them. It worked for me.

ggplot plots in scripts do not display in Rstudio

I have a strange issue with Rstudio: If a script calls ggplot2 functions to display a plot, then using Source to run the script does not produce the plots. If I select the whole script with Ctrl+A, then Run the current line or selection (Ctrl+Enter), then the plot does display. Likewise, typing plotting commands into the console produces correct output.
For example:
library(ggplot2)
p = ggplot(mtcars, aes(wt, mpg))
p + geom_point()
Will only produce output if pasted into console, not if sourced.
There are other questions about this, but neither is helpful:
ggplot2 ggsave function causes graphics device to not display plots falsely claims the issue is fixed in newer versions, it has not.
RStudio - ggplot not saving first plot when printing and saving multiple plots in a script was closed as a duplicate, yet not only is it not a duplicate, but the dev.off() workaround doesn't work ("Error in dev.off() : cannot shut down device 1 (the null device)")
How can I get Rstudio to display plots when a script is sourced? I am using Rstudio 0.98.1062 and R 3.1.1.
The solution is to explicitly call print() on ggplot object:
library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg))
p <- p + geom_point()
print(p)
ggplot function returns object of class ggplot; ggplot2 works by overloading print function to behave differently on objects of class ggplot - instead of printing them to STDOUT, it creates chart.
Everything is working well in interactive mode, because R assumes that most of commands are run through print() function. This is for our convenience and allows us to type rnorm(1) and get any visible output. When Run current selection command is used (Ctrl+Enter), RStudio behaves as if each selected line was typed in interactive mode and run. You can verify that by checking your command history in Console pane after running few selected lines.
But this convenient mode is abandoned when file is read by source(). Since this function is intended to run (potentially long and computationally-expensive) R scripts, it is undesirable to pollute STDOUT with low-priority messages. That's why source() by default will output only error message. If you want anything else, you have to explicitly ask for that.
though it's a quite old question. I had the same problem and found a quick solution, if you want to use "source" button on R studio edit box.
you can simply turn on "source with echo" (Ctrl + Shift + Enter) and the plot shows as expected
I recently happened on this question and realized that the most up to date way is to call show(p) after creating the plot.
I found this question when searching a similar problem (plots not showing up in RStudio). I was trying to troubleshoot a complicated ggplot2 block by running it in parts, but couldn't get anything to show up in the plot window.
Reason: the tiff() function I opened earlier had not closed.
Solution: I ran dev.off() a few times until all my earlier tiff() functions completed, then I was able to create plots in RStudio and view the results in the plot window.
Another option is simply using plot(). When clicking on "Source" in Rstudio, it show the plot in the window like this:
library(ggplot2)
p = ggplot(mtcars, aes(wt, mpg))
p = p + geom_point()
plot(p)
# This pops when clicking on Source
source("~/.active-rstudio-document")
Output:

Resources