在ggplot中更改标签x轴boxplot

时间:2018-12-03 09:30:15

标签: r ggplot2

我是ggplot的新手,我有一个问题要在使用箱线图时重新标记x轴。

  mat <- as.data.frame(cbind(sample(1:120,5000, replace = TRUE), rnorm(500)))
  colnames(mat) <- c("category","value")
  mat$category <- as.factor(mat$category)

  library(ggplot2)

  p <- ggplot(mat, aes(x=category, y=value)) +
    geom_boxplot() + 
    theme_classic() +
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
    xlab("Category") + 
    ylab("Value in units")
  p 

问题是x轴,因为数字很难读取。 我的想法是使用一种序列来减少编号,或者在箱形图中,在x轴上写入的类别更少。

我的问题是在哪里包括以下序列

new_x_labeling = c(1,seq(10,120,5))

我找到了有关更改

中名称的答案

How to change x-axis tick label names, order and boxplot colour using R ggplot?

但是我不知道如何减少名字的数量。

https://rstudio-pubs-static.s3.amazonaws.com/3364_d1a578f521174152b46b19d0c83cbe7e.html

1 个答案:

答案 0 :(得分:2)

类似的东西:

  ggplot(mat, aes(x=category, y=value)) +
    geom_boxplot() + 
    scale_x_discrete(breaks = c(1, seq(10, 120, 5))) +
    theme_classic() +
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
    labs(x = "Category", y = "Value in units")

enter image description here