【问题标题】:Lines stroke (color) as legend and moving legend labels next to lines线条笔触(颜色)作为图例,并在线条旁边移动图例标签
【发布时间】:2016-03-08 16:28:49
【问题描述】:

我正在尝试使用 ggvis 制作类似于此的情节

我遇到了两个问题。

首先,我尝试将线条笔触颜色指定为图例形状,但 ggvis 始终保留圆圈。此外,它也不识别虚线。

library(ggvis)
  data <-data.frame(region=rep(c("A","B","C"),5),c=rep(seq(1980,2000,5),3), val=rnorm(15))

    data %>%
    group_by(region) %>%
    ggvis(~c, ~val) %>%
    layer_smooths(stroke=~region, strokeDash = ~region,strokeWidth := 3, strokeOpacity := 0.65) %>%
    add_axis("y", title="y") %>%
    add_axis("x", title="y", format=####) %>%
    add_legend(c("stroke","strokeDash")) ## Adding this does not update the legend to recognize the line color or dashes.

有人问here,但没有人回答。

最后,我想将每个区域的图例名称放在线条旁边,就像在第一张图中一样。为此,我什至不知道如何开始。

感谢任何帮助。


更新:

我问如何将 x 轴标签设为数字,答案是将 format = "####" 添加到 add_axis。

【问题讨论】:

  • 这可能应该是几个单独的问题,但这里有一种方法可以在 x 轴上获得没有逗号的年份:add_axis("x", title="y", format = "####")
  • 谢谢!它无缝地工作。至于单独的问题,请注明。

标签: r ggplot2 ggvis


【解决方案1】:

我没有找到我特别需要的东西,但我找到了部分解决方案。查看代码中的 cmets。

data <-data.frame(region=rep(c("A","B","C"),5),c=rep(seq(1980,2000,5),3), val=rnorm(15))
data$region2 <- data$region ## create an additional region variable
data$region2 <- as.character(data$region2)
data$region2[data$c != 2000] <- "" ## Fill every year which is not the last year to be an empty character vector

data %>%
    group_by(region) %>%
    ggvis(~c, ~val) %>%
    layer_smooths(stroke=~region, strokeDash = ~region,strokeWidth := 3, strokeOpacity := 0.65) %>%
    layer_text(text := ~region2) %>% ## add this new region variable, which will only write over the last year.
    add_axis("y", title="y") %>%
    add_axis("x", title="y", format="####") %>%
    hide_legend(c("stroke","strokeDash"))

【讨论】:

    猜你喜欢
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 2012-06-13
    • 1970-01-01
    • 2022-11-07
    • 2012-07-05
    • 2023-03-20
    相关资源
    最近更新 更多