inserting custom text to ggplot2 - r

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)

Related

Arrange Ggplots in GridExtra with Set of Column Names and Row Names in R

I have this 15 set of ggplots that I want to give a set of colnames to as cnames <- c('phi = 0.8', 'phi = 0.9', 'phi = 0.95') and row names as rnames <- c("NBB", "MBB", "CBB", "MBB1", "CBB1").
#simple plot
p <- ggplot2::ggplot(mtcars, aes(wt,mpg))
# load package
library(ggplot2)
library(gridExtra)
# setting-up grid of plots...5 columns by 3 rows
sample <- grid.arrange(p + geom_line() + theme_bw(),
p + geom_line() + theme_bw(),
p + geom_line() + theme_bw(),
p + geom_line() + theme_bw(),
p + geom_line() + theme_bw(),
p + geom_line() + theme_bw(),
p + geom_line() + theme_bw(),
p + geom_line() + theme_bw(),
p + geom_line() + theme_bw(),
p + geom_line() + theme_bw(),
p + geom_line() + theme_bw(),
p + geom_line() + theme_bw(),
p + geom_line() + theme_bw(),
p + geom_line() + theme_bw(),
p + geom_line() + theme_bw(),
ncol = 3)
I want the row names to be rotated at 90 degree while the column name be at normal degree
EDIT
I want output similar to this.

R plot not saving as png, please check my code

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

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

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.

Resources