annotate scripts_R/MetadataPlots.R @ 105:edd82eb89b4b branch-tests tip

Merge
author Maria Panteli
date Sun, 15 Oct 2017 13:36:59 +0100
parents d3e05cd49feb
children
rev   line source
Maria@70 1 library(rworldmap)
Maria@70 2 library(ggplot2)
Maria@70 3
m@91 4 library(extrafont)
m@91 5 font_import()
m@91 6 loadfonts()
m@91 7 Arial <- Type1Font(family="Arial", metrics=c("ArialMT.afm","arial-BoldMT.afm","Arial-ItalicMT.afm", "Arial-BoldItalicMT.afm"))
m@91 8 postscriptFonts(Arial=Arial)
m@91 9 par(family="Arial")
m@91 10
Maria@70 11 PlotBarChart<- function(df, cat="Language", ordercat="REGION", mincount=10, legend=T, color_plt="Paired"){
Maria@70 12 idx_cat = which(colnames(df)==cat)
Maria@70 13 idx_ordercat = which(colnames(df)==ordercat)
Maria@70 14 dfsub <- subset(df, df[,idx_cat]!="")
Maria@70 15 dfsub <- dfsub[ dfsub[,idx_cat] %in% names(table(dfsub[,idx_cat]))[table(dfsub[,idx_cat]) >mincount] , ]
Maria@70 16 #dfsub <- dfsub[order(dfsub$REGION.y),]
Maria@70 17 dfsub <- dfsub[order(dfsub[,idx_ordercat]),]
Maria@70 18 dfsub[,idx_cat] <- factor(dfsub[,idx_cat], levels=unique(dfsub[,idx_cat]))
Maria@70 19 g = ggplot(dfsub,aes(dfsub[,idx_cat], fill=dfsub[,idx_ordercat], order=-as.numeric(dfsub[,idx_ordercat])))+geom_bar()
Maria@70 20 #g = g+ylim("0", "100")#+scale_y_discrete(breaks=c("100"),labels=c("100+"))
Maria@70 21 g=g+scale_y_continuous(limits=c(0, 200), breaks=seq(0,200,40))
Maria@70 22 g=g+scale_fill_brewer(palette=color_plt)
m@91 23 g=g+theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5), text = element_text(size=12))
Maria@70 24 g=g+labs(y="Counts",x=cat)+coord_flip()+theme_bw()
Maria@70 25 if (legend){
Maria@70 26 g=g+guides(fill = guide_legend(title = ordercat))}
Maria@70 27 else{
Maria@70 28 g=g+guides(fill="none")
Maria@70 29 }
Maria@70 30 return(g)
Maria@70 31 }
Maria@70 32
Maria@70 33 PlotCountryCounts <- function(df, output=F){
Maria@70 34 countrycounts = table(df$Country)
Maria@70 35 dd=data.frame(countrycounts)
Maria@70 36 names(dd)=c("Country","Counts")
Maria@70 37 spdf<-joinCountryData2Map(dd,joinCode="NAME",nameCountryColumn="Country",nameJoinColumn="Country")
Maria@70 38 spdf<-spdf[-which(spdf$ADMIN=='Antarctica'),]
Maria@70 39 #mapParams <- mapCountryData(spdf, nameColumnToPlot="Counts",catMethod=as.numeric(levels(as.factor(spdf$Counts))),missingCountryCol='grey',borderCol='black',oceanCol="white",colourPalette='heat', mapTitle="",addLegend=F)
Maria@70 40 mapParams <- mapCountryData(spdf, nameColumnToPlot="Counts",catMethod=seq(10,100,10),missingCountryCol='grey',borderCol='black',oceanCol="white",colourPalette="heat", mapTitle="",addLegend=F)
Maria@70 41 #do.call( addMapLegend, c(mapParams, labelFontSize=0.7, legendShrink=0.5,legendWidth=0.5, tcl=0.3, legendMar = 7, legendLabels="all",horizontal=T, legendIntervals="page"))
Maria@70 42 legend("left", legend = c(paste(seq(90,1,-10),'-',seq(100,11,-10)), 'NA'), fill = c(heat.colors(9, alpha = 1), 'grey'), cex = 0.56, bty = "o",bg="white",box.lwd=0,box.col="white")
Maria@70 43 if (output){
Maria@70 44 pdf(file="countrycounts.pdf")
Maria@70 45 mapParams <- mapCountryData(spdf, nameColumnToPlot="Counts",catMethod=seq(10,100,10),missingCountryCol='grey',borderCol='black',oceanCol="white",colourPalette="heat", mapTitle="",addLegend=F)
Maria@70 46 #mapParams <- mapCountryData(spdf, nameColumnToPlot="Counts",catMethod=as.numeric(levels(as.factor(spdf$Counts))),missingCountryCol='grey',borderCol='black',oceanCol="lightblue",colourPalette='heat', mapTitle="",addLegend=F)
Maria@70 47 #do.call( addMapLegend, c(mapParams, labelFontSize=0.7, legendShrink=0.5,legendWidth=0.5, tcl=0.3, legendMar = 7, legendLabels="all",horizontal=T, legendIntervals="page"))
Maria@70 48 legend("left", legend = c(paste(seq(90,1,-10),'-',seq(100,11,-10)), 'NA'), fill = c(heat.colors(9, alpha = 1), 'grey'), cex = 0.56, bty = "o",bg="white",box.lwd=0,box.col="white")
Maria@70 49 dev.off()
Maria@70 50 }
Maria@70 51 }
Maria@70 52
Maria@70 53 PlotYearDistribution <- function(df, output=F){
Maria@70 54 df$Year<-as.numeric(as.character(df$Year))
Maria@70 55 g = ggplot(df,aes(x=Year,y=..count..))+geom_histogram(breaks=seq(1895, 2015, by = 1))
Maria@70 56 #g = ggplot(df,aes(x=Year,y=..count..))+geom_bar()+geom_density(alpha=.3, fill="grey")
Maria@70 57 #g = g+scale_x_continuous(breaks = pretty(df$Year, n=10))
Maria@70 58 g = g+theme_bw()+labs(x ='Year', y ='Count')
Maria@70 59 #g = ggplot(df,aes(x=Year,y=..count..))+geom_histogram()+theme_bw()
Maria@70 60 print(g)
Maria@70 61 if (output){
Maria@70 62 ggsave('yeardistribution.pdf',plot=g)
Maria@70 63 }
Maria@70 64 }
Maria@70 65
Maria@70 66 PlotLanguageDistribution <- function(df, mincount=1){
Maria@70 67 dfsubset <- subset(df, df$Language!="") # ignore the recordings culture info
Maria@70 68 culturecounts = table(dfsubset$Culture)
Maria@70 69 culturecounts = culturecounts[culturecounts>=mincount]
Maria@70 70 barplot(culturecounts, las=2, cex.names=0.2)
Maria@70 71 #g = ggplot(df,aes(x=Language))+geom_bar()
Maria@70 72 #g+theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))
Maria@70 73 }
Maria@70 74
Maria@70 75 PlotCountryOutliers <- function(df, output=''){
Maria@70 76 par(mar = rep(2, 4))
Maria@70 77 spdf<-joinCountryData2Map(df,joinCode="NAME",nameCountryColumn="Country",nameJoinColumn="Country")
Maria@70 78 spdf<-spdf[-which(spdf$ADMIN=='Antarctica'),]
Maria@70 79 #mapParams <- mapCountryData(spdf, nameColumnToPlot="Outliers",catMethod=seq(0,70,5),missingCountryCol='grey',colourPalette='heat', mapTitle="", addLegend=FALSE)
Maria@70 80 mapParams <- mapCountryData(spdf, nameColumnToPlot="Outliers", catMethod=seq(0,1,0.1), missingCountryCol='grey',colourPalette='heat', mapTitle="", addLegend=FALSE)
Maria@70 81 # avoid antarctica
Maria@70 82 #mapParams <- mapCountryData(spdf, nameColumnToPlot="Outliers", ylim=c(-60,90), catMethod=seq(0,1,0.1), missingCountryCol='grey',colourPalette='heat', mapTitle="", addLegend=FALSE)
Maria@70 83 #do.call( addMapLegend, c(mapParams, labelFontSize=0.7, legendWidth=0.5, tcl=0.3, legendMar = 7, legendLabels="all",horizontal=T, legendIntervals="page"))
Maria@70 84 legend("left", legend = c(paste(seq(90,0,-10),'-',seq(100,10,-10),'%'), 'NA'), fill = c(heat.colors(10, alpha = 1), 'grey'), cex = 0.56, bty = "o",bg="white",box.lwd=0,box.col="white")
Maria@70 85 if (output!=''){
m@91 86 if (grepl('.pdf', output)){
m@91 87 pdf(output, pointsize=12)
m@91 88 } else if (grepl('.eps', output)){
m@91 89 postscript(output, pointsize=12)
m@91 90 }
Maria@70 91 #mapParams <- mapCountryData(spdf, nameColumnToPlot="Outliers",catMethod=seq(0,70,5),missingCountryCol='grey',colourPalette='heat', mapTitle="", addLegend=FALSE)
Maria@70 92 mapParams <- mapCountryData(spdf, nameColumnToPlot="Outliers", catMethod=seq(0,1,0.1), missingCountryCol='grey',colourPalette='heat', mapTitle="", addLegend=FALSE)
Maria@70 93 #mapParams <- mapCountryData(spdf, nameColumnToPlot="Outliers", ylim=c(-60,90), catMethod=seq(0,1,0.1), missingCountryCol='grey',colourPalette='heat', mapTitle="", addLegend=FALSE)
Maria@70 94 #do.call( addMapLegend, c(mapParams, labelFontSize=0.7, legendWidth=0.5, tcl=0.3, legendMar=7, legendLabels="all",horizontal=T, legendIntervals="page"))
Maria@70 95 legend("left", legend = c(paste(seq(90,0,-10),'-',seq(100,10,-10),'%'), 'NA'), fill = c(heat.colors(10, alpha = 1), 'grey'), cex = 0.56, bty = "o",bg="white",box.lwd=0,box.col="white")
Maria@70 96 dev.off()
Maria@70 97 }
Maria@70 98 else {
Maria@70 99 return(mapParams)
Maria@70 100 }
Maria@70 101 }