How to plot a lign spanning these two graphs? - r

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))

Related

How to remove blank from geom_line in ggplot2?

geom_line has big gap in line two sides, how to remove this gap!
R scripts is listed as follow,
p <- ggplot(singJanS)+ geom_line(aes(x=sn,y=diff))
p <- p + geom_hline(yintercept=seq(-0.8,0.8,by=0.4), linetype=2, colour="grey") +
geom_vline(xintercept=seq(15,744,by=24), linetype=6, colour="red") +
geom_vline(xintercept=seq(23,744,by=24), linetype=6, colour="blue") +
ylab(Delta~T~' ('~degree~C~')')+xlab("")+
scale_x_continuous(breaks = c(0,120,240,360,480,600,720))+
scale_y_continuous(breaks = c(-0.8,-0.4,0,0.4,0.8)) +theme_bw()
The solution suggested by #Z.Lin works correctly.
The two gaps are removed if you add expand = c(0, 0) to scale_x_continuous().
set.seed(1)
singJanS <- data.frame(sn=1:740, diff=rnorm(740)/3)
p <- ggplot(singJanS)+ geom_line(aes(x=sn,y=diff))
p <- p + geom_hline(yintercept=seq(-0.8,0.8,by=0.4), linetype=2, colour="grey") +
geom_vline(xintercept=seq(15,744,by=24), linetype=6, colour="red") +
geom_vline(xintercept=seq(23,744,by=24), linetype=6, colour="blue") +
ylab(Delta~T~' ('~degree~C~')')+xlab("")+
scale_x_continuous(breaks = c(0,120,240,360,480,600,720),expand = c(0, 0))+
scale_y_continuous(breaks = c(-0.8,-0.4,0,0.4,0.8)) +theme_bw()
p
Try adding to your code
+ scale_x_continuous(limits = c(0, 750))

Controlling the total width of a barplot

How to get rid of all this space where the blue lines are?
Data:
data = data.frame(is_repeat = c(0,0,0,1,1,1,1,1,1,1),
value = c(12000,8000,20000,14000,15000,11000,20000,60000,20000, 20000))
data$is_repeat = factor(data$is_repeat, levels = c(0,1),
labels = c("One-time", "Repeat"))
Plot:
ggplot(data, aes(is_repeat, value)) +
geom_bar(stat = "identity", width = 0.3) +
ggtitle("Title") +
xlab("Type of event") +
ylab("Total Value") +
ylim(0, 150000) +
theme_minimal()
edit: I looked at that question and it did NOT solve my problem. My guess is that in the other question's plot, there are 4 bars, so it looks filled. I want to reduce the total width of the X axis.
another edit: Added data.
If you are looking to remove the space between the bars completely and you don't mind the width of bars you could do it with:
geom_bar(stat="identity", position="stack", width=1)
or theme(aspect.ratio=1)
And to remove the space from the end of the plot to the bars you need
scale_x_discrete(expand = c(0,0), limits=c("One-time", "Repeat"))
So your code looks like this:
ggplot(data, aes(is_repeat, value)) +
geom_bar(stat="identity", position="stack", width=1) +
ggtitle("Title") +
xlab("Type of event") +
ylab("Total Value") +
ylim(0, 150000) +
scale_x_discrete(expand = c(0,0), limits=c("One-time", "Repeat")) +
theme_minimal()
And the output:
You can add space between bars with changing the width=1

Shift text in ggplot up

Using the this code gives the plot printed below. As you can see the percentages are printed on the border of the bars. I would like to have them above the bars. Is there a way to achieve this?
p <- ggplot(data=iris, aes(x=factor(Species), fill=factor(Species)))
p + geom_bar() + scale_fill_discrete(name="Species") + labs(x="") +geom_text(aes(y = (..count..),label = scales::percent((..count..)/sum(..count..))), stat="bin",colour="darkgreen") + theme(legend.position="none")
Just add an arbitrary value to y.
p <- ggplot(data=iris, aes(x=factor(Species), fill=factor(Species)))
p + geom_bar() + scale_fill_discrete(name="Species") + labs(x="") +geom_text(aes(y = (..count..) + 10,label = scales::percent((..count..)/sum(..count..))), stat="bin",colour="darkgreen") + theme(legend.position="none")
Or, as per Heroka's comment, use vjust, which is a better solution
p <- ggplot(data=iris, aes(x=factor(Species), fill=factor(Species)))
p + geom_bar() + scale_fill_discrete(name="Species") + labs(x="") +
geom_text(aes(y = (..count..),
label = scales::percent((..count..)/sum(..count..))),
stat="bin",
colour="darkgreen", vjust = -0.5) +
theme(legend.position="none")
But as this makes things quite cramped at the top you might want to add + expand_limits(y = c(0, 60)) to give you a bit more space for the labels.

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