【问题标题】:adjust x scale values调整 x 刻度值
【发布时间】:2019-01-08 19:42:36
【问题描述】:

所以我正在制作月份的直方图,但 x 轴从 0.5 变为 12.5。有谁知道我如何将其修复为 1 - 12(因为它们代表月份?

x<-c(1,2,3,4,5,6,6,7,8,9,10,11,12)


qplot(x,geom='histogram',fill=I("red"), col=I("darkred"),xlab="Maand",ylab="Hoeveelheid",bins=12)

【问题讨论】:

    标签: r ggplot2 plot histogram axis-labels


    【解决方案1】:

    您可以传递 x as.factor

    library(ggplot2)
    x <- c(1,2,3,4,5,6,6,7,8,9,10,11,12)
    x <- as.data.frame(x)
    
    ggplot(x, aes(as.factor(x))) +
        geom_bar(fill = "red", color = "darkred") +
        xlab("Maand") +
        ylab("Hoeveelheid")
    

    【讨论】:

      【解决方案2】:

      你可以试试

      library(tidyverse)
      tibble(x = c(1,2,3,4,5,6,6,7,8,9,10,11,12)) %>% 
        ggplot(aes(x)) + 
         geom_histogram(binwidth = 1, color="white") + 
         scale_x_continuous(breaks = 1:12)
      

      在base R中你可以试试

      hist(c(1,2,3,4,5,6,6,7,8,9,10,11,12))
      

      【讨论】:

        猜你喜欢
        • 2019-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-03
        • 2021-12-18
        • 1970-01-01
        • 2021-09-21
        • 2020-05-29
        相关资源
        最近更新 更多