简体   繁体   English

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

[英]Display and fill multiple layer_text layers in ggvis

I am having trouble using the "fill" property for more than one text layer: 我在多个文本图层上使用“填充”属性时遇到麻烦:

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

This causes no text to display while this works: 这将导致在工作时不显示任何文本:

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

To avoid needing fill in layer_text to control the color of the text you could move it to be inside layer_bars . 为了避免需要fill layer_text来控制文本的颜色,可以将其移动到layer_bars内部。

I think you are also going to want to put the text you are plotting into a 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`). 我认为您还将要把要绘制的文本放入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` data.frame, otherwise it looks like the text gets plotted many times and looks funny (this would be like what happens with一样)。

One option: 一种选择:

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

If leaving fill in overall ggvis : 如果离开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")

在此处输入图片说明

try stroke instead of fill ? 尝试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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM