调整x比例值

时间:2018-08-01 09:31:17

标签: r ggplot2 plot histogram axis-labels

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

def has_space(item):
    return " " in item

def ask_name():
    return input("Please enter your name: ")

while 1:
    s = ask_name()
    if has_space(s):
        print('Cannot contain spaces.')
    else:
        break

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以传递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")

答案 1 :(得分:0)

您可以尝试

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)

enter image description here

在R底下您可以尝试

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