【问题标题】:Create a secondary y axis for lines graph with ggvis R使用 ggvis R 为折线图创建辅助 y 轴
【发布时间】:2014-12-20 13:08:15
【问题描述】:

这就是我目前在 R 中的 ggvis 包所拥有的。

    mtcars %>% ggvis(x = ~disp) %>% 
    layer_lines(y = ~wt, stroke := "red") %>%
    layer_lines(y = ~mpg) %>%
    add_axis("y", orient = "left", title = "Weight (lb/1000)") %>%
    add_axis("y", orient = "right", title= "Miles/(US) gallon") %>%
    add_axis("x", title = "Displacement (cu.in.)")

我无法获取左侧 Y 轴来表示 wt 比例数据。

这个输出:

【问题讨论】:

    标签: r plot ggvis


    【解决方案1】:

    我假设你想要左 y 轴(即 wt)除以 1000:

    library(dplyr) #you need this library
    mtcars  %>% mutate(wt_scaled=wt/1000)  %>% ggvis(x = ~disp) %>% #use mutate from dplyr to add scaled wt
      layer_lines(y = ~wt_scaled, stroke := "red") %>% #use new column
      add_axis("y", orient = "left", title = "Weight (lb/1000)" ,title_offset = 50) %>% #fix left axis label
      scale_numeric("y", domain = c(0, 0.006), nice = FALSE) %>% #align the ticks as good as possible
      add_axis("y", 'ympg' , orient = "right", title= "Miles/(US) gallon" , grid=F ) %>% #remove right y axis grid and name axis
      layer_lines( prop('y' , ~mpg,  scale='ympg') ) %>% #use scale to show layer_lines which axis it should use
      add_axis("x", title = "Displacement (cu.in.)"  )
    

    我认为这就是你想要的:

    编辑

    如果您只想在左侧 y 轴上绘制 wt(不是很清楚),然后执行 mutate(wt_scaled=wt/1)(或删除 mutate)并将域更改为 domain = c(1.5, 5.5)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-25
      • 1970-01-01
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      • 2020-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多