geanders / noaastormevents_paper

0 stars 0 forks source link

Make the clustering dendrogram a bit prettier #10

Open geanders opened 4 years ago

geanders commented 4 years ago

We could work some on the formatting of the clustering plot (event types by episode) to make it pretty. Some resources with example code to check include:

theresekon commented 3 years ago

I am trying to add in shaded rectangles around certain clusters in the dendrogram using the rect.dendrogram function but I'm getting an error message reading:

Error in strwidth(s, cex = par("cex"), ...) : plot.new has not been called yet

I have colored the different branches of the dendrogram, so adding in the rectangle might not be necessary. I can show you during our meeting today.

Here is the code that I have written for the dendrogram:

b <- events_2019 %>%
  group_by(EVENT_TYPE) %>%
  mutate(n_events = n()) %>%
  ungroup() %>%
  filter(n_events >= 50) %>%
  group_by(EPISODE_ID, EVENT_TYPE) %>%
  summarize(n = n()) %>%
  ungroup() %>%
  spread(key = EPISODE_ID, value = n, fill = 0) %>%
  tibble::remove_rownames() %>%
  as.data.frame() %>%
  tibble::column_to_rownames(var = "EVENT_TYPE") %>%
  dist("canberra") %>%
  hclust() 

dend <- as.dendrogram(b, hang = 0.3)%>% 
  set("leaves_pch", 19) %>% 
  set("leaves_col", "green") %>% 
  set("nodes_cex", 0.7) %>%
  set("branches_k_color", value = c("black", "skyblue", "orange", "black", "black", "blue","red", "hot pink", "purple", "black", "black", "brown", "black", "skyblue"), k = 14) 

dend %>% 
  plot(horiz=TRUE, axes=FALSE, main = "Event Type Cluster Analysis", col.main = "dark green") 

rect.dendrogram(dend, k = 11)