Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.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中生成直方图导致错误_R_Graph_Histogram - Fatal编程技术网

在R中生成直方图导致错误

在R中生成直方图导致错误,r,graph,histogram,R,Graph,Histogram,我将数据保存在一个名为“timemapreport.txt”的文本文件中,我正试图将该文件导入RStudio并为其生成直方图: 数据以以下格式保存在文本文件中: 12 16 1025 965 9 1 9 9 12 我试图使用此代码,但它生成了一个错误: 正在RStudio中读取数据。我觉得很好。然而,当我试图生成直方图时,由于一个错误,它失败了。我在网上做了一些搜索,它说R正在读取数据,但将其视为字符串,而不是数字,所以我尝试将其转换为数字或整数,但仍然不起作用 我取出了函数hist()的一些

我将数据保存在一个名为“timemapreport.txt”的文本文件中,我正试图将该文件导入RStudio并为其生成直方图:

数据以以下格式保存在文本文件中:

12
16
1025
965
9
1
9
9
12
我试图使用此代码,但它生成了一个错误: 正在RStudio中读取数据。我觉得很好。然而,当我试图生成直方图时,由于一个错误,它失败了。我在网上做了一些搜索,它说R正在读取数据,但将其视为字符串,而不是数字,所以我尝试将其转换为数字或整数,但仍然不起作用

我取出了函数hist()的一些参数,以查看是哪个参数导致了错误,但这也不起作用。我甚至把争论减少到只有一个争论,但还是没有运气

感谢您的帮助

谢谢

> timemaps_data <- read.table("C:/R/timemapreport.txt", header=F, sep="\t")
> View(timemaps_data)
> View(timemaps_data)
> max_num <- max(timemaps_data)
> hist(timemaps_data, col=heat.colors(max_num), breaks=max_num, xlim=c(0,max_num), right=F, main="Mementos Histogram", las=1)
Error in hist.default(timemaps_data, col = heat.colors(max_num), breaks = max_num,  : 
  'x' must be numeric
> hist(timemaps_data, col=heat.colors(max_num), breaks=max_num, xlim=c(0:max_num), right=F, main="Mementos Histogram", las=1)
Error in hist.default(timemaps_data, col = heat.colors(max_num), breaks = max_num,  : 
  'x' must be numeric
> hist(timemaps_data, breaks=max_num, xlim=c(0,max_num), right=F, main="Mementos Histogram", las=1)
Error in hist.default(timemaps_data, breaks = max_num, xlim = c(0, max_num),  : 
  'x' must be numeric
> hist(timemaps_data, breaks=max_num, right=F, main="Mementos Histogram", las=1)
Error in hist.default(timemaps_data, breaks = max_num, right = F, main = "Mementos Histogram",  : 
  'x' must be numeric
> hist(timemaps_data, right=F, main="Mementos Histogram", las=1)
Error in hist.default(timemaps_data, right = F, main = "Mementos Histogram",  : 
  'x' must be numeric
> hist(timemaps_data, main="Mementos Histogram", las=1)
Error in hist.default(timemaps_data, main = "Mementos Histogram", las = 1) : 
  'x' must be numeric
> hist(timemaps_data, main="Mementos Histogram")
Error in hist.default(timemaps_data, main = "Mementos Histogram") : 
  'x' must be numeric
> hist(timemaps_data)
Error in hist.default(timemaps_data) : 'x' must be numeric
> hist(timemaps_data, col="lightblue", ylim=c(0,10))
Error in hist.default(timemaps_data, col = "lightblue", ylim = c(0, 10)) : 
  'x' must be numeric
> timemaps_data <- read.table("C:/R/timemapreport.txt", header=F, sep="\n")
> max_num <- max(timemaps_data)
> hist(timemaps_data, col=heat.colors(max_num), breaks=max_num, xlim=c(0,max_num), right=F, main="Mementos Histogram", las=1)
Error in hist.default(timemaps_data, col = heat.colors(max_num), breaks = max_num,  : 
  'x' must be numeric
> timemaps_data <- as.numeric(timemaps_data) 
Error: (list) object cannot be coerced to type 'double'
> timemaps_data <- as.int(timemaps_data) 
Error: could not find function "as.int"
> timemaps_data <- as.integer(timemaps_data) 
Error: (list) object cannot be coerced to type 'integer'
> timemaps_data <- as.integer(timemaps_data) 
Error: (list) object cannot be coerced to type 'integer'
时间地图数据视图(时间地图数据) >查看(时间地图\u数据) >max_num hist(timemaps_data,col=heat.colors(max_num),breaks=max_num,xlim=c(0,max_num),right=F,main=“Mementos Histogram”,las=1) 历史默认值错误(时间映射数据,列=热。颜色(最大值),中断=最大值,: “x”必须是数字 >hist(timemaps\u data,col=heat.colors(max\u num),breaks=max\u num,xlim=c(0:max\u num),right=F,main=“Mementos Histogram”,las=1) 历史默认值错误(时间映射数据,列=热。颜色(最大值),中断=最大值,: “x”必须是数字 >hist(timemaps_data,breaks=max_num,xlim=c(0,max_num),right=F,main=“Mementos Histogram”,las=1) 历史默认值错误(时间映射数据,中断=max\u num,xlim=c(0,max\u num)): “x”必须是数字 >hist(时间地图数据,中断=最大值,右=F,main=“记忆直方图”,las=1) 历史默认值中的错误(时间映射\u数据,中断=max\u num,右=F,main=“Mementos Histogram”,: “x”必须是数字 >hist(时间地图\数据,右=F,main=“记忆直方图”,las=1) 历史默认值错误(timemaps\u数据,right=F,main=“Mementos Histogram”,: “x”必须是数字 >hist(时间地图\数据,main=“记忆直方图”,las=1) 历史默认值错误(timemaps\u数据,main=“Mementos Histogram”,las=1): “x”必须是数字 >hist(时间地图\数据,main=“记忆直方图”) 历史默认值(timemaps\u数据,main=“Mementos Histogram”)中出错: “x”必须是数字 >历史(时间地图\数据) 历史默认值(timemaps\u数据)中出错:“x”必须是数字 >hist(timemaps_data,col=“lightblue”,ylim=c(0,10)) 历史默认值(timemaps_data,col=“lightblue”,ylim=c(0,10))中出错: “x”必须是数字 >timemaps\u data max\u num hist(timemaps\u data,col=heat.colors(max\u num),breaks=max\u num,xlim=c(0,max\u num),right=F,main=“Mementos Histogram”,las=1) 历史默认值错误(时间映射数据,列=热。颜色(最大值),中断=最大值,: “x”必须是数字
>timemaps\u data timemaps\u data timemaps\u data timemaps\u data我认为您的问题在于,当它只知道如何处理
data.frame
的单个列时,您提供了
hist()
作为其参数。请尝试以下操作:

timemaps_data <- read.table("C:/R/timemapreport.txt", header=F, sep="\t")
hist(timemaps_data[,1])

timemaps\u data
numeric
是一个类。当您看到“必须是数字”之类的错误时,表示您的数据属于错误的类。您应该查看
class(timemaps\u data)
str(timemaps\u data)
查看它到底是什么-这可以帮助您解决问题。您的问题很可能在于数据本身。我的赌注是文件的最后一行。但可能是某个不是数字的字符。如果该假设是真的,请在导入之前清理数据,或者过滤任何不能强制为num的值ber。两种解决方案都有效。谢谢。但是,直方图不是最好看的,因为数据变化太大。我想使箱子大小为1,这样x轴上的数据就不会组合在一起,并使每个值看起来像一个条形图。如果x轴上的某个值没有频率,则将其移除,以便为具有频率C的值腾出空间y、 我尝试了这段代码,但不是我想要的。我很高兴能够生成一个直方图。谢谢..hist(timemaps\u data$V1,col=heat.colors(max\u num),breaks=max\u num,xlim=c(0,max\u num),right=F,main=“Mementos histogram”,las=1)
timemaps_data <- read.table("C:/R/timemapreport.txt", header=F, sep="\t")
names(timemaps_data)
hist(timemaps_data$V1)