0

我有兴趣将一些图表从 ggplot 转换为 ggvis,但是关于 ggvis 的某些功能的信息相对较少。

我有一个比特率图表,我需要用格式良好的标签以对数比例绘制。

这是在ggplot中执行此操作的代码:

require(data.table)  # data.table_1.9.2
require(magrittr)    # magrittr_1.0.1
require(ggplot2)     # ggplot2_1.0.0
require(ggvis)       # ggvis_0.3.0.99

# Management requires nice labels on their graphs
format_si = function(unit="", ...) {
  # Returns a function that formats its input in SI-style (poswers of ten)
  # The function inserts the supplied unit
  function(x) {
    limits <- c(1e-24, 1e-21, 1e-18, 1e-15, 1e-12, 1e-9,  1e-6,  1e-3,  1e0,  1e3,  1e6,  1e9,  1e12,  1e15,  1e18, 1e21,  1e24)
    prefix <- c(" y",  " z",  " a",  " f",  " p",  " n",  " µ",  " m",  " ",  " k", " M", " G", " T",  " P",  " E", " Z",  " Y")
    i <- findInterval(abs(x), limits)
    i <- ifelse(i==0, which(limits == 1e0), i)
    paste(format(round(x/limits[i], 1), trim=TRUE, scientific=FALSE, ...), prefix[i], unit, sep="")
  }   
}   

# Create some sample data.
data = data.table(bitrate=rgamma(200, shape = 1.5, scale = 5e6))

# Make a wonderful ggplot2 bitmap graph.
data %>%
    ggplot(aes(bitrate)) +
    geom_bar(stat="bin", binwidth=.5, aes(y=..density..), fill="#ccccff", color="black") +
    scale_x_log10(breaks=10^(4:9), labels=format_si('bit/s')(10^(4:9)), limits=c(10^4,10^9))

ggplot比特率

尝试创建基本的 ggvis 情节作品:

# Create a non-log ggvis super awesome web graph.
data.table(bitrate=rgamma(200, shape = 1.5, scale = 5e6)) %>%
    ggvis(x = ~bitrate) %>%
    compute_bin(~bitrate) %>%
    layer_rects(x = ~xmin_, x2 = ~xmax_, y=~count_, y2=0) 

但是如果我们简单地添加一个对数刻度然后繁荣,现在图表是空白的:

# Try to add a log scale.
data.table(bitrate=rgamma(200, shape = 1.5, scale = 5e6)) %>%
    ggvis(x = ~bitrate) %>%
    compute_bin(~bitrate) %>%
    layer_rects(x = ~xmin_, x2 = ~xmax_, y=~count_, y2=0) %>%
    scale_numeric("x", trans="log") 

是否可以在 ggvis 中重新创建 ggplot 图?向 ggvis 轴添加标记中断或标签格式化函数怎么样?

4

1 回答 1

3

尝试添加expand = 0

scale_numeric("x", trans = "log", expand = 0)
于 2014-11-24T21:51:55.890 回答