How to change the default date interval in ggplot? - r

I am not sure what is the default date interval in ggplot. My data has five data points including Sep-2011, Dec-2011, Mar-2012,Jun-2012 and Sep-2012.
The ggplot displays different data points from my data which I found a bit annoying. Am I missing anything?
Could you help to display "Sep-2011, Dec-2011, Mar-2012,Jun-2012 and Sep-2012
x4.1.m<- structure(list(Var.1=structure(c(1L,2L,3L,4L,5L,6L,1L,2L,3L,4L,5L,6L,1L,2L,3L,4L,5L,6L,1L,2L,3L,4L,5L,6L,1L,2L,3L,4L,5L,6L),.Label=c("I'vechangedforwork/anewjob/goneonaworkplan","IwantaphonethatVodafonedoesn'toffer","IwantBestMates/Favourites","Iwasofferedorsawabetterofferonanothernetwork","Issueswiththe2degreesnetwork(poorcoverage)","Other"),class="factor"),YearQuarter=structure(c(1L,1L,1L,1L,1L,1L,2L,2L,2L,2L,2L,2L,3L,3L,3L,3L,3L,3L,4L,4L,4L,4L,4L,4L,5L,5L,5L,5L,5L,5L),.Label=c("2011-09-01","2011-12-01","2012-03-01","2012-06-01","2012-09-01"),class="factor"),value=c(0.23,0.23,0.121,0.25,0.223,0.14,0.39,0.22,0.05,0.37,0.25,0.2,0.09,0.14,0.05,0.3,0.4,0.12,0.13,0.1,0.26,0.38,0.28,0.15,0.33,0.05,0.06,0.44,0.32,0.43)),.Names=c("Var.1","YearQuarter","value"),row.names=c(NA,-30L),class="data.frame")
library(scales)
library(ggplot2)
###data
x4.1.m$YearQuarter <- as.Date(x4.1.m$YearQuarter)
x4.1.m$label <- paste(round(x4.1.m$value*100,0), "%", sep="")
### plot
x4.line <- ggplot(data=x4.1.m, aes(x=YearQuarter, y=value,colour=Var.1)) +
geom_smooth(se=F, size=1.5)
x4.line <- x4.line + geom_text(aes(label = label),size = 3, hjust = 0.5, vjust =1.5)
### theme
x4.line <- x4.line + theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.background=element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank())
x4.line <- x4.line + ggtitle("Percentages:Main Reasons for Leaving Vodafone by Quarter") +
theme(plot.title = element_text(size=rel(1.2)))+
scale_y_continuous(labels=percent, limits=c(0,0.5)) +
scale_x_date(labels = date_format("%b-%y"),breaks = date_breaks("3 months"))+
labs(y="Percentage",x="Year Quarter")
x4.line

You need to pass your required breaks to breaks argument to scale_x_date (what a surprise there.)
# your breaks
d <- unique(x4.1.m[['YearQuarter']])
themestuff <- theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.background=element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank())
x4.line <- ggplot(data=x4.1.m, aes(x=YearQuarter, y=value,colour=Var.1)) +
geom_smooth(se=F, size=1.5) +
geom_text(aes(label = label),size = 3, hjust = 0.5, vjust =1.5) +
themestuff + ggtitle("Percentages:Main Reasons for Leaving Vodafone by Quarter") +
theme(plot.title = element_text(size=rel(1.2))) +
scale_y_continuous(labels=percent, limits=c(0,0.5)) +
scale_x_date(labels = date_format("%b-%y"), breaks = d)+
labs(y="Percentage",x="Year Quarter")
x4.line

Related

Latex Expression in Legend

I want to add a latex expression in my legend like red-colored. I tried the following code, but I could not do that. How do I manage this issue? Also, I got a waring about this code. How do I remove that?
library(ggplot2)
ggplot(C, aes(sigma, value)) +
geom_line(aes(linetype=name)) +
scale_linetype_manual(values=c("solid", "twodash", "dotted")) +
scale_linetype_discrete(labels = c(expression("X_A = 1 \\& X_B =0"),
expression("X_A = 3 \\& X_B =0"),
expression("X_A = 5 \\& X_B =0"))) +
theme_minimal() +
xlab("standard diviation") +
ylab("rejection rate") +
theme_classic() +
theme(legend.position = c(0.15, 0.85),
legend.background = element_rect(fill="white",
size=0.5,
linetype="solid",
colour ="gray"),
legend.spacing.y = unit(0, "pt"),
text = element_text(size=20),
legend.text = element_text(size=20),
axis.title=element_text(size=20),
legend.title = element_blank(),
panel.grid.major.y = element_line(),
panel.grid.minor.y = element_line())
Scale for 'linetype' is already present. Adding another scale for 'linetype',
which will replace the existing scale.

change line type in ggplot2 in r

I have a data frame like this.
df <- data.frame(date = c('2015-11-23','2015-11-24','2015-11-25','2015-11-23','2015-11-24','2015-11-25'),
variable = c('LCNB', 'LCNB','LCNB','LCDEF','LCDEF','LCDEF'),
value = c(1,2,3,3,2,1))
I want to plot two lines in the same plot, with different color and line types. my current code is:
library(scales)
ggplot(df, aes(x=as.Date(date), y=value, color=variable)) + geom_line(size=1.07) +
scale_color_manual(labels = c("Nb",'Def'), values = c("#E69F00", "#0072B2")) +
scale_x_date(labels = date_format("%Y-%m-%d"), breaks = date_breaks("2.8 month")) +
theme(axis.text.x = element_text(angle = 0, vjust = 0.5, hjust=1))+labs(x="Dates",y="%") +
theme_bw()+
theme( panel.grid.major = element_blank(),panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),plot.margin=unit(c(0,1,0.3,1), "cm")) +
labs(colour = "LC") + theme(legend.position = c(0.95,0.85))
my code so far only makes two lines different color, how can i make them differnt line types as well.
Thank you for the help,
You just need to take the same steps with line type as you did with color, but for linetype:
ggplot(df, aes(x=date, y=value, color=variable, linetype = variable)) +
geom_line(size=1.07,) +
scale_color_manual(
labels = c("Nb",'Def'),
values = c("#E69F00", "#0072B2")
) +
scale_linetype(labels = c("Nb", "Def")) +
scale_x_date(labels = date_format("%Y-%m-%d"), breaks = date_breaks("2.8 month")) +
theme(axis.text.x = element_text(angle = 0, vjust = 0.5, hjust=1)) +
labs(x="Dates",y="%", colour = "LC", linetype = "LC") +
theme_bw()+
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
plot.margin=unit(c(0,1,0.3,1), "cm"),
legend.position = c(0.95,0.85))

Make the faceted x-axis text be the grouping factor in R using ggplot2 when plotting boxplots

I am creating faceted box plots that are grouped by a variable. Instead of having the x-axis text be the factors for the x-axis variable I'd like the x-axis text to be the grouping variable.
However, I don't just want to use the grouping variable as my x-axis variable because I'd like the boxplots to cluster. Its hard to explain well. But I think its clear from the code and comments below.
Let me know if you have any suggestions or can help and thanks in advance!
library(ggplot2)
library(scales)
ln_clr <- "black"
bk_clr <- "white"
set.seed(1)
# Creates variables for a dataset
donor = rep(paste0("Donor",1:3), each=40)
machine = sample(rep(rep(paste0("Machine",1:4), each=1),30))
gene = rep(paste0("Gene",LETTERS[1:5]), each=24)
value = rnorm(24*5, mean=rep(c(0.5,10,1000,25000,8000), each=24),
sd=rep(c(0.5,8,900,9000,3000), each=24))
# Makes all values positive
for(m in 1:length(value)){
if(value[m]<0){
value[m] <- sqrt(value[m]*value[m])
}
}
# Creates a data frame from variables
df = data.frame(donor, machine, gene, value)
# Adds a clone variable
clns <- LETTERS[1:4]
k=1
for(i in 1:nrow(df)/4){
for(j in 1:length(clns)){
df$clone[k] <- paste(df$donor[k],clns[j],sep="")
k = k+1
}
}
df$clone <- as.factor(df$clone)
#*************************************************************************************************************************************
# Creates the facet of the machine but what I want on the x-axis is clone, not donor.
# However, if I set x to clone it doesn't group the boxplots and its harder to read
# the graph.
bp1 <- ggplot(df, aes(x=donor, y=value, group=clone)) +
stat_boxplot(geom ='errorbar', position = position_dodge(width = .83),
width = 0.25, size = 0.7, coef = 1) +
geom_boxplot(coef=1, outlier.shape = NA, position = position_dodge(width = .83),
lwd = 0.3, alpha = 1, colour = ln_clr) +
geom_point(position = position_dodge(width = 0.83), size = 1.8, alpha = 0.9,
mapping=aes(group=clone)) +
facet_wrap(~ machine, ncol=2, scales="free_x")
bp1 + scale_y_log10(expand = c(0, 0)) +
theme(axis.text.x= element_text(size=rel(1), colour = "black", angle=45, hjust=1),
strip.background = element_rect(colour = ln_clr, fill = bk_clr, size = 1))
# Creates the facet of the Donor and clusters the clones but doesn't facet the
# machine. This could be okay if I could put spaces in between the different
# machine values but not the donors and could remove the donor facet labels, and
# only have the machine values show up once.
bp2 <- ggplot(df, aes(x=clone, y=value)) +
stat_boxplot(geom ='errorbar', position = position_dodge(width = .83),
width = 0.25, size = 0.7, coef = 1) +
geom_boxplot(coef=1, outlier.shape = NA, position = position_dodge(width = .83),
lwd = 0.3, alpha = 1, colour = ln_clr) +
geom_point(position = position_dodge(width = 0.83), size = 1.8, alpha = 0.9) +
facet_wrap(machine ~ donor, scales="free_x", ncol=6)
bp2 + scale_y_log10(expand = c(0, 0)) +
theme(axis.text.x= element_text(size=rel(1), colour = "black", angle=45, hjust=1),
strip.background = element_rect(colour = ln_clr, fill = bk_clr, size = 1),
panel.spacing = unit(0, "lines"))
Below is an example comparing what I'd like in an ideal world (Top two facets) as compared to what I'm getting (bottom two facets).
I'm not sure I understand exactly what you're trying to do, so let me know if this is on the right track:
library(dplyr)
pd = position_dodge(width=0.83)
ggplot(df %>% mutate(clone=gsub("Donor[1-3]","",clone),
donor=gsub("Donor", "", donor)),
aes(x=clone, y=value, color=donor, group=interaction(clone,donor))) +
geom_boxplot(coef=1, outlier.shape=NA, position=pd, lwd=0.3) +
geom_point(position=pd, size=1.8, alpha=0.9) +
facet_wrap(~ machine, ncol=2, scales="free_x") +
scale_y_log10(expand = c(0.02, 0)) +
theme(strip.background=element_rect(colour=ln_clr, fill=bk_clr, size=1))
How about this:
ggplot(df, aes(x=clone, y=value, group=interaction(clone,donor))) +
geom_boxplot(coef=1, outlier.shape=NA, lwd=0.3) +
geom_point(size=1.8, alpha=0.9) +
facet_wrap(~ machine, ncol=2, scales="free_x") +
scale_y_log10(expand = c(0.02, 0)) +
theme(axis.text.x= element_text(size=rel(1), colour = "black", angle=45, hjust=1),
strip.background=element_rect(colour=ln_clr, fill=bk_clr, size=1))
I found a work around for this problem but its not very elegant. I'd be super happy if some one came up with a better solution. Using the code to create a function for a "multiplot" found here and adding the code below I was able to do what I wanted. However, This is a slightly wonky solution in that I can't really format my titles with boxes around them and there are still two "clone" titles on the x axis that I can't replace easily with a single x-axis title. Also, had I of had many "machines" in my example this solution would have been painful to scale. All-in-all not ideal but passible for what I need. Special thanks to Eipi10 for their help, I appreciate it.
# Creates a multi-plot function for use in the graphs below
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
library(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# If layout is NULL, then use 'cols' to determine layout
if (is.null(layout)) {
# Make the panel
# ncol: Number of columns of plots
# nrow: Number of rows needed, calculated from # of cols
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
ncol = cols, nrow = ceiling(numPlots/cols))
}
if (numPlots==1) {
print(plots[[1]])
} else {
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
# Make each plot, in the correct location
for (i in 1:numPlots) {
# Get the i,j matrix positions of the regions that contain this subplot
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col))
}
}
}
# Call multiplot function after storing each of the below plots as variables
ln_clr <- "black"
bk_clr <- "white"
bp3 <- ggplot(df[df$machine=="Machine1",], aes(x=clone, y=value)) +
geom_boxplot(coef=1, outlier.shape=NA, lwd=0.3) +
geom_point(size=1.8, alpha=0.9) +
ggtitle("Machine 1") +
expand_limits(y=c(0.001,10^5)) +
facet_wrap(~ donor, nrow=1, scales="free_x") + scale_y_log10(expand = c(0, 0)) +
theme(axis.text.x= element_text(size=rel(1), color = ln_clr, angle=45, hjust=1),
panel.spacing = unit(0.25, "lines"), axis.title.x= element_blank(),
plot.title = element_text(hjust=0.5),
strip.text.x = element_text(size=rel(1), face="bold", colour = ln_clr),
strip.background = element_rect(colour = ln_clr, fill = bk_clr, size = 1),
axis.line.x= element_line(size = 1.25, colour = ln_clr),
axis.line.y= element_line(size = 1.25, colour = ln_clr),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = bk_clr),
panel.border = element_blank(),
plot.background = element_rect(fill = bk_clr))
bp4 <- ggplot(df[df$machine=="Machine2",], aes(x=clone, y=value)) +
geom_boxplot(coef=1, outlier.shape=NA, lwd=0.3) +
geom_point(size=1.8, alpha=0.9) +
ggtitle("Machine 2") +
expand_limits(y=c(0.001,10^5)) +
facet_wrap(~ donor, nrow=1, scales="free_x") + scale_y_log10(expand = c(0, 0)) +
theme(axis.text.x= element_text(size=rel(1), colour = ln_clr, angle=45, hjust=1),
panel.spacing = unit(0.25, "lines"), plot.title = element_text(hjust=0.5),
strip.text.x = element_text(size=rel(1), face="bold", colour = ln_clr),
strip.background = element_rect(colour = ln_clr, fill = bk_clr, size = 1),
axis.line.x= element_line(size = 1.25, colour = ln_clr),
axis.line.y= element_line(size = 1.25, colour = ln_clr),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = bk_clr),
panel.border = element_blank(),
plot.background = element_rect(fill = bk_clr))
bp5 <- ggplot(df[df$machine=="Machine3",], aes(x=clone, y=value)) +
geom_boxplot(coef=1, outlier.shape=NA, lwd=0.3) +
geom_point(size=1.8, alpha=0.9) +
ggtitle("Machine 3") +
expand_limits(y=c(0.001,10^5)) +
facet_wrap(~ donor, nrow=1, scales="free_x") + scale_y_log10(expand = c(0, 0)) +
theme(panel.spacing = unit(0.25, "lines"), axis.title.y= element_blank(),
axis.title.x= element_blank(),axis.line.y= element_blank(),
axis.text.y=element_blank(),
axis.text.x= element_text(size=rel(1), colour = ln_clr, angle=45, hjust=1),
axis.ticks.y=element_blank(), plot.title = element_text(hjust=0.5),
strip.text.x = element_text(size=rel(1), face="bold", colour = ln_clr),
strip.background = element_rect(colour = ln_clr, fill = bk_clr, size = 1),
axis.line.x= element_line(size = 1.25, colour = ln_clr),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = bk_clr),
panel.border = element_blank(),
plot.background = element_rect(fill = bk_clr))
bp6 <- ggplot(df[df$machine=="Machine4",], aes(x=clone, y=value)) +
geom_boxplot(coef=1, outlier.shape=NA, lwd=0.3) +
geom_point(size=1.8, alpha=0.9) +
ggtitle("Machine 4") +
expand_limits(y=c(0.001,10^5)) +
facet_wrap(~ donor, nrow=1, scales="free_x") + scale_y_log10(expand = c(0, 0)) +
theme(axis.text.x= element_text(size=rel(1), colour = ln_clr, angle=45, hjust=1),
panel.spacing = unit(0.25, "lines"), plot.title = element_text(hjust=0.5),
strip.text.x = element_text(size=rel(1), face="bold", colour = ln_clr),
strip.background = element_rect(colour = ln_clr, fill = bk_clr, size = 1),
axis.line.x= element_line(size = 1.25, colour = ln_clr),
axis.line.y= element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
axis.title.y= element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = bk_clr),
panel.border = element_blank(),
plot.background = element_rect(fill = bk_clr))
# Plot all 4 graphs and saves them as a output file
png(filename="graph3.png", width= 9, height= 7.5, units = "in", res=600)
multiplot(bp3, bp4, bp5, bp6, cols=2)
dev.off()
Alternatively, if I set the "strip.text.x = " and the "strip.background =" as element_blank(). I can generate the below:

How to specify different palettes and line size within a single ggplot2 plot

I'm displaying four distributions within the same ggplot2 graph with the following code (data downloadable there: https://www.dropbox.com/s/l5j7ckmm5s9lo8j/1.csv?dl=0):
require(reshape2)
library(ggplot2)
library(RColorBrewer)
fileName = "./1.csv" # downloadable there: https://www.dropbox.com/s/l5j7ckmm5s9lo8j/1.csv?dl=0
mydata = read.csv(fileName,sep=",", header=TRUE)
dataM = melt(mydata,c("bins"))
ggplot(data=dataM, aes(x=bins, y=value, colour=variable)) +
xlab("bins") + ylab("freq") + geom_line(size = .5, alpha = .9) +
scale_colour_brewer(type = "qual", palette = 7) +
geom_line(size = .5, alpha = .9) +
theme_bw() +
theme(plot.background = element_blank()
,panel.grid.minor = element_blank()
,axis.line = element_blank()
,legend.key = element_blank()
,legend.title = element_blank()) +
scale_y_continuous(expand=c(0,0)) +
scale_x_continuous(expand=c(0,0))
How to change this graph so that B, E and W appear according to a specific palette (say: scale_colour_brewer) with a width of .5, while R appears in scale_colour_brewer(type = "qual", palette = 7) with a width of 1?
You can call subsets of your data in seperate geom_line's:
ggplot() +
geom_line(data=dataM[dataM$variable!="R",], aes(x=bins, y=value, colour=variable), size = .5, alpha = .9) +
geom_line(data=dataM[dataM$variable=="R",], aes(x=bins, y=value, colour=variable), size = 1.5, alpha = .9) +
scale_colour_brewer(type = "qual", palette = 7) +
theme_bw() +
theme(plot.background = element_blank(), panel.grid.minor = element_blank(), axis.line = element_blank(),
legend.key = element_blank(), legend.title = element_blank()) +
scale_y_continuous("freq", expand=c(0,0)) +
scale_x_continuous("bins", expand=c(0,0))
this gives:
Another solution (as suggested by #baptiste) is setting the size and colour scales manually:
ggplot(data=dataM, aes(x=bins, y=value, colour=variable, size = variable)) +
geom_line(alpha = .9) +
scale_colour_manual(breaks=c("B","E","W","R"), values=c("green","orange","blue","pink")) +
scale_size_manual(breaks=c("B","E","W","R"), values=c(0.5,0.5,0.5,1.5)) +
theme_bw() +
theme(plot.background = element_blank(), panel.grid.minor = element_blank(), axis.line = element_blank(),
legend.key = element_blank(), legend.title = element_blank()) +
scale_y_continuous("freq", expand=c(0,0)) +
scale_x_continuous("bins", expand=c(0,0))
this gives more or less the same result:
ggplot(aes(x=bins, y=value, colour=variable)) +
geom_line(data=dataM[dataM$variable!="R",]) +
geom_line(data=dataM[dataM$variable=="R",])

Legend linetypes not displaying properly ggplot

I have the following plot:
ggplot(proba[108:140,], aes(c,four, color="a1")) +
geom_line(linetype=1, size=0.3) +
scale_x_continuous(breaks=seq(110,140,5)) +
theme_bw() +
theme(axis.line = element_line(colour = "black", size=0.25),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank()) +
theme(axis.text.x = element_text(angle = 0, hjust = +0.5, size=6,color="black")) +
theme(axis.text.y = element_text(angle = 0, hjust = -100, size=6, color="black")) +
theme(axis.ticks=element_line(colour="black",size=0.25)) +
xlab("\nTime-steps")+ylab("Proportion correct\n") +
theme(axis.text=element_text(size=8),
axis.title=element_text(size=8)) +
geom_line(aes(c,three, color="a2"), size=0.2, linetype=2) +
geom_line(aes(c,one, color="a3"),linetype=3, size=0.8) +
geom_line(aes(c,two, color="a4"), linetype=1, size=0.6) +
scale_color_manual(values=c("a1"="red3", "a2"="red3","a3"="blue3","a4"="blue3")) +
theme(legend.title = element_text(colour="black", size=7)) +
theme(legend.position="bottom" ,
legend.direction="horizontal",
legend.title=theme_blank()) +
theme(legend.text=theme_text(size=7),
legend.background=theme_blank(),
legend.key=theme_blank())
The four lines on the plot are displayed in different linetypes, however the legend does not show these different linetypes only the different colours. I am obviously missing a very simple argument in theme() but I cannot figure out what it is. Maybe I need to specify the linetypes again in scale_colour_manual()?
Here's the data: https://dl.dropboxusercontent.com/u/22681355/proba.csv
proba<-read.csv("proba.csv",head=T)
Here, this is the correct way to do this in ggplot2:
proba <- read.csv("~/Downloads/proba.csv")
proba1 <- proba[108:140,]
proba1 <- melt(proba1,id.vars = 1:2)
ggplot(proba1,aes(x = c,y = value,colour = variable,linetype = variable,size = variable)) +
geom_line() +
scale_x_continuous(breaks=seq(110,140,5)) +
scale_colour_manual(values=c("blue3","blue3","red3","red3")) +
scale_linetype_manual(values = c(2,1,3,1)) +
scale_size_manual(values = c(0.2,0.3,0.8,0.6)) +
xlab("\nTime-steps") +
ylab("Proportion correct\n") +
theme_bw() +
theme(axis.text=element_text(size=6),
axis.title=element_text(size=8),
axis.line = element_line(size=0.25),
axis.ticks=element_line(size=0.25),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
legend.position="bottom" ,
legend.direction="horizontal",
legend.title=element_blank(),
legend.text=element_text(size=7),
legend.background=element_blank(),
legend.key=element_blank())
Try to keep all your theme adjustments in one call to theme, otherwise things get pretty messy. Also, you had some calls to theme_* that should have been element_*.

Resources