gpt4 book ai didi

r - 在 R 中绘制两个条形图

转载 作者:行者123 更新时间:2023-12-04 12:13:54 24 4
gpt4 key购买 nike

 data <- as.matrix(data.frame("A" = c(18,7),
"B+" = c(3,2),
"B" = c(3,3),
"C+" = c(6,0),
"C" = c(7,0),
"D" = c(0,4),
"E" = c(5,23)))


barplot (data,
col = c("red","blue"),
beside = TRUE,
xlab = "Grade",
ylab = "Frequency")


legend("topleft",
c("IFC6503-A","IFC6510"),
fill = c("red","blue"),
inset = c(.01,0)
)
我将两个条形图绘制在一起,条形图包含等级,来自两个不同的数据,等级由 A B+ B C+ CDE ,但结果 B+ 和 C+ 没有出现,只出现 B. 和 C. ,是我的代码错误或者你们能更正我的代码吗?

最佳答案

不要使用特殊字符 +在伯恩哈德已经指出的列名中:
这是一种您可以手动重新标记绘图的 x 轴的方法:

  • 在您的矩阵中提供足够的列名:请参阅 whuber 的评论:https://stats.stackexchange.com/questions/163280/naming-convention-for-column-names
  • 使用 xaxt = "n"在条形图中删除 x 个标签
  • 使用 axis手动插入 x 个标签:
  • data <- as.matrix(data.frame("A" = c(18,7),
    "Bplus" = c(3,2),
    "B" = c(3,3),
    "Cplus" = c(6,0),
    "C" = c(7,0),
    "D" = c(0,4),
    "E" = c(5,23)))


    barplot (data,
    col = c("red","blue"),
    beside = TRUE,
    xlab = "Grade",
    ylab = "Frequency",
    xaxt = "n")

    axis(1, at = seq(2, 20, 3), labels = c("A", "B+", "B", "C+", "C", "D", "E"))


    legend("topleft",
    c("IFC6503-A","IFC6510"),
    fill = c("red","blue"),
    inset = c(.01,0)
    )
    enter image description here

    关于r - 在 R 中绘制两个条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69232355/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com