【问题标题】:Creating Histogram in R not showing any bar在 R 中创建直方图不显示任何条形图
【发布时间】:2018-03-27 19:03:10
【问题描述】:

刚学R,需要用hist()创建直方图。以下是我的原始 R 脚本(假设数据):

x <- c(25130250, 26054121, 28074512, 36025050, 35013655)
y <- c(1:5)
hist(x, main = "Trafic Per Year", xlab="x-value", ylab = "y-Value")

返回:

我想要的是图表:

  1. 在 x 轴上显示 y(即 1、2、3、4、5)
  2. 显示 y 轴上的 x(即 25130250 等)减少到百万(即 25、26、28 等)

所以,我修改了脚本如下,现在它没有显示任何栏。

hist(x, xlim = c(1,5), ylim = c(25,37), breaks = 5,freq = T,
     main = "Trafic Per Year",xlab="x-value",ylab = "y-Value")

非常感谢任何帮助。

【问题讨论】:

  • barplot(round(x / 1000000), names.arg = y)
  • hist 采用未聚合的数据。看起来您的数据已经汇总,所以@bouncyball 的答案可能是最合适的
  • barplot 是聚合数据的一个选项。

标签: r plot histogram


【解决方案1】:

我希望你服务于我所做的就是将 y 分配给 x,并将 x 分配给 y

y <- c(25130250, 26054121, 28074512, 36025050, 35013655)
x <- c(1:5)

barplot(round(y/1000000,3), main = "Trafic Per Year", xlab="x-value", ylab = "y-Value", las = 2)
axis(1,at = c(0.7,1.9,3.1,4.3,5.5),labels=1:5)

【讨论】:

  • 感谢您的帮助,非常感谢。
【解决方案2】:

我猜你想要的是条形图而不是直方图:

 vals <- barplot( round(x/1000000), main = "Trafic Per Year", xlab="x-value", ylab = "y-Value",
                 horiz=TRUE)
 vals   # the function returns the midpoint location of the bars
  #-------
     [,1]
[1,]  0.7
[2,]  1.9
[3,]  3.1
[4,]  4.3
[5,]  5.5
  #-----
 axis(2, at=vals,labels=1:5)

【讨论】:

    猜你喜欢
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 2014-12-04
    • 2015-12-07
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多