plot_TL<-function(TrafficLight,text_size=15,ncol=2){
require(luplot, quietly = TRUE)
# prepare input
tmp<-as.ggplot(TrafficLight)
tmp$Value<-2-tmp$Value
tmp$TL<-as.factor(tmp$Value)
tmp$ymin<-tmp$Value-0.35
tmp$ymax<-tmp$Value+0.35
tmp$Year<-1
#construct the basic plot
p<-magpie2ggplot2(tmp,xaxis="Year",y_axis="Value",xlim=c(-1,3),ylim = c(-0.5,2.5),facet_x="Data1",geom="none",scales="fixed",text_size=text_size,legend_position="none",ncol = ncol)
# add black area for trafficLight
p<-p+geom_rect(mapping = aes(xmin=0.8,xmax=1.2,ymin= -0.5,ymax=2.5),color="black",fill="black")
# add shapes of lights
p<-p+geom_rect(mapping = aes(xmin=0.85,xmax=1.15,ymin= 1.65,ymax=2.35),color="grey20",fill="grey20")
p<-p+geom_rect(mapping = aes(xmin=0.85,xmax=1.15,ymin= 0.65,ymax=1.35),color="grey20",fill="grey20")
p<-p+geom_rect(mapping = aes(xmin=0.85,xmax=1.15,ymin= -0.35,ymax=0.35),color="grey20",fill="grey20")
#add data
p<-p+geom_rect(mapping=aes(ymin=ymin,ymax=ymax,color=TL,fill=TL),xmin=0.85,xmax=1.15)
#Get the correct color scale
colours<-c("0"="green","1"="yellow","2"="red")
p<-p+scale_colour_manual(values=colours[levels(tmp$TL)],
breaks=levels(tmp$TL))
p<-p+scale_fill_manual(values=colours[levels(tmp$TL)],
breaks=levels(tmp$TL))
# get nice format
p <- p + theme_bw(19) %+replace%
theme(axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.position="none",
panel.grid=element_blank(),
strip.text.y = element_text(size=text_size, angle=270),
strip.text.x = element_text(size=text_size, angle=0),
strip.background = element_rect(fill="#D9D9D9"))
return(p)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.