简体   繁体   中英

Straight line being added to line plot?

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: 最低和最高温度

Here's the point plot: 在此处输入图片说明

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: 在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM