gpt4 book ai didi

r - 使用 ggvis R 为线图创建辅助 y 轴

转载 作者:行者123 更新时间:2023-12-03 23:47:44 27 4
gpt4 key购买 nike

这就是我到目前为止在 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 比例数据。

这输出:

enter image description here

最佳答案

我假设您想要将左 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.)" )

我认为这就是你想要的:

enter image description here

编辑 :

如果你只是想在左 y 轴上绘制 wt(不是很清楚),那么做 mutate(wt_scaled=wt/1) (或删除 mutate)并将域更改为 domain = c(1.5, 5.5)

关于r - 使用 ggvis R 为线图创建辅助 y 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27580551/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com