【问题标题】:custom fill color in ggvis (and other options)ggvis(和其他选项)中的自定义填充颜色
【发布时间】:2014-07-17 18:01:09
【问题描述】:

我正在尝试使用 ggvis 创建 2014 赛季的 NFL 赛程强度图表。数据来自 FootballOutsiders.com,稍后我将制作一个 Shiny 应用程序,该应用程序会在赛季期间更新时自动从网站上抓取数据。下面的示例非常接近,但我想进行一些修改。我想...

  1. 在图表的每个单元格中包含“防御”的数值,因此可视化效果类似于原始的“df”数据框。

  2. 自定义色阶,使正值越来越橙色,负值越来越蓝(即,负数越多 = 越蓝)。

  3. #2 的替代方法是制作从橙色到蓝色的渐变,并在“防御”值接近零时使不透明度降低到 0.5。

  4. 可以选择 NA 的颜色,因为它目前在图表中显示为黑色。

我一直在修补add_scale()props(),但到目前为止没有任何效果。

这是图表:

这是数据:

df <- structure(list(team = c("ARI", "ATL", "BAL", "BUF", "CAR", "CHI", 
"CIN", "CLE", "DAL", "DEN", "DET", "GB", "HOU", "IND", "JAX", 
"KC", "MIA", "MIN", "NE", "NO", "NYG", "NYJ", "OAK", "PHI", "PIT", 
"SD", "SEA", "SF", "STL", "TB", "TEN", "WAS"), w1 = c(17.5, -5.8, 
-12.6, 8.7, -6.8, -13.8, -8.7, 4, -4.6, 0.9, -11.4, -25.9, 4.2, 
-0.2, 4.9, 4.2, 4.2, -5.7, 2.4, 13.5, -0.8, 10.3, -5.6, 10.9, 
8.2, -16.4, 14.4, 13.8, 10.5, -15.7, -6.7, 2.5), w2 = c(-11.4, 
-12.6, 4, 2.4, -0.8, -4.6, 13.5, -5.8, 4.2, -6.7, -15.7, -5.6, 
10.3, 4.9, 4.2, -0.2, -13.8, 4.2, 10.5, 8.2, -16.4, 14.4, 2.5, 
0.9, -8.7, -25.9, 17.5, 8.7, -6.8, -5.7, 13.8, 10.9), w3 = c(-4.6, 
-6.8, 8.2, 17.5, 4, -5.6, 4.2, -8.7, -5.7, -25.9, 14.4, -0.8, 
-11.4, 10.9, 0.9, 2.4, -6.7, -5.8, 10.3, 10.5, 2.5, 8.7, 4.2, 
4.2, -15.7, -13.8, -0.2, -16.4, 13.8, 13.5, -12.6, 4.9), w4 = c(NA, 
10.5, -15.7, 2.5, -8.7, 14.4, NA, NA, -5.8, NA, -5.6, 8.7, -13.8, 
4.2, 17.5, 4.2, 10.3, 13.5, -6.7, 13.8, 4.2, -0.8, 2.4, -4.6, 
-6.8, 10.9, NA, 4.9, NA, 4, 0.9, -11.4), w5 = c(-0.2, -11.4, 
0.9, -0.8, 8.7, -15.7, 4.2, 4.2, 2.5, -16.4, -13.8, 10.5, 13.8, 
-8.7, 4, -4.6, NA, 14.4, -12.6, -6.8, 13.5, 17.5, NA, -5.7, 10.9, 
-5.6, 4.2, -6.7, 4.9, -5.8, 8.2, -25.9), w6 = c(4.2, 8.7, -6.8, 
4.2, -12.6, 13.5, -15.7, 4, -25.9, -5.6, 10.5, 2.4, 0.9, 2.5, 
4.2, NA, 14.4, -0.8, -13.8, NA, 4.9, -0.2, 17.5, -11.4, 8.2, 
10.3, 13.8, -5.7, -4.6, -8.7, 10.9, -16.4), w7 = c(10.3, -8.7, 
13.5, 10.5, 14.4, 2.4, 0.9, 10.9, -11.4, -4.6, -5.8, -15.7, 4, 
-12.6, 8.2, 17.5, 8.7, -13.8, -5.6, -0.8, 13.8, 4.2, -16.4, NA, 
2.5, -6.7, -5.7, -0.2, -25.9, NA, 4.2, 4.2), w8 = c(4.9, -0.8, 
-12.6, -5.6, -25.9, 4.2, -8.7, 10.3, 4.2, 17.5, 13.5, -5.8, 4.2, 
4, 2.4, -5.7, 10.9, -6.8, 8.7, 14.4, NA, -13.8, 8.2, -16.4, 0.9, 
-0.2, -15.7, NA, -6.7, 10.5, 2.5, 13.8), w9 = c(13.8, NA, 4, 
NA, -5.8, NA, 10.9, -6.8, -16.4, 4.2, NA, NA, 4.9, -11.4, -12.6, 
-5.6, 17.5, 4.2, -0.2, -15.7, 0.9, -6.7, -25.9, 2.5, -8.7, 2.4, 
10.3, -5.7, -4.6, 8.2, NA, 10.5), w10 = c(-5.7, -6.8, 4.2, -6.7, 
4.9, 14.4, 8.2, -12.6, 10.9, 10.3, 2.4, 8.7, NA, NA, 13.8, -13.8, 
-0.8, NA, NA, -4.6, -25.9, 4, -0.2, -15.7, -5.6, NA, -11.4, -5.8, 
-16.4, 13.5, -8.7, NA), w11 = c(-0.8, -15.7, NA, 2.4, 13.5, 10.5, 
-5.8, 2.5, NA, -5.7, -16.4, 4.9, 8.2, 4.2, NA, -25.9, -13.8, 
8.7, 0.9, -12.6, -4.6, NA, 17.5, 14.4, 4.2, 10.3, -6.7, -11.4, 
-0.2, 4.2, 4, -6.8), w12 = c(-25.9, 8.2, -5.8, -5.6, NA, -6.8, 
2.5, 13.5, -11.4, 2.4, 4.2, 10.5, -12.6, 10.9, 0.9, 10.3, -0.2, 
14.4, -0.8, -8.7, 13.8, -13.8, -6.7, 4.2, NA, -5.7, -16.4, 4.2, 
17.5, 8.7, 4.9, -4.6), w13 = c(13.5, -16.4, 17.5, 8.2, 10.5, 
-0.8, -6.8, -13.8, 4.9, -6.7, 8.7, 4.2, 4.2, 4.2, -11.4, -0.2, 
-5.6, -15.7, 14.4, 4, 10.9, 2.4, -5.7, 13.8, -5.8, -8.7, -4.6, 
-25.9, 10.3, -12.6, 2.5, 0.9), w14 = c(-6.7, 14.4, 2.4, -0.2, 
-5.8, 13.8, 4, 0.9, 8.7, -13.8, -6.8, 13.5, 10.9, 8.2, 2.5, -16.4, 
-8.7, -5.6, 17.5, -15.7, 4.2, 10.5, -4.6, -25.9, -12.6, 4.2, 
4.9, 10.3, 4.2, -0.8, -11.4, -5.7), w15 = c(-5.7, 4, 10.9, 14.4, 
-6.8, -5.8, 8.2, -12.6, 4.9, 17.5, 10.5, -13.8, 0.9, 2.5, -8.7, 
10.3, 4.2, -0.8, 2.4, 8.7, 4.2, 4.2, -6.7, 13.8, 13.5, -0.2, 
-4.6, -25.9, -16.4, -15.7, -5.6, -11.4), w16 = c(-25.9, -5.8, 
2.5, 10.3, 8.2, -0.8, -0.2, -15.7, 0.9, -12.6, 8.7, -6.8, -8.7, 
13.8, 4.2, 4, 10.5, 2.4, -5.6, 13.5, -5.7, 4.2, -13.8, 4.2, -6.7, 
-4.6, -16.4, 17.5, -11.4, 14.4, 10.9, 4.9), w17 = c(-4.6, -15.7, 
8.2, 4.2, 13.5, 10.5, 4, -8.7, 4.2, 10.3, 14.4, -0.8, 10.9, 4.2, 
2.5, 17.5, -5.6, 8.7, -13.8, -6.8, 4.9, 2.4, -0.2, -11.4, -12.6, 
-6.7, -5.7, -16.4, -25.9, -5.8, 0.9, 13.8)), .Names = c("team", 
"w1", "w2", "w3", "w4", "w5", "w6", "w7", "w8", "w9", "w10", 
"w11", "w12", "w13", "w14", "w15", "w16", "w17"), row.names = c(NA, 
32L), class = "data.frame")

这是目前为止的代码:

require(dplyr)
require(ggvis)
require(tidyr) # For the gather function

df2 <- df %>% gather(key, value, w1:w17)
names(df2) <- c("team", "week", "defense")

df2 %>% 
  ggvis(~week, ~team, fill = ~defense) %>%
  layer_rects(width = band(), height = band()) %>%
  scale_nominal("x", padding = 0, points = FALSE) %>%
  scale_nominal("y", padding = 0, points = FALSE)

【问题讨论】:

    标签: r ggvis


    【解决方案1】:

    我通过创建一个新变量def.color 为每个单元格设置颜色,该变量将defense 的每个值映射到特定颜色。在ggplot2 中,您可以使用一行代码(例如scale_fill_manual())直接在对ggplot 的调用中设置颜色,而不是向数据框添加颜色变量。我希望ggvis 有办法做到这一点,但我还没有找到。所以,现在,我们开始吧:

    # Create a new variable df2$def.color for mapping df2$defense values to colors
    
    # Functions to create color ramps for the blue and orange color ranges
    Blue = colorRampPalette(c("darkblue","lightblue"))
    Orange = colorRampPalette(c("orange","darkorange3"))
    
    # Negative values of defense get a blue color scale with 10 colors
    df2$def.color[!is.na(df2$defense) & df2$defense<0] = 
      as.character(cut(df2$defense[!is.na(df2$defense) & df2$defense<0], 
                       seq(min(df2$defense - 0.1, na.rm=TRUE), 0, length.out=11), 
                       labels=Blue(10)))
    
    # Positive values of defense get an orange color scale with 10 colors
    df2$def.color[!is.na(df2$defense) & df2$defense>=0] = 
      as.character(cut(df2$defense[!is.na(df2$defense) & df2$defense>=0], 
                       seq(0, max(df2$defense, na.rm=TRUE)+0.1, length.out=11), 
                       labels=Orange(10)))
    
    # Set NA values in df2$def.color to light gray in df2$def.color
    df2$def.color[is.na(df2$defense)] = "#E5E5E5"  
    
    # Set NA values in df2$defense to blanks so that we won't get "NaN" in cells with 
    # missing data
    df2$defense[is.na(df2$defense)] = ""
    

    现在我们创建情节。要获取颜色,请使用 :=def.color 映射到 fill 以覆盖默认颜色。要添加defense 的值,请使用layer_text。我对每个单元格中的文本位置不满意,但这是我目前能想到的最好的。

    df2 %>% 
      ggvis(~week, ~team, fill:=~def.color) %>% 
      layer_rects(width = band(), height = band()) %>%
      scale_nominal("x", padding = 0, points = FALSE) %>%
      scale_nominal("y", padding = 0, points = FALSE) %>%
      layer_text(text:=~defense, stroke:="white", align:="left", baseline:="top") 
    

    【讨论】:

    • 需要手动将变量映射到颜色上似乎有点违反直觉。 ggvis 的比例函数还没有办法做到这一点吗?
    • 我有一段时间没有尝试使用ggvis,它一直在快速发展中。我同意让比例函数来处理这个问题会更可取。希望有或很快会有一种方法来做到这一点。我还想找到一种方法来更好地控制数字在单元格中的位置。
    • 我自己只关注 ggvis 几天。根据我的阅读,轻松做到这一点是未来的功能。现在,您可以在每一端使用任意颜色定义填充或描边比例,但您还不能拥有 {scales}' scale_fill_gradientn 的等价物 - 这对于蓝白红或颜色等情况至关重要与蓝红相映成趣。
    • 好奇@eipi10 是否能够为情节添加图例?
    【解决方案2】:

    我找到了一个使用scale_ordinal 函数来显示图例的解决方案。用了很多@eipi10写的代码,谢谢!

    # Functions to create color ramps for the blue and orange color ranges,
    # combined in a single palette with 10 colors of each ramp and gray for NAs
    Blue <- colorRampPalette(c("darkblue","lightblue"))
    Orange <- colorRampPalette(c("orange","darkorange3"))
    palette <- c(Blue(10), "#E5E5E5", Orange(10))
    
    # Negative values of defense get a blue color scale with 10 colors, indexes
    # from 1 to 10
    df2$def.label[!is.na(df2$defense) & df2$defense<0] <- 
        as.character(cut(df2$defense[!is.na(df2$defense) & df2$defense<0], 
                     seq(min(df2$defense - 0.1, na.rm = TRUE), 0, length.out = 11), 
                     labels = palette[1:10]))
    
    # Positive values of defense get an orange color scale with 10 colors,
    # indexes from 12 to 21
    df2$def.label[!is.na(df2$defense) & df2$defense>=0] <-
        as.character(cut(df2$defense[!is.na(df2$defense) & df2$defense>=0], 
                     seq(0, max(df2$defense, na.rm = TRUE) + 0.1, length.out = 11), 
                     labels = palette[12:21]))
    
    # Set NA values in df2$defense to 11 in def.label, the label for gray color
    df2$def.label[is.na(df2$defense)] <- palette[[11]] 
    
    # Define the values to be displayed on the legend
    pos.cut.values <- seq(0, max(df2$defense, na.rm = TRUE) + 0.1, length.out = 11)
    neg.cut.values <- seq(min(df2$defense - 0.1, na.rm = TRUE), 0, length.out = 11)
    legend.values <- c(paste(neg.cut.values[1:10], '..', neg.cut.values[2:11]),
                       'NA', paste(pos.cut.values[1:10], '..', pos.cut.values[2:11]))
    
    # Set NA values in df2$defense to blanks so that we won't get "NaN" in cells
    # with missing data
    df2$defense[is.na(df2$defense)] <- ""
    
    df2 %>% 
        ggvis(~week, ~team, fill:=~def.label) %>% 
        scale_ordinal('fill', range = palette) %>%
        add_legend(scales = 'fill', values = legend.values) %>%
        layer_rects(width = band(), height = band()) %>%
        scale_nominal("x", padding = 0, points = FALSE) %>%
        scale_nominal("y", padding = 0, points = FALSE) %>%
        layer_text(text := ~defense, stroke := "white", align := "left",
                   baseline := "top") 
    

    【讨论】:

      猜你喜欢
      • 2016-05-19
      • 2011-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      相关资源
      最近更新 更多