R: "Error in facet_grid" - "unused argument" - r

I am trying to plot some data from my experiment in R using ggplot2, and I am trying to split the graph in two parts using facet_grid().
Here is an MWE I built with the cars dataset:
data(mtcars)
ggplot(data=mtcars, aes(x=mtcars$mpg,y=mtcars$cyl)) +
geom_point()+
facet_grid(rows=mtcars$disp)
I get the following error:
Error in facet_grid(rows = mtcars$disp) :
unused argument (rows = mtcars$disp)
I really have no idea why this is happening. I used this function before, and it worked fine. Would appreciate ideas on how to solve this.
edit:
I accepted the second answer, because it provides some more context, but as I see it, both are equally correct in pointing out that I need to quote the variable name. The actual error was resolved after installig R and all packages again. Now I have a new error, but that is another story. Thanks again!

First, don't explicitly refer to mtcars within the aes() call.
Second, quote the facet argument.
library(ggplot2)
ggplot(data=mtcars, aes(x=mpg,y=cyl)) +
geom_point()+
facet_grid(rows="disp")
Also, consider creating a new variable that collapses disp into fewer values for the facet to be more meaningful & readable.
Here's an example of four arbitrary cut points.
mtcars$disp_cut_4 <- cut(mtcars$disp, breaks=c(0, 200, 300, 400, 500))
ggplot(data=mtcars, aes(x=mpg,y=cyl)) +
geom_point()+
facet_grid(rows="disp_cut_4")

This should do:
ggplot(data=mtcars, aes(mpg, cyl)) +
geom_point()+
facet_grid(rows = "disp")
alternatively:
ggplot(data=mtcars, aes(mpg, cyl)) +
geom_point()+
facet_grid(~disp)

Related

Facetting with factorised variables and geom_hline / geom_vline

Consider this code:
require(ggplot2)
ggplot(data = mtcars) +
geom_point(aes(x = drat, y = wt)) +
geom_hline(yintercept = 3) +
facet_grid(~ cyl) ## works
ggplot(data = mtcars) +
geom_point(aes(x = drat, y = wt)) +
geom_hline(yintercept = 3) +
facet_grid(~ factor(cyl)) ## does not work
# Error in factor(cyl) : object 'cyl' not found
# removing geom_hline: works again.
Google helped me to find a debug, namely wrapping intercept into aes
ggplot(data = mtcars) +
geom_point(aes(x = drat, y = wt)) +
geom_hline(aes(yintercept = 3)) +
facet_grid(~ factor(cyl)) # works
# R version 3.4.3 (2017-11-30)
# ggplot2_2.2.1
Hadley writes here that functions as variables need to be in every layer. (which sounds mysterious to me)
Why does this happen when factorising the facet variable?
So here's my best guess and explanation.
When Hadley says:
This is a known limitation of facetting with a function - the variables you use have to be present on every layer.
He means in ggplot, when you're going to use a function in the facetting function, you need to have the variable in every geom. The issue occurs because there cyl variable is not present in the hline geom.
It's important to remember, this is a limitation, not ideal behaviour. Moreso, a consequence of how their efficient code works, is that when using functions to facet, the variables must be present in every geom.
Without looking into the specifics of the ggplot2 functions, I'm guessing what wrapping aes around the yintercept argument does, is give an aesthetic mapping to the geom_hline function. The aes function maps variables to components of the plot, rather than static values. It's an important distinction. Even though we still set yintercept = 3, the fact that we have placed it in the aesthetic mapping, must somehow reference that cyl also exists in this space. That is, it connects geom_hline indirectly with cyl, meaning it's now in the layer, and no longer a limitation.
This may not be an entirely satisfying answer, but without reading over the ggplot2 code to try and work out specifically why this limitation occurs, this might be as good as you'll get for now. Hopefully one of these workarounds is sufficient for you :)

geom_smooth does not plot line of best fit

I hope this question isn't a duplicate. I tried to find answers per the site's requirements before posting, but since I am so new, the help forums are too foreign to me.
Following Wickham's R for data visualization, I easily used geom_point for an integrated data set, mpg:
simple reference code:
ggplot(data = mpg)+
geom_smooth(mapping = aes(x=displ, y=hwy))+
geom_point(mapping = aes(x=displ, y=hwy))
Excited by this cool plot, I tried to do the same for some personal research data, which describes inteferon-beta production over five time points (A,b,c,d,e instead of numerical data).
I used the same code, essentially:
ggplot(data = ifnonly)+
geom_smooth(mapping = aes(x=HOURS, y=IFNB))+
geom_point(mapping = aes(x=HOURS, y=IFNB))
Unfortunately, the line does not display. In fact, nothing displays until I add the geom_point function. What am I missing here? Is there more complex code required or is there some subtlety that I can apply to future uses of this function and ggplot?
I think you should get your desired output with following one line code
library(ggplot2)
ggplot(mtcars, aes(disp,mpg))+geom_smooth() # one line code where I have mentioned data is mtcars , and disp as x axis and mpg as y axis you could get following output
# please check this link for output
o/p without geom_point
library(ggplot2)
ggplot(mtcars, aes(disp,mpg))+geom_smooth()+geom_point()
o/p with geom_point

Plotting multiple individual correlation plots using ggplot

That is gonna be a very basic and naive question, but as my R and programming skills are very limited, I have no idea how to solve it. I would really appreciate if you guys could help me on this.
I want to plot multiple correlation plots comparing a fixed x-axis (Sepal.Length, in the example below) with each column on my dataset as y-axis (Sepal.Width, Petal.Length and Petal.Width). I suspect I might need to use apply, but I don't know how to build it in a function.
Right now I am able to do it manually one by one, but that is not helpful at all. Bellow, I am sharing the piece of the code I would like to apply to every column in my dataset.
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
geom_smooth(aes(group = 1), method=lm,) +
geom_point(size=4, shape=20, alpha=0.6) + theme(legend.position="none") +
annotate(x=min(iris$Sepal.Width),y=min(iris$Sepal.Width),hjust=.2,
label=paste("R = ", round(cor(iris$Sepal.Width, iris$Sepal.Width),2)),
geom="text", size=4)
After generating all plots my idea is plot all of them side by side using grid.arrange package.
Are you looking something like this?
library(tidyr)
library(dplyr)
library(ggplot2)
iris %>% select(-Species) %>%
gather(YCol, YValue, -Sepal.Length) %>%
ggplot(aes(x=Sepal.Length, y=YValue)) +
geom_point() +
facet_grid(YCol~.)
It contains same Y-axis but if you do not want, then you could use scales="free_y".

r Error: Don't know how to add o to a plot

I am trying to add arrow heads to a line created by geom_line() in ggplot.
Example:
library(grid)
library(ggplot2)
df <- data.frame(x=(1:2),y=(2:1))
ggplot(df,aes(x,y)) +
geom_line() +
arrow()
The error I get is "Error: Don't know how to add o to a plot".
I found a variety of posts with this error, but they had a complex scenario and the answers called for making what looked like subtle changes in the code.
If instead I run:
ggplot(data=cinterval,aes(x=x,y=y))+
geom_line()
I get the expected line.
When I run the expanded code, with a more elaborate plot, I get the error
"Error in as.vector(y) : attempt to apply non-function"
Do this instead
library(grid)
library(ggplot2)
df <- data.frame(x=(1:2),y=(2:1))
ggplot(df,aes(x,y)) +
geom_line(arrow = arrow(length=unit(0.30,"cm"), ends="first", type ="closed"))

ggplot facet_grid label superscript

I am having trouble with putting subscript in facet_grid label. Here is
an example of the work I have been trying to do.
df <- data.frame(species=gl(2,10,labels=c('sp1','sp2')),
age=sample(3:12,40,replace=T),
variable=gl(2,20,labels=c('N1P1 var','N2P1 var')),
value=rnorm(40))
test.plot <- ggplot(data=df,aes(x=age,y=value)) +
geom_point() +
facet_grid(variable~species)
Now I want to make by vertical facet label as 'N[1]P[1] var' and so on,
where the numbers in the squared bracket means subscript.
I have consulted some helps in this platform regarding this, but none helped me. I have used expression, bquote as suggested, but nothing worked!
You need to do 2 things:
first, make your labels as plotmath expressions
variable_labels <-
c(expression(paste(N[1],P[1]~var)), expression(paste(N[2],P[1]~var)))
df <- data.frame(species=gl(2,10,labels=c('sp1','sp2')),
age=sample(3:12,40,replace=T),
variable=gl(2,20,labels=variable_labels),
value=rnorm(40))
And then change the default labeller function in facet_grid to "label_parsed"
test.plot <- ggplot(data=df,aes(x=age,y=value)) +
geom_point() +
facet_grid(variable~species, labeller = "label_parsed")

Resources