Advertisement
Guest User

Untitled

a guest
May 25th, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.87 KB | None | 0 0
  1. my_special_fancy_legend = function(
  2.     values, palette,
  3.     direction = 1,
  4.     borders = NA,
  5.     legend_title = NULL,
  6.     font_size = 11,
  7.     expand_size = 1,
  8.     width = 0.1,
  9.     spacing = "natural"
  10. ) {
  11.     require(ggplot2)
  12.     require(RColorBrewer)
  13.     if (!(spacing %in% c("natural", "constant"))) stop("spacing must be either 'natural' or 'constant'")
  14.     values = as.numeric(values)
  15.    
  16.     colors = brewer.pal(length(values) - 1, palette)
  17.    
  18.     if (direction == -1) colors = rev(colors)
  19.  
  20.     values = sort(unique(values))
  21.     inf_values = which(is.infinite(values))
  22.     if (length(inf_values) != 0) values = values[-inf_values]
  23.     plotcolors = colors
  24.  
  25.     labels = values
  26.     if (spacing == "constant") {
  27.         values = 1:length(values)
  28.     }
  29.    
  30.     cbar_df = data.frame(stringsAsFactors = FALSE,
  31.         y = values,
  32.         yend = c(values[-1], NA),
  33.         color = as.character(1:length(values))
  34.     )[-length(values),]
  35.  
  36.     xmin = 1 - width/2
  37.     xmax = 1 + width/2
  38.  
  39.     cbar_plot = ggplot(cbar_df, aes(xmin=xmin, xmax = xmax, ymin = y, ymax = yend, fill = color)) +
  40.         geom_rect(show.legend = FALSE,
  41.                   color=borders)
  42.  
  43.     if (any(inf_values == 1)) { # < arrow
  44.         firstv = values[1]
  45.         polystart = data.frame(
  46.             x = c(xmin, xmax, 1),
  47.             y = c(rep(firstv, 2), firstv - diff(range(values)) / 10)
  48.         )
  49.         plotcolors = plotcolors[-1]
  50.         cbar_plot = cbar_plot +
  51.             geom_polygon(data=polystart, aes(x=x, y=y),
  52.                         show.legend = FALSE,
  53.                         inherit.aes = FALSE,
  54.                         fill = colors[1],
  55.                         color=borders)
  56.     }
  57.     if (any(inf_values > 1)) { # > arrow
  58.         lastv = values[length(values)]
  59.         polyend = data.frame(
  60.             x = c(xmin, xmax, 1),
  61.             y = c(rep(lastv, 2), lastv + diff(range(values)) / 10)
  62.         )
  63.         plotcolors = plotcolors[-length(plotcolors)]
  64.         cbar_plot = cbar_plot +
  65.             geom_polygon(data=polyend, aes(x=x, y=y),
  66.                         show.legend = FALSE,
  67.                         inherit.aes = FALSE,
  68.                         fill = colors[length(colors)],
  69.                         color=borders)
  70.     }
  71.  
  72.     cbar_plot = cbar_plot +
  73.         geom_segment(data=data.frame(y = values, yend = values), aes(y=y, yend=yend), x = xmin - 0.05, xend = xmax, inherit.aes = FALSE) +
  74.         annotate(geom = 'text', x = xmin - 0.1, y = values, label = labels) +
  75.         scale_x_continuous(expand = c(expand_size,expand_size)) +
  76.         scale_fill_manual(values=plotcolors) +
  77.         coord_flip() +
  78.         theme_void(base_size = font_size)
  79.        
  80.     if (!is.null(legend_title)) {
  81.         cbar_plot = cbar_plot +
  82.             annotate(geom = 'text', x = xmax + 0.05, y = mean(range(values)), label = legend_title)
  83.     }
  84.        
  85.     cbar_plot
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement