Eduardo M. A. M. Mendes
2014-Jun-27 20:05 UTC
[R] ggplot2 - change title and size of legends - time series.
Hi
I am trying to understand how to use ggplot2 to plot multiple time-series in one
single graphics. In order to do so, I have modified the example given at
http://docs.ggplot2.org/current/guide_legend.html to use my own data.
Here is the script so far
# Script for plotting all variables
rm(list=ls())
graphics.off()
## ---- Q1 ----
library(ggplot2);
library(gridExtra)
require(timeDate)
require(reshape2)
ind <- seq(1,1000)
modelnames <- c("PRO1_PXXS_F_MODEL_1",
"PRO2_PXXS_F_MODEL_2",
"PRO3_PXXS_F_MODEL_3",
"PRO4_PXXS_F_MODEL_4",
"Real_Data")
# Create the time vector
Time<-as.POSIXct(timeSequence(from="2014-05-09
18:43:00",length.out=length(ind),
by="mins"))
# Create variable
aux=cbind(runif(length(ind),min=2300,max=2700),runif(length(ind),min=3200,max=3700),
runif(length(ind),min=2600,max=2800),runif(length(ind),min=2300,max=2400),
runif(length(ind),min=2700,max(3300)))
resmodMay=data.frame(id=Time,aux);
colnames(resmodMay)=c("Time",modelnames);
meltdf<-melt(resmodMay, id="Time")
## ---- Q2 ----
# Plot
g<-ggplot(meltdf,aes(x=Time,y=value,colour=variable,group=variable))
g<-g+geom_line()+labs(title = "Some plots")
g<-g+xlab("Date")+ylab("Value of the
variable")+scale_fill_discrete(guide = guide_legend(title = "V"))
print(g)
## ---- Q3 ----
# One Solution
meltdfm<-melt(resmodMay, id="Time")
colnames(meltdfm)[2]<-"Models"
g1<-ggplot(meltdfm,aes(x=Time,y=value,colour=Models,group=Models))
g1<-g1+geom_line()+labs(title = "Some plots")
g1<-g1+xlab("Date")+ylab("Value of the
variable")+scale_fill_discrete(guide = guide_legend(title = "V"))
print(g1)
On the top of the legends ggplot2 writes "variable"; I need to change
that and also need to increase the size of the legends (fonts size, symbols,
everything). As you can see I have a solution for the first problem (title of
the legend), but that is not the type of solution I want. I was thinking of
using ggplot2 layers to change that. Something like that last line on ##----
Q2 -----, which unfortunately it does not work (even though is given as example
on ggplot2 documentation - It is likely that I have misread the example).
Any help will be most appreciated.
Ed
[[alternative HTML version deleted]]
Eduardo M. A. M. Mendes
2014-Jun-28 18:08 UTC
[R] ggplot2 - change title and size of legends - time series.
Hi all
Thanks to Dennis Murphy I got an answer to my question.
I am posting below a modified version of my script that might be of some help to
other ggplot2 users.
Many thanks
Ed
library(ggplot2);
library(gridExtra)
require(timeDate)
require(reshape2)
ind <- seq(1,1000)
modelnames <- c("PRO1_PXXS_F_MODEL_1",
"PRO2_PXXS_F_MODEL_2",
"PRO3_PXXS_F_MODEL_3",
"PRO4_PXXS_F_MODEL_4",
"Real_Data")
# Create the time vector
Time<-as.POSIXct(timeSequence(from="2014-05-09
18:43:00",length.out=length(ind),
by="mins"))
# Create variable
aux=cbind(runif(length(ind),min=2300,max=2700),runif(length(ind),min=3200,max=3700),
runif(length(ind),min=2600,max=2800),runif(length(ind),min=2300,max=2400),
runif(length(ind),min=2700,max(3300)))
resmodMay=data.frame(id=Time,aux);
colnames(resmodMay)=c("Time",modelnames);
meltdf<-melt(resmodMay, id="Time")
## ---- Q2 ----
# Plot 2
g<-ggplot(meltdf,aes(x=Time,y=value,colour=variable,group=variable))+geom_line()
g<-g+labs(title = "Some plots", x="Date", y="Value
of the variable")
g<-g+theme(legend.position = "left") # Change legend position to
the left
g<-g+scale_colour_discrete(name = "Models") # Change legend title
g<-g+theme(legend.title = element_text(colour="blue", size=16,
face="bold")) # Change size, color and face of legend title
g<-g+theme(title = element_text(colour="black", size=16,
face="italic")) # Change size, color and face of plot title
g<-g+theme(axis.text = element_text(size = 18), axis.title =
element_text(size = rel(1.75))) # Change size of the text in the axis-labels and
axis title
g<-g+theme(legend.key.size = unit(0.5, "in")) # Change key size in
the legend
g<-g+theme(legend.text = element_text(size=12)) # Change the size labels in
the legend
print(g)
On Jun 27, 2014, at 5:05 PM, Eduardo M. A. M. Mendes
<emammendes@gmail.com> wrote:
> Hi
>
> I am trying to understand how to use ggplot2 to plot multiple time-series
in one single graphics. In order to do so, I have modified the example given at
http://docs.ggplot2.org/current/guide_legend.html to use my own data.
>
> Here is the script so far
>
>
> # Script for plotting all variables
>
> rm(list=ls())
> graphics.off()
>
> ## ---- Q1 ----
>
> library(ggplot2);
> library(gridExtra)
> require(timeDate)
> require(reshape2)
>
> ind <- seq(1,1000)
>
> modelnames <- c("PRO1_PXXS_F_MODEL_1",
> "PRO2_PXXS_F_MODEL_2",
> "PRO3_PXXS_F_MODEL_3",
> "PRO4_PXXS_F_MODEL_4",
> "Real_Data")
>
> # Create the time vector
>
> Time<-as.POSIXct(timeSequence(from="2014-05-09
18:43:00",length.out=length(ind),
> by="mins"))
>
> # Create variable
>
aux=cbind(runif(length(ind),min=2300,max=2700),runif(length(ind),min=3200,max=3700),
>
runif(length(ind),min=2600,max=2800),runif(length(ind),min=2300,max=2400),
> runif(length(ind),min=2700,max(3300)))
> resmodMay=data.frame(id=Time,aux);
> colnames(resmodMay)=c("Time",modelnames);
> meltdf<-melt(resmodMay, id="Time")
>
> ## ---- Q2 ----
>
> # Plot
>
> g<-ggplot(meltdf,aes(x=Time,y=value,colour=variable,group=variable))
> g<-g+geom_line()+labs(title = "Some plots")
> g<-g+xlab("Date")+ylab("Value of the
variable")+scale_fill_discrete(guide = guide_legend(title = "V"))
> print(g)
>
> ## ---- Q3 ----
>
> # One Solution
>
> meltdfm<-melt(resmodMay, id="Time")
> colnames(meltdfm)[2]<-"Models"
> g1<-ggplot(meltdfm,aes(x=Time,y=value,colour=Models,group=Models))
> g1<-g1+geom_line()+labs(title = "Some plots")
> g1<-g1+xlab("Date")+ylab("Value of the
variable")+scale_fill_discrete(guide = guide_legend(title = "V"))
> print(g1)
>
>
> On the top of the legends ggplot2 writes "variable"; I need to
change that and also need to increase the size of the legends (fonts size,
symbols, everything). As you can see I have a solution for the first problem
(title of the legend), but that is not the type of solution I want. I was
thinking of using ggplot2 layers to change that. Something like that last line
on ##---- Q2 -----, which unfortunately it does not work (even though is given
as example on ggplot2 documentation - It is likely that I have misread the
example).
>
> Any help will be most appreciated.
>
> Ed
>
[[alternative HTML version deleted]]