2017-07-20 58 views
0

我试图绘制最小和最大温度,并与正常点图(使用pch = 16)它看起来很正常,但是当我将绘图类型更改为行(使用type =“l”)时,它添加了一条线这似乎连接了第一个和最后一个值。直线被添加到线图?

有没有办法摆脱连接第一个和最后一个值的直线&为什么会发生这种情况?

这里的数据结构:

> y 
Source: local data frame [365 x 6] 
Groups: Month [12] 

    Month Day Tmp_min MonthDay_min Tmp_max 
    <fctr> <chr> <dbl>  <chr> <dbl>  
1  07 01  62  07/01  69  
2  07 02  61  07/02  67  
3  07 03  60  07/03  66  
4  07 04  60  07/04  64  
5  07 05  60  07/05  65  
6  07 06  61  07/06  66  
7  07 07  61  07/07  67  
8  07 08  61  07/08  69  
9  07 09  61  07/09  70  
10  07 10  62  07/10  69  

这里的情节代码:

plot(Tmp_min ~ as.Date(y$MonthDay_min, format='%m/%d'), data=y, type="l", 
col="turquoise", ylab="Temperature (F)", 
    main=paste("Minimum and Maximum Daily Temperatures"), xlab="Month", 
    ylim=c(0,100)) 

points(Tmp_max ~ as.Date(y$MonthDay_min, format='%m/%d'), data=y, type="l", 
    col="orange", ylim=c(0,100)) 

这里的线图: Minimum and maximum temperatures

以下是积分图: enter image description here

+1

什么是你的数据的最小值和最大值设置时间为?我想因为你没有一年,你可能会有'时间旅行'的问题... – pyll

+0

最短日期是07/01,最大是06/30(从7/1/16开始,结束于6/30/17)。试图设置,所以它开始于7/1,并在6/30结束,但无法得到的工作 - 只有阴谋1月至12月 – kslayerr

+0

我99%肯定这个问题是由于没有一年。 – pyll

回答

1

此问题可能是由于缺少一年...尝试添加一年。

MonthDay_min <- c('07/01', '07/02', '07/03', '07/04', '06/30') 
Tmp_min <- c(62, 70, 61, 58, 100) 
Tmp_max <- c(69, 78, 66, 64, 105) 

y <- data.frame(MonthDay_min, Tmp_min, Tmp_max) 

year <- c(2016, 2016, 2016, 2016, 2017) 

y$MonthDay_min <- paste(y$MonthDay_min, '/', year, sep = "") 

plot(Tmp_min ~ as.Date(y$MonthDay_min, format='%m/%d/%Y'), data=y, type="l", 
    col="turquoise", ylab="Temperature (F)", 
    main=paste("Minimum and Maximum Daily Temperatures"), xlab="Month", 
    ylim=c(0,100)) 

points(Tmp_max ~ as.Date(y$MonthDay_min, format='%m/%d/%Y'), data=y, type="l", 
     col="orange", ylim=c(0,100)) 
2

这大概是因为你有相同的开始和结束日期(它们可以在不同的岁月,但你必须月+日只)。见例如:

date_seq = seq.Date(from = as.Date("2017/1/1"), to = as.Date("2018/1/1"), by = "day") 
date_seq_month_day <- format(date_seq, format="%m-%d") 
daily_white_noise = rnorm(length(date_seq)) 
dataframe <-data.frame(days = date_seq_month_day, observations = daily_white_noise) 
plot(observations ~ as.Date(date_seq_month_day, format='%m-%d'), data=dataframe, type="l", col="turquoise", ylab="Temperature (F)", main=paste("Minimum and Maximum Daily Temperatures"), xlab="Month") 

图片将是这样的: enter image description here

+0

这正是问题所在。我在数据上添加了多年,现在它没有线路。谢谢您的回答! – kslayerr