在ggvis中显示并填充多个layer_text层

Display and fill multiple layer_text layers in 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")

试试 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")

为了避免在 layer_text 中需要 fill 来控制文本的颜色,您可以将其移动到 layer_bars.

我认为您还想将正在绘制的文本放入 data.frame, otherwise it looks like the text gets plotted many times and looks funny (this would be like what happens withggplot2: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")