代码之家  ›  专栏  ›  技术社区  ›  SteveS

根据R中的count min max range向直方图的count(y轴)添加中断?

  •  0
  • SteveS  · 技术社区  · 6 年前

    ggplot 直方图。

    在y轴上我有个数。

    我在和你玩 scale_y_discrete min(count) , max(count) 加上=1。

    df <- structure(list(user_id = c(1L, 1L, 3L, 3L, 4L, 4L, 4L, 6L, 8L, 
    8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L), obs_id = c(1L, 
    30L, 133L, 134L, 144L, 160L, 162L, 226L, 272L, 273L, 274L, 275L, 
    276L, 299L, 307L, 322L, 323L, 324L, 325L, 326L, 327L, 328L), 
        n = c(6L, 6L, 10L, 6L, 11L, 11L, 12L, 6L, 3L, 2L, 5L, 2L, 
        3L, 5L, 12L, 11L, 25L, 7L, 5L, 2L, 5L, 17L)), class = c("grouped_df", 
    "tbl_df", "tbl", "data.frame"), row.names = c(NA, -22L), vars = "user_id", drop = TRUE, .Names = c("user_id", 
    "obs_id", "n"), indices = list(0:1, 2:3, 4:6, 7L, 8:12, 13:21), group_sizes = c(2L, 
    2L, 3L, 1L, 5L, 9L), biggest_group_size = 9L, labels = structure(list(
        user_id = c(1L, 3L, 4L, 6L, 8L, 9L)), class = "data.frame", row.names = c(NA, 
    -6L), vars = "user_id", drop = TRUE, .Names = "user_id"))
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   aosmith    6 年前

    你可以做一个函数 breaks

    scale_continuous , 打破

    一种函数,以极限为输入,以中断为输出

    这里是一个例子,我从0到最大y轴限制为1。(我使用0而不是最小计数,因为直方图从0开始。)

    这个 x ggplot() 或由用户设置。

    byone = function(x) {
         seq(0, max(x), by = 1)
    }
    

    您可以将此函数传递给 打破 在里面 scale_y_continuous() . 这个 limits 直接从绘图中提取并传递给函数的第一个参数。

    ggplot(df, aes(user_id)) +
         geom_histogram() +
         scale_y_continuous(breaks = byone)
    

    enter image description here