在ggvis中显示和填充多个layer_text图层

时间:2016-08-30 20:48:25

标签: r ggvis

我无法使用" fill"多个文本图层的属性:

Month = seq(1,12)
Median_Peak_Duration = c(6,5,4,3,1,2,2,3,4,4,5,5.5)
PeakyMonth = ifelse(Median_Peak_Duration > 3, 'Non-Peaky', 'Peaky')
testDat = data.frame(Month, Median_Peak_Duration,PeakyMonth)

testDat %>%
ggvis(x = ~Month, y = ~Median_Peak_Duration, fill = ~PeakyMonth) %>%
layer_bars(width = .8) %>%
# Annotation
layer_lines(x = ~Month, y = 3, strokeDash := 6, strokeWidth := 3) %>%
layer_text(x = 6, y = 4.5, text:= "Some more Text", 
  fontSize := 12, align := "center", baseline := "top", fill := "black") %>%
layer_text(x = 6, y = 3, text:= "Some Text", 
  fontSize := 12, align := "center", baseline := "top", fill := "black")

这会导致无法显示文本:

testDat %>%
ggvis(x = ~Month, y = ~Median_Peak_Duration, fill = ~PeakyMonth) %>%
layer_bars(width = .8) %>%
# Annotation
layer_lines(x = ~Month, y = 3, strokeDash := 6, strokeWidth := 3) %>%
layer_text(x = 6, y = 4.5, text:= "Some more Text", 
  fontSize := 12, align := "center", baseline := "top", fill := "black")

2 个答案:

答案 0 :(得分:2)

为避免在fill中需要layer_text来控制文字的颜色,您可以将其移至layer_bars内。

我想你也想把你正在密谋的文字放到data.frame, otherwise it looks like the text gets plotted many times and looks funny (this would be like what happens with ggplot2:geom_text`中。

一个选项:

testDat %>%
    ggvis(x = ~Month, y = ~Median_Peak_Duration) %>%
    layer_bars(width = .8, fill = ~PeakyMonth) %>%
    # Annotation
    layer_lines(x = ~Month, y = 3, strokeDash := 6, strokeWidth := 3) %>%
    layer_text(data = data.frame(x = 6, y = 4.5, text = "Some more text"), 
             x = ~x, y = ~y, text := ~text,
             fontSize := 12, align := "center", baseline := "top") %>%
    layer_text(data = data.frame(x = 6, y = 3, text = "Some Text"),
             x = ~x, y = ~y, text := ~text,
             fontSize := 12, align := "center", baseline := "top")

如果将fill留在整体ggvis中:

testDat %>%
    ggvis(x = ~Month, y = ~Median_Peak_Duration, fill = ~PeakyMonth) %>%
    layer_bars(width = .8) %>%
    # Annotation
    layer_lines(x = ~Month, y = 3, strokeDash := 6, strokeWidth := 3) %>%
    layer_text(data = data.frame(x = 6, y = 4.5, text = "Some more text"), 
             x = ~x, y = ~y, text := ~text,
             fontSize := 12, align := "center", baseline := "top", fill := "black") %>%
    layer_text(data = data.frame(x = 6, y = 3, text = "Some Text"),
             x = ~x, y = ~y, text := ~text,
             fontSize := 12, align := "center", baseline := "top", fill := "black")

enter image description here

答案 1 :(得分:0)

尝试stroke而不是fill

testDat %>%
    ggvis(x = ~Month, y = ~Median_Peak_Duration, fill = ~PeakyMonth) %>%
    layer_bars(width = .8) %>%
    # Annotation
    layer_lines(x = ~Month, y = 3, strokeDash := 6, strokeWidth := 3) %>%
    layer_text(x = 6, y = 4.5, text:= "Some more Text", 
               fontSize := 12, align := "center", baseline := "top", stroke := "black") %>%
    layer_text(x = 6, y = 3, text:= "Some Text", 
               fontSize := 12, align := "center", baseline := "top", stroke := "black")