R plot not saving as png, please check my code - r

I have written code for a plot which is correct, yet I cannot save it as a png file. I used the png() function before I plotted and used dev.off() after and it won't save. I didn't include the reading of the data and creation of variables btw. (Mac)
I've tried everything, nothing works. Is there a way to save fullplot?
png(filename = "plot_3.png", width = 480, height = 480)
p1 <- (ggplot(PTsum, aes(x=Group.1, y =x))
+ geom_bar(stat="identity", color="black"
, fill="gray") + ggtitle("Point") +
xlab("Date") + ylab("Total Emission"))
p2 <- (ggplot(NPTsum, aes(x=Group.1, y =x))
+ geom_bar(stat="identity", color="black"
, fill="gray") + ggtitle("NonPoint") +
xlab("Date") + ylab("Total Emission"))
p3 <- (ggplot(Rsum, aes(x=Group.1, y =x))
+ geom_bar(stat="identity", color="black"
, fill="gray") + ggtitle("Road") +
xlab("Date") + ylab("Total Emission"))
p4 <- (ggplot(NRsum, aes(x=Group.1, y =x))
+ geom_bar(stat="identity", color="black"
, fill="gray") + ggtitle("NonRoad") +
xlab("Date") + ylab("Total Emission"))
fullplot <- multiplot(p1,p2,p3,p4,cols=2)
dev.off()
I expected a file called plot3.png but it wasn't there

Related

How to plot a lign spanning these two graphs?

Consider the following figure:
mainplot = ggplot(mtcars, aes(y=mpg,x=wt)) + geom_point() + theme_classic(15) + ylim(c(5,40)) + geom_hline(yintercept=c(15,25), color="red")
gg = ggplot(data.frame(mpg=0), aes(x=mpg))
f = function(mpg,center) {exp(-(mpg - center)^2/(20))}
f15 = function(mpg) {f(mpg,15)}
f25 = function(mpg) {f(mpg,25)}
sideplot = gg + stat_function(fun = f15, linetype="dashed") + stat_function(fun = f25, linetype="dashed") + theme_classic(15) + scale_x_continuous(name=NULL,limits=c(5,40)) + coord_flip() + ylab("f") + theme(axis.title.y=element_blank(),axis.text.y=element_blank(),axis.ticks.y=element_blank()) + geom_vline(xintercept=c(15,25), color="red")
multiplot(mainplot, sideplot, layout=matrix(c(1,1,1,2),nrow=1))
As the figure is made of two independent graphs, the red horizontal lines are interrupted. Is there any way I can make it a continuous line?
It is possible that the easiest solution consists at using Adobe Illustrator (or some equivalent) to modify the figure.
Not really a solution but a work around.
Reduce the margins to stick the two graphs together
Make your line a dashed line
Remove the y axis line of the sideplot
mainplot = ggplot(mtcars, aes(y=mpg,x=wt)) + geom_point() + theme_classic(15) + ylim(c(5,40)) + geom_hline(yintercept=c(15,25), color="red", linetype="dashed") + theme(plot.margin = unit(c(1,0,1,1), "cm"))
sideplot = gg + stat_function(fun = f15, linetype="dashed") + stat_function(fun = f25, linetype="dashed") + theme_classic(15) + scale_x_continuous(name=NULL,limits=c(5,40)) + coord_flip() + ylab("f") + theme(axis.line.y=element_blank(),axis.title.y=element_blank(),axis.text.y=element_blank(),axis.ticks.y=element_blank()) + geom_vline(xintercept=c(15,25), color="red", linetype="dashed") + theme(plot.margin = unit(c(1,1,1,0), "cm"))
multiplot(mainplot, sideplot, layout=matrix(c(1,1,1,2),nrow=1))

Displaying the number of counts per bin in a ggplot2 hexbin graph

I'm working on a project to simulate the movement of missing ships. I've made a distribution map using this code:
library("ggplot2")
a <- rnorm(1000, 30.2, 2)
b <- rnorm(1000, 10, 5)
y <- (x + a + b) * 0.6
df <- data.frame(x,y)
p <- ggplot(df,aes(x=x,y=y)) +
ggtitle("A Priori Map") + xlab("Longtitude") + ylab("Latitude") +
scale_fill_gradientn(colors = topo.colors(10))
p + stat_binhex(show.legend = T, bins = 20)
This produces a map like this:
A hexbin map
However, instead of showing the number of counts using a color, I would like to show the actual count in a point. So if the program 'landed' on a certain point 3 times, it would display '3'.
How can this be done in R?
Here's how to add counts to the existing graph:
library(ggplot2)
theme_set(theme_bw())
set.seed(2)
a <- rnorm(1000, 30.2, 2)
b <- rnorm(1000, 10, 5)
x = rnorm(1000)
y <- (x + a + b) * 0.6
df <- data.frame(x,y)
p <- ggplot(df,aes(x=x,y=y)) +
ggtitle("A Priori Map") +
xlab("Longtitude") + ylab("Latitude") +
scale_fill_gradientn(colors = topo.colors(10)) +
stat_binhex(show.legend = T, bins = 20)
p + geom_text(stat="binhex", bins=20, aes(label=..count..), show.legend=FALSE,
colour=hcl(15,100,60), fontface="bold", size=3.5)
To remove the fill colours, you could do:
ggplot(df,aes(x=x,y=y)) +
ggtitle("A Priori Map") +
xlab("Longtitude") + ylab("Latitude") +
stat_binhex(bins = 20, fill=NA, colour="black") +
geom_text(stat="binhex", bins=20, aes(label=..count..), colour="red")
You could also use text size to highlight the regions of highest density:
ggplot(df,aes(x=x,y=y)) +
ggtitle("A Priori Map") +
xlab("Longtitude") + ylab("Latitude") +
stat_binhex(show.legend = T, bins = 20, fill=NA, colour="grey70") +
geom_text(stat="binhex", bins=20, aes(label=..count.., size=..count..), colour="red") +
scale_size_continuous(range=c(3,6)) +
guides(size=FALSE)
Which also works without the hex-grid:
ggplot(df,aes(x=x,y=y)) +
ggtitle("A Priori Map") +
xlab("Longtitude") + ylab("Latitude") +
geom_text(stat="binhex", bins=20, aes(label=..count.., size=..count..), colour="red") +
scale_size_continuous(range=c(3,6)) +
guides(size=FALSE)

R - Background colour on ggplot split across a diagonal?

Using R, library(ggplot2)
I have a ggplot that is a perfect square. From the top left corner, to the bottom right corner, I have a line.
So say xlim(0,100), ylim(0,100) with geom_abline(intercept=100, slope=-1).
How do I make everything above the line have say a blue background and below a red?
library(ggplot2)
dat = data.frame(x=c(0,100), ymin=0, y=c(100,0), ymax=100)
ggplot(dat) +
geom_ribbon(aes(x, ymin=y, ymax=ymax), fill="blue") +
geom_ribbon(aes(x, ymin=ymin, ymax=y), fill="red") +
geom_abline(aes(intercept=100, slope=-1)) +
#geom_line(aes(x,y), lwd=1) + # If you just want a line with the same extent as the data
theme_bw()
And analogously for more general curves:
x=seq(0,100,0.1)
dat = data.frame(x=x, ymin=0, y=2*x + 3*x^2 - 0.025*x^3)
dat$ymax=max(dat$y)
ggplot(dat) +
geom_ribbon(aes(x, ymin=y, ymax=ymax), fill="blue") +
geom_ribbon(aes(x, ymin=ymin, ymax=y), fill="red") +
geom_line(aes(x,y), colour="yellow", lwd=1) +
theme_bw()

Draw lines between two facets in ggplot2

How can I draw several lines between two facets?
I attempted this by plotting points at the min value of the top graph but they are not between the two facets. See picture below.
This is my code so far:
t <- seq(1:1000)
y1 <- rexp(1000)
y2 <- cumsum(y1)
z <- rep(NA, length(t))
z[100:200] <- 1
df <- data.frame(t=t, values=c(y2,y1), type=rep(c("Bytes","Changes"), each=1000))
points <- data.frame(x=c(10:200,300:350), y=min(y2), type=rep("Bytes",242))
vline.data <- data.frame(type = c("Bytes","Bytes","Changes","Changes"), vl=c(1,5,20,5))
g <- ggplot(data=df, aes(x=t, y=values)) +
geom_line(colour=I("black")) +
facet_grid(type ~ ., scales="free") +
scale_y_continuous(trans="log10") +
ylab("Log values") +
theme(axis.text.x = element_text(angle = 90, hjust = 1), panel.margin = unit(0, "lines"))+
geom_point(data=points, aes(x = x, y = y), colour="green")
g
In order to achieve that, you have to set the margins inside the plot to zero. You can do that with expand=c(0,0). The changes I made to your code:
When you use scale_y_continuous, you can define the axis label inside that part and you don't need a seperarate ylab.
Changed colour=I("black") to colour="black" inside geom_line.
Added expand=c(0,0) to scale_x_continuous and scale_y_continuous.
The complete code:
ggplot(data=df, aes(x=t, y=values)) +
geom_line(colour="black") +
geom_point(data=points, aes(x = x, y = y), colour="green") +
facet_grid(type ~ ., scales="free") +
scale_x_continuous("t", expand=c(0,0)) +
scale_y_continuous("Log values", trans="log10", expand=c(0,0)) +
theme(axis.text.x=element_text(angle=90, vjust=0.5), panel.margin=unit(0, "lines"))
which gives:
Adding lines can also be done with geom_segment. Normally the lines (segments) will appear in both facets. If you want them to appear between the two facets, you will have to restrict that in data parameter:
ggplot(data=df, aes(x=t, y=values)) +
geom_line(colour="black") +
geom_segment(data=df[df$type=="Bytes",], aes(x=10, y=0, xend=200, yend=0), colour="green", size=2) +
geom_segment(data=df[df$type=="Bytes",], aes(x=300, y=0, xend=350, yend=0), colour="green", size=1) +
facet_grid(type ~ ., scales="free") +
scale_x_continuous("t", expand=c(0,0)) +
scale_y_continuous("Log values", trans="log10", expand=c(0,0)) +
theme(axis.text.x=element_text(angle=90, vjust=0.5), panel.margin=unit(0, "lines"))
which gives:

inserting custom text to ggplot2

I have a ggplot graph that I would like to insert custom string below 0 as "Within" and above 0 as "Breached".
I am doing this:
ggplot(z, aes(Date, Breach1/60, group=Jobs, label=c("Within SLA", "Breached SLA"))) +
geom_line(size=1) +
theme_bw() + ylab("Hours") + xlab("Date") + opts(title="Jobs") +
geom_hline(yintercept=0, color="red", size=2) + geom_text(hjust=0, vjust=3)
This seems to put text all over the place. I like to put one text above the zero and one text below the zero value. Any ideas?
You are after annotate:
ggplot(z, aes(Date, Breach1/60, group=Jobs)) +
geom_line(size=1) +
theme_bw() + ylab("Hours") + xlab("Date") + opts(title="Jobs") +
geom_hline(yintercept=0, color="red", size=2) +
annotate("text", label = "Within SLA", x = 1, y = 2) +
annotate("text", label = "Breached", x = 1, y = -2)

Resources