【发布时间】:2015-11-19 10:16:52
【问题描述】:
我需要在 shiny 中使用 ggvis 的 labels= 轴功能。
在 R 中,labels 轴参数允许更改 x 标签(添加 Kb 格式),以保持项目之间的顺序和相对距离:
mtcars <- mtcars[1:10, ]
my_data <- mtcars[order(mtcars$disp),]
xpos <- sort(mtcars$disp)
plot(my_data$disp, my_data$mpg, type = "l", xaxt="n")
axis(1, at=xpos, labels=sprintf("%.2fKb", xpos/10))
这样我们就得到了我们需要的东西:
现在我们尝试使用 ggvis 在闪亮上获得完全相同的效果:
library(shiny)
library(ggvis)
mtcars
ui <- pageWithSidebar( div(),
sidebarPanel(uiOutput("plot_ui"),width=2),
mainPanel(ggvisOutput("plot"))
)
server <- function(input, output, session) {
mtc <- reactive({
my_data <- mtcars[1:10, ]
# Do some sorting/ordering if you want (here sorted by disp)
my_data <- my_data[order(my_data$disp), ]
my_labels <- c(as.character(paste0((my_data$disp)/1000, "Kb")))
y <- my_data$mpg
x <- factor(c(my_data$disp), labels = c(unique(my_labels)))
data.frame(x = x, y = y)
})
mtc %>%
ggvis(~x,~y ) %>%
layer_lines() %>%
add_axis("x", properties=axis_props(labels=list(fontSize = 10))) %>%
bind_shiny("plot", "plot_ui")
}
shinyApp(ui = ui, server = server)
当我们用红色突出显示时,距离不正确。那么我们如何才能在具有 axis(label=... 的普通 R 上拥有相同的情节?
【问题讨论】:
-
为什么在观察者中使用 ggvis 而不是 plot?这个问题与 ggvis 中的轴有关吗?
-
是的,这可能是一个 ggvis 轴问题,我更新了标签