如何在基础 R 中为具有较长名称的多个类别创建箱线图?
在基础 R 中,我们使用 boxplot 函数来创建箱线图,但是如果我们有类别向量和对应的数值向量,则可以轻松创建箱线图。为此,我们应该在数据框中保存那些向量,并使用 $ 运算符和 las = 2 参数来创建箱线图,如以下示例所示。
示例
考虑以下向量
Countries<−sample(c("China","India","Canada","USA","Russia","Indonesia","Nepal"),5000,replace=TRUE) > Rate<−rnorm(5000,10,1)
使用上述向量创建数据框 −
df<−data.frame(Countries,Rate) > head(df,20)
输出
Countries Rate 1 Russia 9.885246 2 Nepal 9.895285 3 USA 11.524113 4 Nepal 10.133226 5 Indonesia 9.711389 6 Russia 10.017597 7 Nepal 9.204832 8 Russia 8.436829 9 Russia 10.579013 10 Canada 7.984238 11 Canada 9.407908 12 Indonesia 9.047598 13 China 9.193126 14 India 8.524555 15 China 10.398155 16 India 6.926581 17 China 10.104521 18 Canada 10.178826 19 India 12.006973 20 Canada 10.371956
为国家创建箱线图 −
boxplot(df$Rate~df$Countries)
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
输出
通过更改国家名称的打印方向为国家创建箱线图 −
boxplot(df$Rate~df$Countries,las=2,xlab="")
输出
广告