根据精确范围和R中的采样可视化ggplot [英] Visualize an ggplot according to a precise range and sampling in R
问题描述
我有如下表:
天|日期| Temp1---- | ----------- | ---------------1 | 9/15/2014 | 852 | 9/16/2014 | 853 | 9/17/2014 | 884 | 9/18/2014 | 83.. | ..... | ..871 | 6/8/2017 | 98
我有很多数据作为日期列9/15/2014到6/8/2017.而且我选择将这6个月的价值用于ggplot清理.
这是我的代码:
库(ggplot2)#library(Rserve)#Rserve(args ="--vanilla")测试<-read_csv("C:/Users/..../Desktop/Myfile.csv")valueDatemax<-max(测试$日期)valueDatemin<-分钟(Test $ Date)#模拟一些数据df<-data.frame(Test,Test $ Date<-seq.Date(from = as.Date(valueDatemin),到= as.Date(valueDatemax),by ="6个月"))要求(ggplot2)p< -ggplot(df,aes(x = Test $ Date,y = Temp1))打印(p)
但是我有一个我不明白的错误,我尝试了所有的解决方案,但是没有找到.
此错误:
`$<-.data.frame`(`* tmp *`,日期,value = c(-719143,-718962,-718778,:替换有18行,数据有871
我希望以后可以选择日期范围",例如最后30天.
你能帮我吗?
谢谢
您有数据导入问题,而不是绘图问题.具体来说:
- 您没有提供CSV文件的小示例.这使您很难知道您的CSV文件实际上是正确的,并且可以通过调用的函数按预期进行解析.我将假设"Myfile.csv":
日期,日期和温度1
1,9/15/2014,85
2,9/16/2014,85
3,9/17/2014,88
4,9/18/2014,83
5,9/19/2014,83
- 似乎缺少一个依赖项:read_csv文件不在基R中.也许您是说readr :: read_csv吗?我在这篇文章的其余部分中都假设了这一点.
- 关键是我在这里看不到任何代码告诉R您的第二列是日期.我也看不到有关该日期的格式和应如何解析的信息.仔细阅读
注意:如果您可以选择日期的表示方式,请选择一种标准格式,例如 ISO 8601 .这将简化您的任务,因为阅读器将能够自动猜测它正在处理日期.
I have table as presented below:
Day |Date |Temp1 ----|-----------|--------------- 1 |9/15/2014 |85 2 |9/16/2014 |85 3 |9/17/2014 |88 4 |9/18/2014 |83 .. |..... |.. 871 |6/8/2017 |98
I have a lot of data as Date columns 9/15/2014 until 6/8/2017. And I chose to take value all the 6 months for an ggplot clean.
This my code :
library(ggplot2) #library(Rserve) #Rserve(args = "--vanilla") Test <- read_csv("C:/Users/..../Desktop/Myfile.csv") valueDatemax <- max(Test$Date) valueDatemin <- min(Test$Date) # Simulate some data df <- data.frame(Test, Test$Date <- seq.Date(from = as.Date(valueDatemin), to = as.Date(valueDatemax), by="6 month")) require(ggplot2) p<-ggplot(df, aes(x=Test$Date, y=Temp1)) print(p)
But I have an error that I don’t understand and I tried all solutions on net but I didn’t found.
this the error :
Error in `$<-.data.frame`(`*tmp*`, Date, value = c(-719143, -718962, -718778, : replacement has 18 rows, data has 871
And i would like after to can choose my Date range , per example 30 last days.
Can you help me ?
Thank you
解决方案You have a data import problem, not a plotting problem. Specifically:
- You did not provide a small example of your CSV file. This makes it hard to know that your CSV file is actually correct and can be parsed as-expected by the functions you call. I will assume "Myfile.csv":
Day, Date, Temp1
1, 9/15/2014, 85
2, 9/16/2014, 85
3, 9/17/2014, 88
4, 9/18/2014, 83
5, 9/19/2014, 83
- There seem to be a missing dependency: the read_csv file is not in base R. Perhaps you meant readr::read_csv? I am assuming this for the rest of this post.
- The critical point is that I see no code here that tells R that your second column is a date. Neither do I see information about how this date is formatted and should be parsed. Have a good look at the readr vignette.
- No need to use
seq.Date
, just make sure your data is of class "Date" and callseq
on it.
The corrected code could look like:
library(ggplot2) library(readr) Test <- read_csv( "Myfile.csv", col_types=list( Day=col_integer(), Date=col_date("%m/%d/%Y"), Temp1=col_integer() ) ) print(class(Test$Date)) # a Date object as expected valueDatemax <- max(Test$Date) valueDatemin <- min(Test$Date) date <- seq(from = valueDatemin, to = valueDatemax, by="2 days") # TODO: change "by" as needed in the final code Test <- Test[Test$Date %in% date, ] # keep only the desired dates p <- ggplot(Test, aes(Date, Temp1)) + geom_point() print(p)
Note: If you have the choice on how your dates are represented, choose a standard format, like ISO 8601. This will facilitate your task because readr will be able to automatically guess it is dealing with dates.
这篇关于根据精确范围和R中的采样可视化ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!