Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

R 条形图


条形图

条形图使用矩形条形来可视化数据。条形图可以水平或垂直显示。条形的高度或长度与其表示的值成正比。

使用 barplot() 函数绘制垂直条形图

例子

# x 轴值
x <- c("A", "B", "C", "D")

# y 轴值
y <- c(2, 4, 6, 8)

barplot(y, names.arg = x)

结果

自己尝试 »

例子解释

  • x 变量表示 x 轴上的值 (A,B,C,D)
  • y 变量表示 y 轴上的值 (2,4,6,8)
  • 然后我们使用 barplot() 函数创建这些值的条形图
  • names.arg 定义 x 轴上每个观测值的名称

条形颜色

使用 col 参数更改条形颜色

例子

x <- c("A", "B", "C", "D")
y <- c(2, 4, 6, 8)

barplot(y, names.arg = x, col = "red")

结果

自己尝试 »

密度/条形纹理

要更改条形纹理,请使用 density 参数

例子

x <- c("A", "B", "C", "D")
y <- c(2, 4, 6, 8)

barplot(y, names.arg = x, density = 10)

结果

自己尝试 »


条形宽度

使用 width 参数更改条形宽度

例子

x <- c("A", "B", "C", "D")
y <- c(2, 4, 6, 8)

barplot(y, names.arg = x, width = c(1,2,3,4))

结果

自己尝试 »

水平条形

如果希望条形水平显示而不是垂直显示,请使用 horiz=TRUE

例子

x <- c("A", "B", "C", "D")
y <- c(2, 4, 6, 8)

barplot(y, names.arg = x, horiz = TRUE)

结果

自己尝试 »

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.