Failing to create "flat violin plot" using geom_flat_violin() - r

I'm trying to use David Robinson's geom_flat_violin() to make a plot according to the example in this link: https://gist.github.com/dgrtwo/eb7750e74997891d7c20
ggplot(diamonds, aes(cut, carat)) +
geom_flat_violin() +
coord_flip()
If I follow the example I get
Error in geom_flat_violin() : could not find function "geom_flat_violin"
but when I try to copy in his function code, there are several "unexpected ',' " errors and I don't understand what to copy and what to leave behind. Can anyone help?

There are a number of issues with the code listed on the provided link. As the comments in the link suggested, there are some mismatched parenthesis. This can be fixed by removing line 50.
Also, be sure to install the libraries prior to running. This includes the ggplot2, dplyr, and plyr. Typically, they can be installed like so:
install.packages( c( "dplyr", "ggplot2", "plyr" ) )
I was able to run that code after making the fixes listed above.
source( './geom_flat_violin.R' )
ggplot( diamonds, aes( cut, carat ) ) + geom_flat_violin() + coord_flip()

Related

Horizontal line on ggplot2 barplot

I want a horizontal line to be placed over my ggplot2 bar plot at the average of all the bars:
ggplot(bar_graph, aes(x=V1, y=numBridges)) +
geom_bar(stat = "identity")+
xlab("Agreement")+
ylab("Number of Nodes")+
geom_hline(yintercept=15)
Unfortunately this code gives me the error message:
Error in is.finite(x) : default method not implemented for type 'list'
Which is super odd because 15 is not a list. It's also even more odd because, just to check, I ran the following code to make a vertical line - which worked well:
ggplot(bar_graph, aes(x=V1, y=numBridges)) +
geom_bar(stat = "identity")+
xlab("Agreement")+
ylab("Number of Bridge Nodes")+
geom_vline(xintercept=5)
A couple years ago there was a bug in ggplot2 that could only be resolved by installing directly from github, so in the event that this was still the problem I ran:
remove.packages('ggplot2')
devtools::install_github('hadley/ggplot2')
But that did not work either.
On Mac Monterrey:
packageVersion("ggplot2")
[1] ‘3.3.6.9000’
Does anyone have any ideas or workarounds? Would greatly appreciate any help!
Thanks to neilfws for pointing out that the column in my dataframe was indeed a list. I just did bar_graph$numBridges %>% unlist() to fix it.
I guess a nice reminder that a dataframe can be a cleverly hidden list!

Loading an R package without all the names in it

I'm sorry if this a duplicate, but it's un-internet-searchable.
I would like to load ggplot, but without littering my global namespace with all the functions therein.
I am content with accessing the functions as ggplot2::aes(), and I don't want just aes() to work.
In other programming languages, this is the default behaviour.
I think you have really answered this yourself. This works without any library statements as long as ggplot2 is installed:
ggplot2::ggplot(BOD, ggplot2::aes(Time, demand)) + ggplot2::geom_point()
This could alternately be done like this:
ggplot <- ggplot2::ggplot
aes <- ggplot2::aes
geom_point <- ggplot2::geom_point
ggplot(BOD, aes(Time, demand)) + geom_point()
Another possibility is to just temporarily add ggplot2 and then remove it again:
library(ggplot2)
ggplot(BOD, aes(Time, demand)) + geom_point()
detach("package:ggplot2", unload = TRUE)
There are a number of R packages which provide facilities that may be of interest including the modules and import packages on CRAN. Also the klmr modules R package on github (not on CRAN) provides a Python-like framework.

Coloring points based on variable with R ggpairs

I'm trying to reproduce the figure in https://tgmstat.wordpress.com/2013/11/13/plot-matrix-with-the-r-package-ggally/ with the code
require(GGally)
data(tips, package="reshape")
ggpairs(data=tips, title="tips data", colour = "sex")
However, in the plot I get the points are not colored based on sex, instead they are all the same color. I get the following warning
Warning message:
In warn_if_args_exist(list(...)) :
Extra arguments: 'colour' are being ignored. If these are meant to be >aesthetics, submit them using the 'mapping' variable within ggpairs with >ggplot2::aes or ggplot2::aes_string.
I've tried adding ggplot2::aes(colour = sex), but that did not work either.
Does anyone else here have the same problem? I'm using R version 3.3.1 and GGally_1.2.0.
Thanks.
GGally has been under fairly rapid development, so it's not surprising that a blog post from 2013 has out-of-date code. When I run your code with GGally 1.2.0 I get the same warning. It works for me if I add the mapping:
require(GGally)
data(tips, package="reshape")
g1 <- ggpairs(data=tips, title="tips data",
mapping=ggplot2::aes(colour = sex),
lower=list(combo=wrap("facethist",binwidth=1)))
Following the wiki page for the wrap() incantation to stop complaints about needing to set binwidth in stat_bin ...

Color error in ggplot2 (Error in grDevices::col2rgb(colour, TRUE) : invalid RGB specification)

If I run:
library(ggplot2)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
bp<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_boxplot()
bp
I see the error: Error in grDevices::col2rgb(colour, TRUE) : invalid RGB specification.
If I run the identical code on my other computer, I get the expected plot. I am guessing that some plotting parameter is the culprit, but I have no idea how to find it. What's going wrong?
The computer that behaves strangely is Mac OS X 10_11_3 running R 3.2.2 via RStudio 0.99.489
Sorry I am late, but the problem is that the function alpha is masked form ggplot2, that's why if you restart R and happen to call alpha after sourcing psych but before ggplot2 it works.
Anyway, the way to solve it is to make explicit that we want the psych function:
reliability = psych::alpha(df)
Restarting R was sufficient to fix the weird behavior. Obviously should have tried that before posting...

Store output from gridExtra::grid.arrange into an object

I am placing multiple plots into one image using gridExtra::grid.arrange and would like to have the option of saving the combined plot as an object that could be returned from within a function as part of a list of returned objects. Ideally, I would like to do this without printing the plot object.
The code below creates two plots, combines them with grid.arrange, and attempts to save the result into x. However, x evaluates to NULL and the plot is printed. The documentation for grid.arrange points me to arrangeGrob and suggests plotting can be turned off using plot=FALSE, but I get an error when I try that because FALSE is not a grob object.
Any suggestions for what I'm not understanding?
# R under development
# Windows 7 (32 bit)
# ggplot2 1.0.0
# gridExtra 0.9.1
p1 <- ggplot(mtcars, aes(x=factor(cyl), y=mpg)) + geom_boxplot()
p2 <- ggplot(mtcars, aes(x=factor(cyl), y=wt)) + geom_boxplot()
x <- gridExtra::grid.arrange(p1, p2)
x
Per the comments, I'm adding this edit. When I try it with arrangeGrob, I get no output at all.
> gridExtra::arrangeGrob(p1, p2)
> print(gridExtra::arrangeGrob(p1, p2))
Error: No layers in plot
> x <- gridExtra::arrangeGrob(p1, p2)
> x
Error: No layers in plot
The code in your edit does not work properly since you didn't load gridExtra.
library(gridExtra)
y <- arrangeGrob(p1, p2, ncol = 1)
class(y)
#[1] "gtable" "grob" "gDesc"
grid.draw(y)
Edit: since version 2.0.0, my comment about grid dependency below is no longer valid, since grid is now imported.
Edit: With gridExtra version >= 2.0.0, there is no need to attach either package,
p <- ggplot2::qplot(1,1)
x <- gridExtra::arrangeGrob(p, p)
grid::grid.draw(x)
Funny that this was asked so recently - I was running into this problem as well this week and was able to solve it in a bit of a hacky way, but I couldn't find any other solution I was happier with.
Problem 1: ggplotGrob is not found
I had to make sure ggplot2 is loaded. I don't completely understand what's happening (I admit I don't fully understand imports/depends/attaching/etc), but the following fixes that. I'd be open to feedback if this is very dangerous.
if (!"package:ggplot2" %in% search()) {
suppressPackageStartupMessages(attachNamespace("ggplot2"))
on.exit(detach("package:ggplot2"))
}
Somebody else linked to this blog post and I think that works as well, but from my (non-complete) understanding, this solution is less horrible. I think.
Problem 2: no layers in plot
As you discovered too, fixing that problem allows us to use grid.arrange, but that returns NULL and doesn't allow saving to an object. So I also wanted to use arrangeGrob but I also ran into the above error when gridExtra was not already loaded. Applying the fix from problem 1 again doesn't seem to work (maybe the package is getting de-attached too early?). BUT I noticed that calling grid::grid.draw on the result of arrangeGrob prints it fine without error. So I added a custom class to the output of arrangeGrob and added a generic print method that simply calls grid.draw
f <- function() {
plot <- gridExtra::arrangeGrob(...)
class(plot) <- c("ggExtraPlot", class(plot))
plot
}
print.ggExtraPlot <- function(x, ...) {
grid::grid.draw(x)
}
Hooray, now I can open a fresh R session with no packages explicitly loaded, and I can successfully call a function that creates a grob and print it later!
You can see the code in action in my package on GitHub.

Resources