comparison scripts_R/Metadata_subsetBLSM.R @ 70:cc028157502a branch-tests

scripts R
author Maria Panteli
date Fri, 22 Sep 2017 16:29:32 +0100
parents
children bde45ce0eeab
comparison
equal deleted inserted replaced
69:7bed7a5966c1 70:cc028157502a
1 #df = read.csv("/Users/mariapanteli/Documents/2014-2015/Python/pythoncode/MergeBL-Smith/data/df_BLSM.csv",header=TRUE)
2 #df = read.csv("data/df_subset_remove.csv",header=TRUE)
3 #df = read.csv("/Users/mariapanteli/Documents/2014-2015/Python/pythoncode/CodeForBL/data/metadataBL_new.csv",header=TRUE)
4 #df = read.csv("/Users/mariapanteli/Documents/2014-2015/Python/pythoncode/MergeBL-Smith/data/metadata_BLSM.csv",header=TRUE)
5 #df = df[1:29182,] # BL data
6 df = read.csv('data/df_and_clusters.csv', header=T)
7
8 source("MetadataPlots.R")
9
10 ## for plos use arial
11 #install.packages("extrafont")
12 library(extrafont)
13 font_import()
14 loadfonts()
15 Arial <- Type1Font(family="Arial", metrics=c("ArialMT.afm","arial-BoldMT.afm","Arial-ItalicMT.afm", "Arial-BoldItalicMT.afm"))
16 postscriptFonts(Arial=Arial)
17 par(family="Arial")
18
19 #pdf(file="data/country_distribution_BL.pdf")
20 pdf(file="data/country_distribution.pdf")
21 PlotCountryCounts(df)
22 dev.off()
23 postscript(file="data/country_distribution.eps")
24 PlotCountryCounts(df)
25 dev.off()
26
27 pdf(file="data/year_distribution.pdf", width=6, height=4)
28 PlotYearDistribution(df)
29 dev.off()
30 postscript("data/year_distribution.eps", width=10)
31 PlotYearDistribution(df)
32 dev.off()
33 #PlotBarChart(df, cat="Year", ordercat="REGION", mincount=10)
34
35 #pdf(file="data/language_distribution_BL.pdf")
36 levels(df$Language)[which(levels(df$Language)=="Southwestern Caribbean Creole English")]="SouthW Carib. Creole English"
37 df$Language[which(df$Language=="Southwestern Caribbean Creole English")] = "SouthW Carib. Creole English"
38 levels(df$Language)[which(levels(df$Language)=="Lesser Antillean Creole French")]="Lesser Antil. Creole French"
39 df$Language[which(df$Language=="Lesser Antillean Creole French")] = "Lesser Antil. Creole French"
40 df$REGION[which(df$Country=="French Guiana")] = "South America"
41 pdf(file="data/language_distribution.pdf")
42 PlotBarChart(df, cat="Language", ordercat="Region", mincount=10)
43 dev.off()
44 postscript("data/language_distribution.eps", width=8, height=10)
45 PlotBarChart(df, cat="Language", ordercat="Region", mincount=10)
46 dev.off()
47
48 #language phylogeny
49 df = read.csv('data/metadata_BLSM_language.csv', header=T)
50 pdf(file="data/language_iso3_iso1.pdf")
51 PlotBarChart(df, cat="Language_iso3", ordercat="Language_iso1", mincount=10)
52 dev.off()
53
54 # PlotCountryCounts(df)
55 # PlotCountryCultureNcounts(df, mincount=20)
56 # PlotCountryLanguageNcounts(df, mincount=20)
57 # PlotYearDistribution(df)
58 # PlotLanguageDistribution(df)
59 # PlotCultureDistribution(df)
60 # PlotNxNcounts(df, cat1="Country", cat2="Genre_Album", mincount=20)
61
62 df = read.csv('data/df_and_clusters.csv', header=T)
63 #PlotBarChart(df, cat="Clusters", ordercat="CountryLang", mincount=1,legend=F)
64 df$REGION[which(df$Country=="French Guiana")] = "South America"
65 g = ggplot(df,aes(df$Clusters, fill=df$REGION))+geom_bar()
66 levels(df$REGION)[which(levels(df$REGION)=="South America")]="S. America"
67 levels(df$REGION)[which(levels(df$REGION)=="North America")]="N. America"
68
69 #library(rworldmap)
70 #wrld = getMap()
71 #regiondata<-wrld@data[,c("ADMIN","GEO3", "Stern")]
72 #df<-merge(df,regiondata,by.x="Country",by.y="ADMIN",all.x=T)
73
74 #cluster_labels_df = read.csv('data/clusters_top3_labels.csv')
75 cluster_labels_df = read.csv('data/clusters_top3_countries.csv')
76 cluster_labels = paste(cluster_labels_df[,1],cluster_labels_df[,2],cluster_labels_df[,3],sep="")
77 #df$CountryLang = as.factor(paste(df$Country, df$Language, sep="-"))
78
79 countrycounts = table(df$Clusters,df$Country)
80 library(cluster)
81 library(ape)
82 library(gridExtra)
83 library(ggdendro)
84 library(dendextend)
85 hc = hclust(dist(countrycounts), method="average")
86 hc2=hc
87 #hc2$labels = as.character(1:length(cluster_labels))
88 hc2$labels = ""
89 #dhc <- as.dendrogram(hc2)
90 # library(dynamicTreeCut)
91 # clusters <- cutreeDynamic(hc2, minClusterSize = k_clust,method = "tree")
92 # clusters <- clusters[order.dendrogram(dhc)]
93 # clusters_numbers <- unique(clusters) - (0 %in% clusters)
94 # n_clusters <- length(clusters_numbers)
95 # library(colorspace)
96 # cols <- rainbow_hcl(n_clusters)
97 # dhc <- hc2 %>% as.dendrogram %>%
98 # set("branches_k_color", k=k_clust) %>% branches_attr_by_clusters(clusters, values = cols)
99 k_clust = 5
100 dhc <- hc2 %>% as.dendrogram %>%
101 set("branches_k_color", k=k_clust) %>% set("branches_lwd", 0.7) %>%
102 set("labels_cex", 0.6) %>% set("labels_colors", k=k_clust) %>%
103 set("leaves_pch", 19) %>% set("leaves_cex", 0.5)
104 #ddata <- dendro_data(dhc, type = "rectangle")
105 ddata <- as.ggdend(dhc)
106 p <- ggplot(ddata)+coord_flip()
107 #p <- ggplot(segment(ddata)) +
108 # geom_segment(aes(x = x, y = y, xend = xend, yend = yend, colour=ddata$segments$col)) +
109 # coord_flip() + theme_dendro() + theme(legend.position="none") +
110 # geom_text(aes(x = x, y = y, label = label, angle = -90, hjust = 0.5, vjust=1.3, colour=ddata$labels$col), data= label(ddata))
111
112 #dend <- hc2 %>% as.dendrogram %>%
113 # set("branches_k_color", k = 5) %>% set("branches_lwd", 0.7) %>%
114 # set("labels_cex", 0.6) %>% set("labels_colors", k = 5) %>%
115 # set("leaves_pch", 19) %>% set("leaves_cex", 0.5)
116 #ggd1 <- as.ggdend(dend)
117 #pp <- ggplot(ggd1, horiz = TRUE)
118
119 library(stringr)
120 for (i in 1:length(cluster_labels)){
121 cl = cluster_labels[i]
122 cl = str_replace_all(cl, "[(']", "")
123 cl = str_replace_all(cl, "[|]", "-")
124 cl = str_replace_all(cl, ", ", " (")
125 cl = str_replace_all(cl, "[)]", "), ")
126 cl = str_replace_all(cl, "nan", "NA")
127 #cl = paste(cl, "cluster",i)
128 cluster_labels[i] = cl
129 }
130 #cluster_idx = paste("cluster",1:length(cluster_labels))
131 #df$Clusters = as.factor(df$Clusters)
132 df$Clusters = factor(x=df$Clusters,levels=hc$labels[hc$order])
133 cluster_labels = cluster_labels[hc$order]
134 #g = ggplot(df,aes(as.factor(df$Clusters), fill=df$CountryLang))+geom_bar()
135 #g = ggplot(df,aes(Clusters, fill=REGION))+geom_bar()+facet_grid(~REGION,space="free",scales="free")#,scales="free")
136 g = ggplot(df,aes(as.factor(df$Clusters), fill=df$Region))+geom_bar()
137 #g = ggplot(df,aes(as.factor(df$Clusters), fill=df$REGION))+geom_bar()
138 g = g+scale_x_discrete(labels=cluster_labels)
139 #g = g+scale_y_continuous(position="right")
140 #g = g+scale_fill_brewer(palette="Paired")#+scale_fill_grey()
141 g = g+scale_fill_brewer(palette="Paired")#+scale_fill_grey()
142 #g = g+labs(y="Counts", x="Top 3 country-language tags in each cluster")+coord_flip()+theme_bw()#+guides(fill="none")
143 g = g+labs(y="Counts", x="Clusters")+coord_flip()+theme_bw()#+guides(fill="none")
144 #g = g+labs(y="Counts", x="Clusters")+coord_flip()+theme_bw()#+guides(fill="none")
145 #g = g+guides(fill = guide_legend(title = "Region"))+theme(legend.position=c(.9,.8),legend.margin = unit(0, "cm"),legend.key.size = unit(0.3, "cm"),legend.title = element_text(size=10),legend.text = element_text(size=10))
146 #g = g+guides(fill = guide_legend(title = "Region"))+theme(legend.position="left",legend.margin = unit(0, "cm"),legend.key.size = unit(0.3, "cm"),legend.title = element_text(size=9),legend.text = element_text(size=9))
147 g = g+guides(fill = guide_legend(title = "Region"))+theme(legend.position="top",legend.title = element_text(size=9),legend.text = element_text(size=9))
148 g = g+theme(panel.border = element_rect(colour = "white"),strip.background=element_rect(fill="white"),strip.text.x = element_blank())
149 #g = g+theme(axis.text.y = element_text(colour = ddata$labels$col))
150 ggsave('data/clusters_top3.pdf',plot=g)
151 ggsave('data/clusters_top3.eps',plot=g)
152
153 #g_legend<-function(a.gplot){
154 # tmp <- ggplot_gtable(ggplot_build(a.gplot))
155 # leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
156 # legend <- tmp$grobs[[leg]]
157 # return(legend)}
158 #mylegend<-g_legend(g)
159
160 #pdf(file="data/clusters_top3_hclust.pdf", width=12, height=5)
161 #grid.arrange(arrangeGrob(g + theme(legend.position="none"),p + theme(legend.position="none"),nrow=1, widths=c(4,1)),mylegend, nrow=2,heights=c(10, 1))
162 #dev.off()
163
164 #grid.arrange(arrangeGrob(g,p,nrow=1, ncol=2))
165 #ggsave('data/clusters_top3_hclust.pdf',plot=g_comb)
166 #g=g+annotate(x=20, y=1:18, label=cluster_idx)+geom_text(aes(x=20,y=1:18,label=cluster_idx))
167 #+guides(fill = guide_legend(title = "Region"))
168 #grid.draw(cbind(ggplotGrob(g), ggplotGrob(pp), size = "last"))