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

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.

Related

ggplot graphs appearing empty when using Rmarkdown

I am having problems using ggplot with Rmarkdown.
I have a code that is supposed to return a ggplot graph.When I execute the code in a R session it works but the same code returns an empty plot when using R markdown.This is the code I am using in the R session
m<-ggplot(data=t,aes_string(x=v,y =t$cnt_pct))
m+geom_bar(stat = "identity",fill="#FF0000",color='black')+geom_text(aes(label = cnt_pct),vjust=-0.2)+ggtitle(paste(v,paste("graph")))+theme(plot.title = element_text(hjust = 0.5))+ylab("percent")+geom_line(aes_string(x=v,y =t$keshel_pct),group=1,size=2)
This is the code I am using in the Rmarkdown
m<-ggplot(data=t,aes_string(x=v,y =t$cnt_pct))
m+geom_bar(stat = "identity",fill="#FF0000",color='black')+geom_text(aes(label = cnt_pct),vjust=-0.2)+ggtitle(paste(v,paste("graph")))+theme(plot.title = element_text(hjust = 0.5))+ylab("percent")+geom_line(aes_string(x=v,y =t$keshel_pct),group=1,size=2)
plot(m)
The only diffrence between the two is that with the Rmarkwond a plot(m) line is added.
For some reason the plot command makes the graph empty, when I remove it and run the Rmarkdown no graphs are returned.
This is the graph empty
This is the graph working in a session of R
Anybody has any idea what is my mistake?
Any help will be appriciated
You have a few issues here:
1) your plot syntax has issues. aes_string takes strings referring to column names, but you're passing a variable v for x and direct column data t$cnt_pnt for y. Check some ggplot examples to get the syntax right.
2) The plot(m) statement shouldn't work for two reasons: first, you print ggplot objects, not plot them. Second, you are not assigning the m + geom_bar(...) statement to anything, so even if your plot statement did work, you'd get the plot without the geom_bar(...) stuff. Try e.g. y = m+ geom_bar(...); print(y) instead.

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 all the bar graphs for a dataset in pdf from R [duplicate]

I'd like to spawn several graphics windows from within a function in R using ggplot graphics...
testf <- function(a, b) {
devAskNewPage(TRUE)
qplot(a, b);
# grid.newpage(recording = TRUE)
dev.new()
qplot(a, a+a);
# grid.newpage(recording = TRUE)
dev.new()
qplot(b, b+b);
}
library(ggplot2)
x <- rnorm(50)
y <- rnorm(50)
testf(x, y)
However, neither dev.new() nor grid.newpage() seems to flush the preceding plot.
I know that, in R, functions normally only produce the last thing they evaluate, but I'd like to understand the process better and to learn of any possible workarounds.
Thoughts?
The grid-based graphics functions in lattice and ggplot2 create a graph object, but do not display it. The print() method for the graph object produces the actual display, i.e.,
print(qplot(x, y))
solves the problem.
See R FAQ 7.22.

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.

Generate multiple graphics from within an R function

I'd like to spawn several graphics windows from within a function in R using ggplot graphics...
testf <- function(a, b) {
devAskNewPage(TRUE)
qplot(a, b);
# grid.newpage(recording = TRUE)
dev.new()
qplot(a, a+a);
# grid.newpage(recording = TRUE)
dev.new()
qplot(b, b+b);
}
library(ggplot2)
x <- rnorm(50)
y <- rnorm(50)
testf(x, y)
However, neither dev.new() nor grid.newpage() seems to flush the preceding plot.
I know that, in R, functions normally only produce the last thing they evaluate, but I'd like to understand the process better and to learn of any possible workarounds.
Thoughts?
The grid-based graphics functions in lattice and ggplot2 create a graph object, but do not display it. The print() method for the graph object produces the actual display, i.e.,
print(qplot(x, y))
solves the problem.
See R FAQ 7.22.

Resources