Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何在条形图的X轴上显示单位(非标签)?_R_Bar Chart_Units Of Measurement - Fatal编程技术网

R 如何在条形图的X轴上显示单位(非标签)?

R 如何在条形图的X轴上显示单位(非标签)?,r,bar-chart,units-of-measurement,R,Bar Chart,Units Of Measurement,我正在尝试创建一个数据可视化,以显示按班级数量划分的学校数量。我创建了一个TIBLE来实现这一点(我在下面粘贴了),其中有两列,“c(Grade 5$schlcode)”,显示唯一学校代码的列表,“n”是每个学校代码中的班级数 n_classes <- count(grade5, c(grade5$schlcode)) > > n_classes # A tibble: 1,002 x 2 `c(grade5$schlcode)` n

我正在尝试创建一个数据可视化,以显示按班级数量划分的学校数量。我创建了一个TIBLE来实现这一点(我在下面粘贴了),其中有两列,“c(Grade 5$schlcode)”,显示唯一学校代码的列表,“n”是每个学校代码中的班级数

n_classes <- count(grade5, c(grade5$schlcode))
> 
> n_classes
# A tibble: 1,002 x 2
   `c(grade5$schlcode)`     n
                  <dbl> <int>
 1                11005     2
 2                11006     2
 3                11009     1
 4                11010     2
 5                11013     3
 6                11015     3
 7                11017     2
 8                11020     3
 9                11021     2
10                11027     3
# … with 992 more rows
但当我绘制图时,它正确地显示了有1个班级、2个班级和3个班级的学校的频率,以此类推,但我没有显示X轴的单位(因此每个条都应该标记为1、2、3、4(班级)。但是没有单位,只有xlab

> barplot(x$n, x$`c(n_classes$n)`,
+         xlab = "Number of Classes in a School",
+         ylab = "Frequency of Schools",
+         main = "Number of Classes Schools Have",
+         col = "grey",
+         border = "orange")

如何修复此问题?

计数编号的列名非常奇怪,这会导致很多问题:

x = data.frame("`c(n_classes$n)`" = 1:6,
n=c(296,456,199,42,8,1),check.names=FALSE)
如果我们这样做

x$`c(n_classes$n)`
NULL
这是因为它将`解释为字符串。您可以尝试:

barplot(x$n,x$"`c(n_classes$n)`",
         xlab = "Number of Classes in a School",
         ylab = "Frequency of Schools",
         main = "Number of Classes Schools Have",
         col = "grey",
         border = "orange")

只需在向量上调用两次表即可避免所有这些情况:

set.seed(100)
grade5 = data.frame(schlcode=sample(1:20,100,replace=TRUE))
barplot(table(table(grade5$schlcode)))

通常的惯例是将单位放在
xlab
ylab
中,例如“一所学校的班级数量(计数)”。如果要将“(计数)”放在其他地方,可能需要使用
mtext()
将其明确地放在那里。
set.seed(100)
grade5 = data.frame(schlcode=sample(1:20,100,replace=TRUE))
barplot(table(table(grade5$schlcode)))