3

I want to plot the wind speed at three different sites with x = date and y = wind speed.

The plot I want should basically look like this:

enter image description here Except I want the full date instead of just the day on the x-axis. The graph above is just a workaround, as the code I wanted to use produces a glitch described below.

The data:

> u10
          date u10.TXL u10.MF u10.THF
1   2013-05-01     2.9    2.0     3.5
2   2013-05-02     3.1    2.1     4.1
3   2013-05-03     2.8    2.0     3.4

The code I wanted to use:

plot(u10$date[month==5&year==2013],u10$u10.MF[month==5&year==2013],
     type="b", col="green",
     main="wind speed at three sites", ylab="wind speed [m/s]", xlab="date",
     xlim=c(1,30), ylim=c(0,12),
     las=0)

lines(u10$date[month==5&year==2013],u10$u10.THF[month==5&year==2013], col="red")
lines(u10$date[month==5&year==2013],u10$u10.TXL[month==5&year==2013], col="blue")

points(u10$date[month==5&year==2013],u10$u10.THF[month==5&year==2013], col="red")
points(u10$date[month==5&year==2013],u10$u10.TXL[month==5&year==2013], col="blue")

Results in the following graph:

enter image description here

As you can see, the first, "original" data is not plotted with standard points and a line, despite me using

type="b"

I even tried to use the "pch" command, which will just add the symbol over the "minus" sign I automatically get.

Question: Why do I have this kind of plot and why can't I change it? Is it because the date is defined as date and R just assumes the wind value for every time of the day? But why does it work for the second and third line?

The workaround seems to work, but I just wonder what I did wrong. Also, I didn't find out how to add the full date with the workaround solution.

Thanks in advance for any help

PS:Here is the code I used for the workaround:

plot(u10$date[month==5&year==2013],NULL,
     type="p", pch="",
     main="wind speed at three sites", ylab="wind speed [m/s]", xlab="date",
     xlim=c(1,30), ylim=c(0,12),
     las=0)

lines(u10$date[month==5&year==2013],u10$u10.MF[month==5&year==2013], col="green")
lines(u10$date[month==5&year==2013],u10$u10.THF[month==5&year==2013], col="red")
lines(u10$date[month==5&year==2013],u10$u10.TXL[month==5&year==2013], col="blue")

points(u10$date[month==5&year==2013],u10$u10.MF[month==5&year==2013], col="green")
points(u10$date[month==5&year==2013],u10$u10.THF[month==5&year==2013], col="red")
points(u10$date[month==5&year==2013],u10$u10.TXL[month==5&year==2013], col="blue")
tonytonov
  • 25,060
  • 16
  • 82
  • 98
Anne
  • 377
  • 2
  • 4
  • 16
  • Could you please edit your question to provide the output of `dput(head(u10,10))`? – Stephan Kolassa May 16 '14 at 18:30
  • In addition, I am not entirely clear on what exactly you are trying to achieve. Is it just the matter of getting the correct date on the x axis? – Stephan Kolassa May 16 '14 at 18:31
  • if the first plot is what you want, just use that code and suppress the x axis and add the date labels in manually with mtext or axis – rawr May 16 '14 at 18:31

2 Answers2

1

I tried your code with the minimal sample of your data. I also get the horizontal bars or minuses when I supply the dates as simple strings. If you add them to a data.frame, these strings are turned to Factor format (str(u10$date)) and if you attempt to plot these factors (?plot.factor):

This functions implements a scatterplot method for factor arguments of the generic plot function. If y is missing barplot is produced. For numeric y a boxplot is used, and for a factor y a spineplot is shown. For any other type of y the next plot method is called, normally plot.default.

So in your case, R attempts to draw a boxplot (Factor on x, numeric on y axis). The minuses are in fact squashed boxplots.

You can solve this by supplying date in a Date format:

# Generate data
date = c(as.Date(c('2013-05-01', '2013-05-02', '2013-05-03')))
str(date) # 'Date'
u10.TXL = c(2.9,3.1,2.8)
u10.MF = c(2.0,2.1,2.0)
u10.THF = c(3.5,4.1,3.4)
u10 = data.frame(date, u10.TXL, u10.MF, u10.THF)

# Plot the lot:
plot(u10$date,u10$u10.MF,
     type="b", col="green",
     main="wind speed at three sites", ylab="wind speed [m/s]", xlab="date",
     ylim=c(0,12),
     las=0)

lines(u10$date,u10$u10.THF, col="red", type='b')
lines(u10$date,u10$u10.TXL, col="blue", type='b')

BTW, you can do lines of type 'b' to add points and lines at the same time.

koekenbakker
  • 3,524
  • 2
  • 21
  • 30
0

I have encountered the same problem when the class of the variable being plotted on the x-axis is 'ordered factor.' Try recoding 'date' as a 'numeric' variable and you will probably get the plot format that you want.

Larry
  • 45
  • 5