I use
frame <- read.table(paste('data', fname, sep="/"), sep=",", header=TRUE)
colnames(frame) <- c("pos", "word.length")
plot <- ggplot(frame, aes(x=pos, y=word.length)) + xlim(0,20) + ylim(0,20) + geom_density2d() + stat_density2d(aes(color=..level..))
png(paste("graphs/", fname, ".png", sep=""), width=600, height=600)
print(plot)
dev.off()
to create plots, but they get cut off. How do I fix this?
http://ompldr.org/vZTN0eQ
The data I used to create this plot: http://sprunge.us/gKiL
According to the ggplot2 book, you use scale_x_continuous(limits=c(1,20)) instead of xlim(1,20) for that.
Related
This question already has an answer here:
Generate multiple graphics from within an R function
(1 answer)
Closed 3 years ago.
I need to make a bunch of individual plots and want to accomplish this in a for loop. I am using ggplot2. I would just use the facet option if it could save each graph in a separate file, which I don't think it can do.
There is something going on because the plots are not saved into the files. The files are generated, though, but are empty. Here is an idea of what my code looks like:
for(i in 1:15) {
pdf(paste("path/plot", i, ".pdf", sep=""), width=4, height=4)
abc <- ggplot(data[data[,3]==i,],
aes(variable, value, group=Name, color=Name)) +
geom_point(alpha=.6, size=3)+geom_line() +
theme(legend.position="none", axis.text.x = element_text(angle = -330)) +
geom_text(aes(label=Name),hjust=0, vjust=0, size=2.5) +
ggtitle("Title")
abc
dev.off()
}
How can I save the plots into these files?
Note that if I has a numeric value and I run the code inside the for loop, everything works.
When I use print it works:
for(i in 1:15) {
pdf(paste("plot", i, ".pdf", sep=""), width=4, height=4)
abc <- ggplot(mtcars, aes(cyl, disp)) +
geom_point(alpha=.6, size=3)
print(abc)
dev.off()
}
Or try ggsave:
for(i in 1:15) {
Filename <- paste("plot", i, ".pdf", sep="")
abc <- ggplot(mtcars, aes(cyl, disp)) +
geom_point(alpha=.6, size=3)
ggsave(filename = Filename, abc, width=4, height=4)
}
I drew two panels in a column using ggplot2 facet, and would like to add two vertical lines across the panels at x = 4 and 8. The following is the code:
library(ggplot2)
library(gtable)
library(grid)
dat <- data.frame(x=rep(1:10,2),y=1:20+rnorm(20),z=c(rep("A",10),rep("B",10)))
P <- ggplot(dat,aes(x,y)) + geom_point() + facet_grid(z~.) + xlim(0,10)
Pb <- ggplot_build(P);Pg <- ggplot_gtable(Pb)
for (i in c(4,8)){
Pg <- gtable_add_grob(Pg, moveToGrob(i/10,0),t=8,l=4)
Pg <- gtable_add_grob(Pg, lineToGrob(i/10,1),t=6,l=4)
}
Pg$layout$clip <- "off"
grid.newpage()
grid.draw(Pg)
The above code is modified from:ggplot, drawing line between points across facets.
And .
There are two problems in this figure. First, only one vertical line was shown. It seems that moveToGrob only worked once.. Second, the shown line is not exact at x = 4. I didn't find the Pb$panel$ranges variable, so is there a way that I can correct the range as well? Thanks a lot.
Updated to ggplot2 V3.0.0
In the simple scenario where panels have common axes and the lines extend across the full y range you can draw lines over the whole gtable cells, having found the correct npc coordinates conversion (cf previous post, updated because ggplot2 keeps changing),
library(ggplot2)
library(gtable)
library(grid)
dat <- data.frame(x=rep(1:10,2),y=1:20+rnorm(20),z=c(rep("A",10),rep("B",10)))
p <- ggplot(dat,aes(x,y)) + geom_point() + facet_grid(z~.) + xlim(0,10)
pb <- ggplot_build(p)
pg <- ggplot_gtable(pb)
data2npc <- function(x, panel = 1L, axis = "x") {
range <- pb$layout$panel_params[[panel]][[paste0(axis,".range")]]
scales::rescale(c(range, x), c(0,1))[-c(1,2)]
}
start <- sapply(c(4,8), data2npc, panel=1, axis="x")
pg <- gtable_add_grob(pg, segmentsGrob(x0=start, x1=start, y0=0, y1=1, gp=gpar(lty=2)), t=7, b=9, l=5)
grid.newpage()
grid.draw(pg)
You can just use geom_vline and avoid the grid mess altogether:
ggplot(dat, aes(x, y)) +
geom_point() +
geom_vline(xintercept = c(4, 8)) +
facet_grid(z ~ .) +
xlim(0, 10)
I'm pretty happy with the results I'm getting with R. Most of my stacked histogram plots are looking fine, e.g.
and
However, I have a few that have so many categories in the legend that the legend is crowing out the plot, e.g.
How can I fix this?
Here is my plot.r, which I call on the command line like this
RScript plot.r foo.dat foo.png 1600 800
foo.dat
account,operation,call_count,day
cal3510,foo-method,1,2016-10-01
cra4617,foo-method,1,2016-10-03
cus4404,foo-method,1,2016-10-03
hin4510,foo-method,1,2016-10-03
mas4484,foo-method,1,2016-10-04
...
entirety of foo.dat: http://pastebin.com/xnJtJSrU
plot.r
library(ggplot2)
library(scales)
args<-commandArgs(TRUE)
filename<-args[1]
png_filename<-args[2]
wide<-as.numeric(args[3])
high<-as.numeric(args[4])
print(wide)
print(high)
print(filename)
print(png_filename)
dat = read.csv(filename)
dat$account = as.character(dat$account)
dat$operation = as.character(dat$operation)
dat$call_count = as.integer(dat$call_count)
dat$day = as.Date(dat$day)
png(png_filename,width=wide,height=high)
p <- ggplot(dat, aes(x=day, y=call_count, fill=account))
p <- p + geom_histogram(stat="identity")
p <- p + scale_x_date(labels=date_format("%b-%Y"), limits=as.Date(c('2016-10-01','2017-01-01')))
print(p)
dev.off()
Answer from #PierreLafortune
using:
p <- p + theme(legend.position="bottom")
p <- p + guides(fill=guide_legend(nrow=5, byrow=TRUE))
This question already has an answer here:
Generate multiple graphics from within an R function
(1 answer)
Closed 3 years ago.
I need to make a bunch of individual plots and want to accomplish this in a for loop. I am using ggplot2. I would just use the facet option if it could save each graph in a separate file, which I don't think it can do.
There is something going on because the plots are not saved into the files. The files are generated, though, but are empty. Here is an idea of what my code looks like:
for(i in 1:15) {
pdf(paste("path/plot", i, ".pdf", sep=""), width=4, height=4)
abc <- ggplot(data[data[,3]==i,],
aes(variable, value, group=Name, color=Name)) +
geom_point(alpha=.6, size=3)+geom_line() +
theme(legend.position="none", axis.text.x = element_text(angle = -330)) +
geom_text(aes(label=Name),hjust=0, vjust=0, size=2.5) +
ggtitle("Title")
abc
dev.off()
}
How can I save the plots into these files?
Note that if I has a numeric value and I run the code inside the for loop, everything works.
When I use print it works:
for(i in 1:15) {
pdf(paste("plot", i, ".pdf", sep=""), width=4, height=4)
abc <- ggplot(mtcars, aes(cyl, disp)) +
geom_point(alpha=.6, size=3)
print(abc)
dev.off()
}
Or try ggsave:
for(i in 1:15) {
Filename <- paste("plot", i, ".pdf", sep="")
abc <- ggplot(mtcars, aes(cyl, disp)) +
geom_point(alpha=.6, size=3)
ggsave(filename = Filename, abc, width=4, height=4)
}
I am trying to make a bar plot that has extra data on it. Associated with each data point is a value from a factor that indicates why the height is what it is. So far I'm reasonably happy with my results:
library(ggplot2)
tab <- read.table("http://www.cs.colorado.edu/~coxaj/table2.csv",
header=T, sep=",", strip.white=T)
tab <- with(tab, tab[order(Analysis, -as.numeric(Analysis)), ])
bar_width <- 0.5
space_width <- 0.8
p <- ggplot(tab, aes(x=Filter,y=Depth,fill=Analysis)) +
geom_bar(position=position_dodge(width=space_width), width=bar_width) +
geom_point(position=position_dodge(width=space_width), aes(shape=Termination)) +
scale_shape_manual(values=c(1,4,5,6)) +
geom_hline(aes(yintercept=16, linetype=2)) +
scale_x_discrete(name='') +
scale_y_continuous(name='Search Depth') +
scale_fill_manual(values=c("#E66101", "#FDB863", "#B2ABD2", "#5E3C99")) +
theme_bw()
ggsave(filename='table2.pdf', height=3, width=8)
This produces a plot that looks like this:
The problem is that it puts these pointless circles in the legend for Analysis. I would like to remove that circle, but keep the legend. Does ggplot2 let me do this?
try this:
p <- ggplot(tab, aes(x=Filter,y=Depth)) +
geom_bar(aes(fill = Analysis),
position=position_dodge(width=space_width), width=bar_width) +
geom_point(position=position_dodge(width=space_width),
mapping = aes(group = Analysis, shape=Termination)) +
...