Error when using "panel.ellipse" function in R - r

I want to draw a confidence ellipse. I search the R document and find the function: panel.ellipse. Here is the description website
Then I tried. I used the code below:
library(corrgram)
a<-c(1,2,3,4,5)
b<-c(2,4,6,5,3)
panel.ellipse(a, b)
But an error occur:
Error in plot.xy(xy.coords(x, y), type = type, ...) :
plot.new has not been called yet
I didn't call "plot.new", why did R say that?

You're linking to the latticeExtra::panel.ellipse function in the description link, but seem to be using corrgram which also has a panel.ellipse function. So I'm not sure which panel.ellipse function you are using/want to use.
From ?corrgram::panel.ellipse:
# CAUTION: The latticeExtra package also has a 'panel.ellipse' function
# that clashes with the same-named function in corrgram. In order to us
# the right one, the example below uses 'lower.panel=corrgram::panel.ellipse'.
# If you do not have latticeExtra loaded, you can just use
# 'lower.panel=panel.ellipse'.
Why not use ggplot2::stat_ellipse instead?
# Your sample data
a<-c(1,2,3,4,5)
b<-c(2,4,6,5,3)
df <- cbind.data.frame(a, b);
# Use stat_ellipse to draw confidence ellipse
require(ggplot2);
ggplot(df, aes(a, b)) + geom_point() + stat_ellipse();

Related

Specifying the `stratum` and `alluvium` parameters without attaching ggalluvial

I use ggalluvial with ggplot2, though, I'd like to be able to generate the same plot without attaching ggalluvial but only specify its use with ggalluvial::. If it is not attached, I get the following error: Error: Can't find stat called "stratum".
d <- data.frame(
status = rep(c("state1","state2","state3"), rep(90, times=3)),
cellIndex = rep(seq_len(90), times=3),
cellCategory = c(rep(letters[seq_len(3)], each=30),
rep(letters[c(2,3,1)], each=30),
rep(letters[c(3,1,2)], each=30))
)
ggplot2::ggplot(data=d, ggplot2::aes(x=status, stratum=cellCategory, alluvium=cellIndex,
fill=cellCategory, label=cellCategory)) +
ggalluvial::geom_flow(stat="alluvium", lode.guidance="rightleft", color="darkgray") +
ggalluvial::geom_stratum() +
ggplot2::geom_text(stat="stratum", size=3)
This was a tough one---digging into the code for ggplot2, the stat argument pastes the string you give and then looks for that object (in this case "StatStratum") in the environment you're in. Because you don't want to load the package, it won't be able to find it (and there's no way to change the argument itself).
Answer
So you need to save that object from the ggalluvial package like so:
StatStratum <- ggalluvial::StatStratum
Then leave the rest of your code as is.
The following worked for me.
ggplot2::geom_text(stat = ggalluvial::StatStratum)

levelplot() not rendering png from commandline [duplicate]

I started using the lattice graphic package but I stumbled into a problem. I hope somebody can help me out.
I want to plot a histogram using the corresponding function.
Here is the file foo.r:
library("lattice")
data <- data.frame(c(1:2),c(2:3))
colnames(data) <- c("RT", "Type")
pdf("/tmp/baz.pdf")
histogram( ~ RT | factor(Type), data = data)
dev.off()
When I run this code using R --vanilla < foo.r it works all fine.
However, if I use a second file bar.r with
source("bar")
and run R --vanilla < bar.r the code produces an erroneous pdf file.
Now I found out that source("bar", echo=TRUE) solves the problem. What is going on here? Is this a bug or am I missing something?
I'm using R version 2.13.1 (2011-07-08) with lattice_0.19-30
It is in the FAQ for R -- you need print() around the lattice function you call:
7.22 Why do lattice/trellis graphics not work?
The most likely reason is that you forgot to tell R to display the
graph. Lattice functions such as xyplot() create a graph object, but
do not display it (the same is true of ggplot2 graphics, and Trellis
graphics in S-Plus). The print() method for the graph object produces
the actual display. When you use these functions interactively at the
command line, the result is automatically printed, but in source() or
inside your own functions you will need an explicit print() statement.
Example of the case
visualise.r
calls plot2this.r
calls ggplot2 and returns p object
Here the fix in the function plot2this.r from return(p) to return(print(p)).
Initial plot2this.r
p <- ggplot(dat.m, aes(x = Vars, y = value, fill=variable))
return(p)
Fix
p <- ggplot(dat.m, aes(x = Vars, y = value, fill=variable))
return(print(p))
Output now: expected output with the wanted plot.

Saving plots to working directory - plots are blank? [duplicate]

I started using the lattice graphic package but I stumbled into a problem. I hope somebody can help me out.
I want to plot a histogram using the corresponding function.
Here is the file foo.r:
library("lattice")
data <- data.frame(c(1:2),c(2:3))
colnames(data) <- c("RT", "Type")
pdf("/tmp/baz.pdf")
histogram( ~ RT | factor(Type), data = data)
dev.off()
When I run this code using R --vanilla < foo.r it works all fine.
However, if I use a second file bar.r with
source("bar")
and run R --vanilla < bar.r the code produces an erroneous pdf file.
Now I found out that source("bar", echo=TRUE) solves the problem. What is going on here? Is this a bug or am I missing something?
I'm using R version 2.13.1 (2011-07-08) with lattice_0.19-30
It is in the FAQ for R -- you need print() around the lattice function you call:
7.22 Why do lattice/trellis graphics not work?
The most likely reason is that you forgot to tell R to display the
graph. Lattice functions such as xyplot() create a graph object, but
do not display it (the same is true of ggplot2 graphics, and Trellis
graphics in S-Plus). The print() method for the graph object produces
the actual display. When you use these functions interactively at the
command line, the result is automatically printed, but in source() or
inside your own functions you will need an explicit print() statement.
Example of the case
visualise.r
calls plot2this.r
calls ggplot2 and returns p object
Here the fix in the function plot2this.r from return(p) to return(print(p)).
Initial plot2this.r
p <- ggplot(dat.m, aes(x = Vars, y = value, fill=variable))
return(p)
Fix
p <- ggplot(dat.m, aes(x = Vars, y = value, fill=variable))
return(print(p))
Output now: expected output with the wanted plot.

Pesky ggplot2 namespace conflict when using ggplot2 in package

I'm contributing to the qmethod R package, and I just wrote a function that creates a bunch of ggplot2 objects.
The function works fine, but builds and R CMD Check warns me that:
replacing previous import by ‘ggplot2::%+%’ when loading ‘qmethod’
I've looked at SE posts and #hadley's book but can't figure out the problem.
Here's the relevant parts of my NAMESPACE:
import("ggplot2",
"stringr")
import("psych")
importFrom("plyr","count")
importFrom("reshape2","melt")
importFrom("digest", "digest")
importFrom("RColorBrewer", "brewer.pal")
And here's part of my DESCRIPTION:
Imports:
digest,
psych,
knitr,
RColorBrewer,
stringr,
ggplot2,
plyr,
reshape2
The part where I call a ggplot2 function inside my function array.viz.R looks like this (and more):
g <- ggplot(
data = array.viz.data
,aes(
x = fsc # factor scores, always same variable bc dataframe is constructed for every factor array by above loop
,y = ycoord # just the random ycoord for viz
,ymax = max(ycoord)
,ymin = 0
#,label = item.wrapped # this for some reason causes an error
)
)
g <- g + geom_tile( # add background tiles
aes(
fill = item.sd
)
)
Ps.: you can find the entire current work here: https://github.com/maxheld83/qmethod/tree/array-viz
Pps.: I am aware that ggplot2 itself imports a bunch of the functions I also import (such as reshape2), so I have a hunch that that might be a problem.
Turns out, import("psych") is the offending package.
It seems to somehow export again ggplot::%+%, though I can't think of why that would be the case.
Anyway, the fix is:
importFrom("psych", "principal") # that's the function we were using

R package lattice won't plot if run using source()

I started using the lattice graphic package but I stumbled into a problem. I hope somebody can help me out.
I want to plot a histogram using the corresponding function.
Here is the file foo.r:
library("lattice")
data <- data.frame(c(1:2),c(2:3))
colnames(data) <- c("RT", "Type")
pdf("/tmp/baz.pdf")
histogram( ~ RT | factor(Type), data = data)
dev.off()
When I run this code using R --vanilla < foo.r it works all fine.
However, if I use a second file bar.r with
source("bar")
and run R --vanilla < bar.r the code produces an erroneous pdf file.
Now I found out that source("bar", echo=TRUE) solves the problem. What is going on here? Is this a bug or am I missing something?
I'm using R version 2.13.1 (2011-07-08) with lattice_0.19-30
It is in the FAQ for R -- you need print() around the lattice function you call:
7.22 Why do lattice/trellis graphics not work?
The most likely reason is that you forgot to tell R to display the
graph. Lattice functions such as xyplot() create a graph object, but
do not display it (the same is true of ggplot2 graphics, and Trellis
graphics in S-Plus). The print() method for the graph object produces
the actual display. When you use these functions interactively at the
command line, the result is automatically printed, but in source() or
inside your own functions you will need an explicit print() statement.
Example of the case
visualise.r
calls plot2this.r
calls ggplot2 and returns p object
Here the fix in the function plot2this.r from return(p) to return(print(p)).
Initial plot2this.r
p <- ggplot(dat.m, aes(x = Vars, y = value, fill=variable))
return(p)
Fix
p <- ggplot(dat.m, aes(x = Vars, y = value, fill=variable))
return(print(p))
Output now: expected output with the wanted plot.

Resources