install.packages("tidyverse") install.packages("hrbrthemes") install.packages("viridis") # Libraries library(tidyverse) library(hrbrthemes) library(viridis) ## 5 HISTOGRAMA #Histograma # Load dataset from github data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/1_OneNum.csv", header=TRUE) # Make the histogram data %>% filter( price<300 ) %>% ggplot( aes(x=price)) + stat_bin(breaks=seq(0,300,10), fill="#69b3a2", color="#e9ecef", alpha=0.9) + ggtitle("Night price distribution of Airbnb appartements")# + #theme_ipsum() ##6 GRÁFICO DE DENSIDAD #Histograma utilizando density # Libraries library(tidyverse) library(hrbrthemes) library(viridis) # Load dataset from github data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/1_OneNum.csv", header=TRUE) # Make the histogram data %>% filter( price<300 ) %>% ggplot( aes(x=price)) + geom_density(fill="#69b3a2", color="#e9ecef", alpha=0.8) + ggtitle("Night price distribution of Airbnb appartements")# + # theme_ipsum() #Utilizar density para dos variables de forma inteligente (violín?) data <- data.frame( x = rnorm(1000), y = rnorm(1000, mean=2) ) data %>% ggplot( aes(x) ) + stat_bin( aes(x = x, y = ..density..), binwidth = diff(range(data$x))/30, fill="#69b3a2" ) + geom_label( aes(x=4.5, y=0.25, label="variable1"), color="#69b3a2") + geom_density( aes(x = y, y = -..density..), binwidth = diff(range(data$x))/30, fill= "#404080") + geom_label( aes(x=4.5, y=-0.25, label="variable2"), color="#404080") + theme_ipsum() + xlab("value of x") ##7 GRÁFICO DE DISPERSIÓN # Libraries library(tidyverse) library(hrbrthemes) library(viridis) # Load dataset from github data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/2_TwoNum.csv", header=T, sep=",") %>% dplyr::select(GrLivArea, SalePrice) # plot data %>% ggplot( aes(x=GrLivArea, y=SalePrice/1000)) + geom_point(color="#69b3a2", alpha=0.8) + ggtitle("Ground living area partially explains sale price of apartments") + theme_ipsum() + theme( plot.title = element_text(size=12) ) + ylab('Sale price (k$)') + xlab('Ground living area') library(ggExtra) # create a ggplot2 scatterplot p <- data %>% ggplot( aes(x=GrLivArea, y=SalePrice/1000)) + geom_point(color="#69b3a2", alpha=0.8) + theme_ipsum() + theme( legend.position="none" ) # add marginal histograms ggExtra::ggMarginal(p, type = "histogram", color="grey") ##8 GRÁFICO DE DENSIDAD # Libraries library(tidyverse) library(hrbrthemes) library(viridis) library(patchwork) # Dataset: a <- data.frame( x=rnorm(20000, 10, 1.2), y=rnorm(20000, 10, 1.2), group=rep("A",20000)) b <- data.frame( x=rnorm(20000, 14.5, 1.2), y=rnorm(20000, 14.5, 1.2), group=rep("B",20000)) c <- data.frame( x=rnorm(20000, 9.5, 1.5), y=rnorm(20000, 15.5, 1.5), group=rep("C",20000)) data <- do.call(rbind, list(a,b,c)) p1 <- data %>% ggplot( aes(x=x, y=y)) + geom_point(color="#69b3a2", size=2) + theme_ipsum() + theme( legend.position="none" ) p2 <- ggplot(data, aes(x=x, y=y) ) + stat_density_2d(aes(fill = ..density..), geom = "raster", contour = FALSE) + scale_x_continuous(expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0)) + scale_fill_viridis() + theme( legend.position='none' ) p1 + p2 ##9 DISTRIBUCIÓN MARGINAL # library library(ggplot2) library(ggExtra) # The mtcars dataset is proposed in R head(mtcars) # classic plot : p <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, size=cyl)) + geom_point() + theme(legend.position="none") # with marginal histogram p1 <- ggMarginal(p, type="histogram") # marginal density p1 p2 <- ggMarginal(p, type="density") # marginal boxplot p2 p3 <- ggMarginal(p, type="boxplot") p3 ##10 PLOT DE LÍNEA # Libraries library(tidyverse) library(hrbrthemes) library(plotly) library(patchwork) library(babynames) library(viridis) # Load dataset from github data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/3_TwoNumOrdered.csv", header=T) data$date <- as.Date(data$date) # plot data %>% ggplot( aes(x=date, y=value)) + geom_line(color="#69b3a2") + ggtitle("Evolution of Bitcoin price") + ylab("bitcoin price ($)") + theme_ipsum() ggplot(mpg, aes(displ, hwy)) + geom_point() + geom_smooth(color="#69b3a2",span = 1) + theme_ipsum() ##11 CORRELOGRAMA # Quick display of two cabapilities of GGally, to assess the distribution and correlation of variables install.packages("GGally") library(GGally) # Create data data <- data.frame( var1 = 1:100 + rnorm(100,sd=20), v2 = 1:100 + rnorm(100,sd=27), v3 = rep(1, 100) + rnorm(100, sd = 1)) data$v4 = data$var1 ** 2 data$v5 = -(data$var1 ** 2) # Check correlations (as scatterplots), distribution and print corrleation coefficient ggpairs(data, title="correlogram with ggpairs()") # Quick display of two capabilities of GGally, to assess the distribution and correlation of variables library(GGally) library(Rcpp) # From the help page: data(flea) #ggpairs(flea, columns = 2:4) #Ojo, debería ser la línea siguiente, pero se cuelga si la ejecuto ggpairs(flea, columns = 2:4,ggplot2::aes(colour=species)) ##13 GRÁFICO DE BARRAS # Libraries library(tidyverse) library(hrbrthemes) library(kableExtra) options(knitr.table.format = "html") # Load dataset from github data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/7_OneCatOneNum.csv", header=TRUE, sep=",") # Barplot data %>% filter(!is.na(Value)) %>% arrange(Value) %>% tail(20) %>% mutate(Country=factor(Country, Country)) %>% ggplot( aes(x=Country, y=Value) ) + geom_bar(stat="identity", fill="#69b3a2") + coord_flip() + theme_ipsum() + theme( panel.grid.minor.y = element_blank(), panel.grid.major.y = element_blank(), legend.position="none" ) + xlab("") + ylab("Weapon quantity (SIPRI trend-indicator value)") #Gráfico de barras CIRCULAR # Load dataset from github data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/7_OneCatOneNum.csv", header=TRUE, sep=",") # Order data tmp <- data %>% filter(!is.na(Value)) %>% arrange(desc(Value)) %>% mutate(Country=factor(Country, Country)) # Set a number of 'empty bar' empty_bar=10 # Add lines to the initial tmpset to_add = matrix(NA, empty_bar, ncol(tmp)) colnames(to_add) = colnames(tmp) tmp=rbind(tmp, to_add) tmp$id=seq(1, nrow(tmp)) # Get the name and the y position of each label label_tmp=tmp number_of_bar=nrow(label_tmp) angle= 90 - 360 * (label_tmp$id-0.5) /number_of_bar # I substract 0.5 because the letter must have the angle of the center of the bars. Not extreme right(1) or extreme left (0) label_tmp$hjust<-ifelse( angle < -90, 1, 0) label_tmp$angle<-ifelse(angle < -90, angle+180, angle) label_tmp$Country <- gsub("United States", "US", label_tmp$Country) label_tmp$Country <- paste(label_tmp$Country, " (", label_tmp$Value,")", sep="") # Make the plot ggplot(tmp, aes(x=as.factor(id), y=Value)) + # Note that id is a factor. If x is numeric, there is some space between the first bar geom_bar(stat="identity", fill=alpha("#69b3a2", 0.8)) + ylim(-7000,13000) + theme_minimal() + theme( axis.text = element_blank(), axis.title = element_blank(), panel.grid = element_blank(), plot.margin = unit(rep(-1,4), "cm") ) + coord_polar(start = 0) + geom_text(data=label_tmp, aes(x=id, y=Value+200, label=Country ), color="black", fontface="bold",alpha=0.6, size=2.5, angle= label_tmp$angle, hjust=label_tmp$hjust, inherit.aes = FALSE ) + geom_text( aes(x=24, y=8000, label="Who sells more weapons?"), color="black", inherit.aes = FALSE) ##14 BOX PLOT # create a dataset data <- data.frame( name=c( rep("A",500), rep("B",500), rep("B",500), rep("C",20), rep('D', 100) ), value=c( rnorm(500, 10, 5), rnorm(500, 13, 1), rnorm(500, 18, 1), rnorm(20, 25, 4), rnorm(100, 12, 1) ) ) # sample size sample_size = data %>% group_by(name) %>% summarize(num=n()) #Tabla usada en los plots TablaFinal <- data %>% left_join(sample_size) %>% mutate(myaxis = paste0(name, "\n", "n=", num)) # geom_boxplot ggplot(TablaFinal, aes(x=myaxis, y=value, fill=name)) + geom_boxplot() # geom_boxplot con arreglos estáticos importantes ggplot(TablaFinal, aes(x=myaxis, y=value, fill=name)) + geom_boxplot() + scale_fill_viridis(discrete = TRUE) + theme_ipsum() + theme( legend.position="none", plot.title = element_text(size=11)) + ggtitle("A Boxplot") + xlab("") # geom_boxplot + geom_jitter ggplot(TablaFinal, aes(x=myaxis, y=value, fill=name)) + geom_boxplot() + scale_fill_viridis(discrete = TRUE) + geom_jitter(color="grey", size=0.7, alpha=0.5) + theme_ipsum() + theme( legend.position="none", plot.title = element_text(size=11)) + ggtitle("A Boxplot") + xlab("") ##15 VIOLIN PLOT # geom_violin con arreglos estáticos ggplot(TablaFinal, aes(x=myaxis, y=value, fill=name)) + geom_violin(width=1.4) + scale_fill_viridis(discrete = TRUE) + theme_ipsum() + theme( legend.position="none", plot.title = element_text(size=11) ) + ggtitle("Violin") + xlab("") # geom_violin + geom_jitter ggplot(TablaFinal, aes(x=myaxis, y=value, fill=name)) + geom_violin(width=1.5) + scale_fill_viridis(discrete = TRUE) + geom_jitter(color="grey", size=0.7, alpha=0.5) + theme_ipsum() + theme( legend.position="none", plot.title = element_text(size=11) ) + ggtitle("Violin + geom_jitter") + xlab("") ##16 RIDGELINE PLOT (Gráfico de Cresta) # Libraries library(tidyverse) library(hrbrthemes) library(viridis) # Load dataset from github data <- read.table("https://raw.githubusercontent.com/zonination/perceptions/master/probly.csv", header=TRUE, sep=",") data <- data %>% gather(key="text", value="value") %>% mutate(text = gsub("\\.", " ",text)) %>% mutate(value = round(as.numeric(value),0)) %>% filter(text %in% c("Almost Certainly","Very Good Chance","We Believe","Likely","About Even", "Little Chance", "Chances Are Slight", "Almost No Chance")) install.packages("ggridges") library(ggridges) data %>% mutate(text = fct_reorder(text, value)) %>% ggplot( aes(y=text, x=value, fill=text)) + geom_density_ridges(alpha=0.6, bandwidth=4) + scale_fill_viridis(discrete=TRUE) + scale_color_viridis(discrete=TRUE) + theme_ipsum() + theme( legend.position="none", panel.spacing = unit(0.1, "lines"), strip.text.x = element_text(size = 8) ) + xlab("") + ylab("Assigned Probability (%)") #Dibujado del Ridgeline plot con histogramas data %>% mutate(text = fct_reorder(text, value)) %>% ggplot( aes(y=text, x=value, fill=text)) + geom_density_ridges(alpha=0.6, stat="binline", bins=20) + scale_fill_viridis(discrete=TRUE) + scale_color_viridis(discrete=TRUE) + theme_ipsum() + theme( legend.position="none", panel.spacing = unit(0.1, "lines"), strip.text.x = element_text(size = 8) ) + xlab("") + ylab("Assigned Probability (%)") ##17 WORD CLOUD # Libraries library(tidyverse) library(hrbrthemes) library(tm) library(proustr) # Load dataset from github data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/14_SeveralIndepLists.csv", header=TRUE) to_remove <- c("_|[0-9]|\\.|function|^id|script|var|div|null|typeof|opts|if|^r$|undefined|false|loaded|true|settimeout|eval|else|artist") data <- data %>% filter(!grepl(to_remove, word)) %>% filter(!word %in% stopwords('fr')) %>% filter(!word %in% proust_stopwords()$word) # The wordcloud 2 library is the best option for wordcloud in R install.packages('wordcloud2') library(wordcloud2) # prepare a list of word (50 most frequent) mywords <- data %>% filter(artist=="nekfeu") %>% dplyr::select(word) %>% group_by(word) %>% summarize(freq=n()) %>% arrange(freq) %>% tail(30) # Make the plot wordcloud2(mywords, minRotation = -pi/2, maxRotation = -pi/2, backgroundColor = "white", color="#69b3a2") ##18 PIE CHART # Libraries library(tidyverse) library(hrbrthemes) library(viridis) library(patchwork) # create 3 data frame: data1 <- data.frame( name=letters[1:5], value=c(17,18,20,22,24) ) data2 <- data.frame( name=letters[1:5], value=c(20,18,21,20,20) ) data3 <- data.frame( name=letters[1:5], value=c(24,23,21,19,18) ) # Plot plot_pie <- function(data, vec){ ggplot(data, aes(x="name", y=value, fill=name)) + geom_bar(width = 1, stat = "identity") + coord_polar("y", start=0, direction = -1) + scale_fill_viridis(discrete = TRUE, direction=-1) + geom_text(aes(y = vec, label = rev(name), size=4, color=c( "white", rep("black", 4)))) + scale_color_manual(values=c("black", "white")) + theme_ipsum() + theme( legend.position="none", plot.title = element_text(size=14), panel.grid = element_blank(), axis.text = element_blank(), legend.margin=unit(0, "null") ) + xlab("") + ylab("") } a <- plot_pie(data1, c(10,35,55,75,93)) b <- plot_pie(data2, c(10,35,53,75,93)) c <- plot_pie(data3, c(10,29,50,75,93)) a + b + c ##19 MAPA DE CALOR install.packages('heatmaply') library(tidyverse) library(hrbrthemes) library(viridis) library(plotly) #library(d3heatmap) # Load data data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/multivariate.csv", header=T, sep=";") colnames(data) <- gsub("\\.", " ", colnames(data)) # Select a few country data <- data %>% filter(Country %in% c("France", "Sweden", "Italy", "Spain", "England", "Portugal", "Greece", "Peru", "Chile", "Brazil", "Argentina", "Bolivia", "Venezuela", "Australia", "New Zealand", "Fiji", "China", "India", "Thailand", "Afghanistan", "Bangladesh", "United States of America", "Canada", "Burundi", "Angola", "Kenya", "Togo")) %>% arrange(Country) %>% mutate(Country = factor(Country, Country)) # Matrix format mat <- data rownames(mat) <- mat[,1] mat <- mat %>% dplyr::select(-Country, -Group, -Continent) mat <- as.matrix(mat) # Heatmap #d3heatmap(mat, scale="column", dendrogram = "none", width="800px", height="80Opx", colors = "Blues") library(heatmaply) p <- heatmaply(mat, dendrogram = "none", xlab = "", ylab = "", main = "", scale = "column", margins = c(60,100,40,20), grid_color = "white", grid_width = 0.00001, titleX = FALSE, hide_colorbar = TRUE, branches_lwd = 0.1, label_names = c("Country", "Feature:", "Value"), fontsize_row = 5, fontsize_col = 5, labCol = colnames(mat), labRow = rownames(mat), heatmap_layers = theme(axis.line=element_blank()) ) p p <- heatmaply(mat, #dendrogram = "row", xlab = "", ylab = "", main = "", scale = "column", margins = c(60,100,40,20), grid_color = "white", grid_width = 0.00001, titleX = FALSE, hide_colorbar = TRUE, branches_lwd = 0.1, label_names = c("Country", "Feature:", "Value"), fontsize_row = 5, fontsize_col = 5, labCol = colnames(mat), labRow = rownames(mat), heatmap_layers = theme(axis.line=element_blank()) ) p ##21 DIAGRAMA DE VENN # Specific library install.packages('UpSetR') library(UpSetR) # Dataset input <- c( M.acuminata = 759, P.dactylifera = 769, A.thaliana = 1187, O.sativa = 1246, S.bicolor = 827, B.distachyon = 387, "P.dactylifera&M.acuminata" = 467, "O.sativa&M.acuminata" = 29, "A.thaliana&O.sativa" = 6, "S.bicolor&A.thaliana" = 9, "O.sativa&P.dactylifera" = 32, "S.bicolor&P.dactylifera" = 49, "S.bicolor&M.acuminata" = 49, "B.distachyon&O.sativa" = 547, "S.bicolor&O.sativa" = 1151, "B.distachyon&A.thaliana" = 10, "B.distachyon&M.acuminata" = 9, "B.distachyon&S.bicolor" = 402, "M.acuminata&A.thaliana" = 155, "A.thaliana&P.dactylifera" = 105, "B.distachyon&P.dactylifera" = 25, "S.bicolor&O.sativa&P.dactylifera" = 42, "B.distachyon&O.sativa&P.dactylifera" = 12, "S.bicolor&O.sativa&B.distachyon" = 2809, "B.distachyon&O.sativa&A.thaliana" = 18, "S.bicolor&O.sativa&A.thaliana" = 40, "S.bicolor&B.distachyon&A.thaliana" = 14, "O.sativa&B.distachyon&M.acuminata" = 28, "S.bicolor&B.distachyon&M.acuminata" = 13, "O.sativa&M.acuminata&P.dactylifera" = 35, "M.acuminata&S.bicolor&A.thaliana" = 21, "B.distachyon&M.acuminata&A.thaliana" = 7, "O.sativa&M.acuminata&A.thaliana" = 13, "M.acuminata&P.dactylifera&A.thaliana" = 206, "P.dactylifera&A.thaliana&S.bicolor" = 4, "O.sativa&A.thaliana&P.dactylifera" = 6, "S.bicolor&O.sativa&M.acuminata" = 64, "S.bicolor&M.acuminata&P.dactylifera" = 19, "B.distachyon&A.thaliana&P.dactylifera" = 3, "B.distachyon&M.acuminata&P.dactylifera" = 12, "B.distachyon&S.bicolor&P.dactylifera" = 23, "M.acuminata&B.distachyon&S.bicolor&A.thaliana" = 54, "P.dactylifera&S.bicolor&O.sativa&M.acuminata" = 62, "B.distachyon&O.sativa&M.acuminata&P.dactylifera" = 18, "S.bicolor&B.distachyon&O.sativa&A.thaliana" = 206, "B.distachyon&M.acuminata&O.sativa&A.thaliana" = 29, "O.sativa&M.acuminata&A.thaliana&S.bicolor" = 71, "M.acuminata&O.sativa&P.dactylifera&A.thaliana" = 28, "B.distachyon&M.acuminata&O.sativa&A.thaliana" = 7, "B.distachyon&S.bicolor&P.dactylifera&A.thaliana" = 11, "B.distachyon&O.sativa&P.dactylifera&A.thaliana" = 5, "A.thaliana&P.dactylifera&S.bicolor&O.sativa" = 21, "M.acuminata&S.bicolor&P.dactylifera&A.thaliana" = 23, "M.acuminata&B.distachyon&S.bicolor&P.dactylifera" = 24, "M.acuminata&O.sativa&S.bicolor&B.distachyon" = 368, "P.dactylifera&B.distachyon&S.bicolor&O.sativa" = 190, "P.dactylifera&B.distachyon&S.bicolor&O.sativa&A.thaliana" = 258, "P.dactylifera&M.acuminata&S.bicolor&B.distachyon&O.sativa" = 685, "M.acuminata&S.bicolor&B.distachyon&O.sativa&A.thaliana" = 1458, "S.bicolor&M.acuminata&P.dactylifera&O.sativa&A.thaliana" = 149, "B.distachyon&M.acuminata&P.dactylifera&O.sativa&A.thaliana" = 80, "M.acuminata&S.bicolor&B.distachyon&P.dactylifera&A.thaliana" = 113, "M.acuminata&S.bicolor&B.distachyon&P.dactylifera&O.sativa&A.thaliana" = 7674 ) # Plot upset(fromExpression(input), nintersects = 40, nsets = 6, order.by = "freq", decreasing = T, mb.ratio = c(0.6, 0.4), number.angles = 0, text.scale = 1.1, point.size = 2.8, line.size = 1) ##22 Treemap # libraries library(tidyverse) library(treemap) # Load dataset from github data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/11_SevCatOneNumNestedOneObsPerGroup.csv", header=T, sep=";") data[ which(data$value==-1),"value"] <- 1 colnames(data) <- c("Continent", "Region", "Country", "Pop") # Plot library(treemap) p <- treemap(data, # data index=c("Continent", "Region", "Country"), vSize="Pop", type="index", # Main title="", palette="Dark2", # Borders: border.col=c("black", "grey", "grey"), border.lwds=c(1,0.5,0.1), # Labels fontsize.labels=c(0.7, 0.4, 0.3), fontcolor.labels=c("white", "white", "black"), fontface.labels=1, bg.labels=c("transparent"), align.labels=list( c("center", "center"), c("left", "top"), c("right", "bottom")), overlap.labels=0.5, inflate.labels=T ) ##23 Dendogramas # libraries library(ggraph) library(igraph) library(tidyverse) library(dendextend) install.packages('colormap') library(colormap) library(kableExtra) options(knitr.table.format = "html") # create a data frame data=data.frame( level1="CEO", level2=c( rep("boss1",4), rep("boss2",4)), level3=paste0("mister_", letters[1:8]) ) # transform it to a edge list! edges_level1_2 = data %>% select(level1, level2) %>% unique %>% rename(from=level1, to=level2) edges_level2_3 = data %>% select(level2, level3) %>% unique %>% rename(from=level2, to=level3) edge_list=rbind(edges_level1_2, edges_level2_3) # Now we can plot that mygraph <- graph_from_data_frame( edge_list ) ggraph(mygraph, layout = 'dendrogram', circular = FALSE) + geom_edge_diagonal() + geom_node_point(color="#69b3a2", size=3) + geom_node_text( aes( label=c("CEO", "Manager", "Manager", LETTERS[8:1]) ), hjust=c(1,0.5, 0.5, rep(0,8)), nudge_y = c(-.02, 0, 0, rep(.02,8)), nudge_x = c(0, .3, .3, rep(0,8)) ) + theme_void() + coord_flip() + scale_y_reverse() ##Ahora armando clusters de datos #Extraido de http://www.sthda.com/english/wiki/beautiful-dendrogram-visualizations-in-r-5-must-known-methods-unsupervised-machine-learning # Load data data(USArrests) # Compute distances and hierarchical clustering dd <- dist(scale(USArrests), method = "euclidean") hc <- hclust(dd, method = "ward.D2") # Put the labels at the same height: hang = -1 plot(hc, hang = -1, cex = 0.6) # Convert hclust into a dendrogram and plot hcd <- as.dendrogram(hc) # Define nodePar nodePar <- list(lab.cex = 0.6, pch = c(NA, 19), cex = 0.7, col = "blue") # Horizontal plot plot(hcd, xlab = "Height", nodePar = nodePar, horiz = TRUE) # Triangle plot plot(hcd, xlab = "Height",type = "triangle", nodePar = nodePar, horiz = TRUE) # Fan install.packages("ape") library("ape") plot(as.phylo(hc), type = "fan") # Cut the dendrogram into 4 clusters colors = c("red", "blue", "green", "black") clus4 = cutree(hc, 4) plot(as.phylo(hc), type = "fan", tip.color = colors[clus4], label.offset = 1, cex = 0.7) #mapa de calor con dendograma install.packages('heatmaply') library(tidyverse) library(hrbrthemes) library(viridis) library(plotly) #library(d3heatmap) # Load data data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/multivariate.csv", header=T, sep=";") colnames(data) <- gsub("\\.", " ", colnames(data)) # Select a few country data <- data %>% filter(Country %in% c("France", "Sweden", "Italy", "Spain", "England", "Portugal", "Greece", "Peru", "Chile", "Brazil", "Argentina", "Bolivia", "Venezuela", "Australia", "New Zealand", "Fiji", "China", "India", "Thailand", "Afghanistan", "Bangladesh", "United States of America", "Canada", "Burundi", "Angola", "Kenya", "Togo")) %>% arrange(Country) %>% mutate(Country = factor(Country, Country)) # Matrix format mat <- data rownames(mat) <- mat[,1] mat <- mat %>% dplyr::select(-Country, -Group, -Continent) mat <- as.matrix(mat) library(heatmaply) p <- heatmaply(mat, #dendrogram = "row", xlab = "", ylab = "", main = "", scale = "column", margins = c(60,100,40,20), grid_color = "white", grid_width = 0.00001, titleX = FALSE, hide_colorbar = TRUE, branches_lwd = 0.1, label_names = c("Country", "Feature:", "Value"), fontsize_row = 5, fontsize_col = 5, labCol = colnames(mat), labRow = rownames(mat), heatmap_layers = theme(axis.line=element_blank()) ) p