使用R / ggplot复制数据可视化

时间:2018-01-05 17:34:15

标签: r ggplot2

使用ggplot2

复制我在打印介质中看到的可视化效果

上下文
我一直希望数据可视化对于非数据人员更具吸引力/美感,非数据人员是我工作的大多数人(营销人员,管理人员等利益相关者) - 我注意到当可视化看起来像学术时 - 出版质量(标准ggplot2美学)他们倾向于假设他们不能理解它并且不打扰尝试,首先打败可视化的整个目的。然而,当它看起来更像图形时(就像你可能在网站或营销材料上看到的那样),他们专注并试图理解可视化,通常是成功的。通常我们最终会从这些类型的可视化中进行最有趣的讨论,因此这是我的最终目标。

可视化The vis I'd like to replicate in R/ggplot2

这是我在一些营销手册上看到的关于geo的网络流量设备份额的内容,虽然它实际上有点繁忙且不清楚,但它比我在标准中创建的类似堆叠条形图更能产生共鸣 - 我有我没有丝毫想到如何在ggplot2内复制这样的东西,任何尝试都会受到高度赞赏!以下是data.table中使用的一些示例整洁数据:

structure(list(country = c("Argentina", "Argentina", "Argentina", 
                       "Brazil", "Brazil", "Brazil", "Canada",
                       "Canada", "Canada", "China", "China",
                       "China", "Japan", "Japan", "Japan", "Spain",
                       "Spain", "Spain", "UK", "UK", "UK", "USA",
                       "USA", "USA"), 
           device_type = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 
                                     2L, 3L, 1L, 2L, 3L, 1L, 2L, 
                                     3L, 1L, 2L, 3L, 1L, 2L, 3L, 
                                     1L, 2L, 3L), 
                                   class = "factor", 
                                   .Label = c("desktop", 
                                              "mobile", 
                                              "multi")), 
           proportion = c(0.37, 0.22, 0.41, 0.3, 0.31, 0.39, 
                          0.35, 0.06, 0.59, 0.19, 0.2, 0.61, 
                          0.4, 0.18, 0.42, 0.16, 0.28, 0.56, 
                          0.27, 0.06, 0.67, 0.37, 0.08, 0.55)),
      .Names = c("country", "device_type", "proportion"), 
      row.names = c(NA, -24L), 
      class = c("data.table", "data.frame"))

2 个答案:

答案 0 :(得分:4)

您可以尝试使用" ggalluvial"包及其各自的" geom"。

Chek this out

答案 1 :(得分:4)

您还可以考虑googleVis

library(googleVis)

dat <- structure(list(country = c("Argentina", "Argentina", "Argentina", 
                           "Brazil", "Brazil", "Brazil", "Canada",
                           "Canada", "Canada", "China", "China",
                           "China", "Japan", "Japan", "Japan", "Spain",
                           "Spain", "Spain", "UK", "UK", "UK", "USA",
                           "USA", "USA"), 
               device_type = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 
                                         2L, 3L, 1L, 2L, 3L, 1L, 2L, 
                                         3L, 1L, 2L, 3L, 1L, 2L, 3L, 
                                         1L, 2L, 3L), 
                                       class = "factor", 
                                       .Label = c("desktop", 
                                                  "mobile", 
                                                  "multi")), 
               proportion = c(0.37, 0.22, 0.41, 0.3, 0.31, 0.39, 
                              0.35, 0.06, 0.59, 0.19, 0.2, 0.61, 
                              0.4, 0.18, 0.42, 0.16, 0.28, 0.56, 
                              0.27, 0.06, 0.67, 0.37, 0.08, 0.55)),
          .Names = c("country", "device_type", "proportion"), 
          row.names = c(NA, -24L), 
          class = c("data.table", "data.frame"))

link_order <- unique(dat$country)
node_order <- unique(as.vector(rbind(dat$country, as.character(dat$device_type))))

link_cols <- data.frame(color = c('#ffd1ab', '#ff8d14', '#ff717e', '#dd2c40', '#d6b0ea', 
                        '#8c4fab','#00addb','#297cbe'), 
                        country = c("UK", "Canada", "USA", "China", "Spain", "Japan", "Argentina", "Brazil"),
                        stringsAsFactors = F)

node_cols <- data.frame(color = c("#ffc796", "#ff7100", "#ff485b", "#d20000", 
                                  "#cc98e6", "#6f2296", "#009bd2", "#005daf", 
                                  "grey", "grey", "grey"),
                        type = c("UK", "Canada", "USA", "China", "Spain", "Japan", 
                                 "Argentina", "Brazil", "multi", "desktop", "mobile"))

link_cols2 <- sapply(link_order, function(x) link_cols[x == link_cols$country, "color"])
node_cols2 <- sapply(node_order, function(x) node_cols[x == node_cols$type, "color"])

actual_link_cols <- paste0("[", paste0("'", link_cols2,"'", collapse = ','), "]")
actual_node_cols <- paste0("[", paste0("'", node_cols2,"'", collapse = ','), "]")

opts <- paste0("{
        link: { colorMode: 'source',
               colors: ", actual_link_cols ," },
        node: {colors: ", actual_node_cols ,"}}")

Sankey <- gvisSankey(dat, 
                     from = "country", 
                     to = "device_type", 
                     weight = "proportion",
                     options = list(height = 500, width = 1000, sankey = opts))


plot(Sankey) 

enter image description here