0

I am trying to plot minimum and maximum temperatures, and with a normal dot plot (using pch = 16) it looks normal, but when I change the plot type to line (using type="l"), it adds a line that seems to connect the first and last values.

Is there a way to get rid of the straight line that connects the first and last value & why is that happening?

Here's the Data Structure:

> 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    

Here's the plot code:

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))

Here's the line plot: Minimum and maximum temperatures

Here's the point plot: enter image description here

Community
  • 1
  • 1
kslayerr
  • 819
  • 1
  • 11
  • 21
  • 1
    what is the min and max date in your data set? I think because you don't have year, you may have a 'time-traveling' issue... – pyll Jul 20 '17 at 13:25
  • The min date is 07/01 and the max is 06/30 (starting 7/1/16, ending 6/30/17). Was trying to set that so it started at 7/1 and ended at 6/30 but could not get that to work - only plots Jan - Dec – kslayerr Jul 20 '17 at 13:26
  • I am 99% sure this issue is due to no year. – pyll Jul 20 '17 at 13:28
  • interesting it only shows up in the line, though! I will see if adding a year helps... you are probably right.... – kslayerr Jul 20 '17 at 13:28
  • what R is doing is attempting to chronologically link the points....doesn't show up in scatter because R doesn't care about the order so much. R is getting confused because your data ends and then it connects it back to the first point. – pyll Jul 20 '17 at 13:29
  • 1
    That was exactly the problem... thank you!! – kslayerr Jul 20 '17 at 13:33

2 Answers2

3

Probably this happens because you have the same start and end dates (they may differ in years, but you have month + day only). See the example:

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")

The picture will be like this: enter image description here

hannarud
  • 121
  • 3
  • 6
  • That was exactly the problem. I added years to the data and now it plots without the line. Thank you for your answer! – kslayerr Jul 20 '17 at 13:35
1

This issue is likely due to missing year...try adding a year.

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))
pyll
  • 1,688
  • 1
  • 26
  • 44