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

学生的入学人数不应被用作geom_barplot的x轴上的连续数。

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

    我有以下数据:

    matriculation_number int[]  grade num[]
    11111                       1.2
    22222                       2.0
    77777                       2.8
    .....                       ...
    

    并使用以下命令:

    boxPlot <- 
       ggplot(data = data, aes(x = matriculation_number, y = grade)) +
       geom_boxplot(aes(group=matriculation_number))) +
       xlab(matriculation_number)+
       ylab('Grades')
    

    问题是,在X轴上,应该显示取数,只有一个取数_号可见。同样,x轴将矩阵数视为数字而不是字符串,并沿整个轴展开。

    enter image description here

    如何在每个柱状图上显示取数_,以及如何在x轴上均匀分布取数_?

    1 回复  |  直到 6 年前
        1
  •  2
  •   G5W    6 年前

    在绘制之前, matriculation_number 成了一个因素。您可以在原始数据中使用

    data$matriculation_number = factor(data$matriculation_number)
    

    或者按照@mrflick的建议,您可以在plot语句中使用

    boxPlot <- 
        ggplot(data = data, aes(x = factor(matriculation_number), y = grade)) +
        geom_boxplot(aes(group=matriculation_number))) +
        xlab(matriculation_number)+
        ylab('Grades')