Ziyuan
Ziyuan

Reputation: 4568

Attach arrows to points

I am visualizing a 4 dimensional data set. Let's denote the variables as x, y1, y2 and y3, where x is for dates, y is a continuous variable and y2, y3 are components of 2 dimensional vectors (y2, y3). Now I want to a line plot for x and y1, additionally attaching arrows for (y2, y3) at points (x, y1).

I have tried

ggplot(data=data,aes(x=x,y=y1)) + geom_line() +
      geom_segment(aes(xend=x+y2,yend=y1+y3), arrow = arrow())

but it doesn't work well so I think I may need to do some rescaling. How can I do this with ggplot?

UPDATE: I've attached a sample data set (together with its column definition). The data set contains oceanographic and surface meteorological readings taken from a series of buoys positioned throughout the equatorial Pacific. The data is expected to aid in the understanding and prediction of El Nino/Southern Oscillation (ENSO) cycles (from the description of the repository). Now, for example, I want to visualize x=day, y1=humidity, y2=zon.winds, y3=mer.winds with the symbol described above.

UPDATE2: for example, I want to plot this for a particular buoy enter image description here

Upvotes: 1

Views: 156

Answers (1)

MrGumble
MrGumble

Reputation: 5776

I am having trouble figuring what you want to display. As far as I can see, your dataset has 50 buoys that each deliver a measurement each day.

library(ggplot2)
elnino <- read.table('elnino.txt', col.names=c('buoy','day','latitude','longitude','zon.winds','mer.winds','humidity','air.temp','ss.temp'), as.is=TRUE, na='.')
elnino <- elnino[elnino$humidity > 40,]  # removing a single point that seems to be an outlier.

ggplot(elnino, aes(x=day,y=humidity, group=buoy)) + geom_point()
ggplot(elnino, aes(x=day,y=humidity, group=buoy)) + geom_line()

Which gives these two results. enter image description here enter image description here

What I cannot see is how do you want to display the ''zon.winds'' and ''mer.winds'' variables? I figure these in combination gives a vector, but where do you want these placed? You would get ~ 700 arrows filling your plot.

Update In that case, you got it right, that you have to use geom_segment and calculate the ''x'', ''xend'', ''y'' and ''yend'', see geom_segment.

# We select a single buoy
el <- subset(elnino, buoy==1)

library(grid)
ggplot(el, aes(x=day,y=humidity, group=buoy)) + geom_line() + geom_segment(aes(yend=humidity+zon.winds, xend=day+mer.winds), arrow = arrow(length = unit(0.1,"cm")))

This however doe not look very nice, because the coordinates in ''zon.winds'' and ''mer.winds'' are taken as absolutes! So to utilise them, we will need to do some manual transformation of them. My values are absolute arbitrarily.

el <- transform(el, zon.winds = zon.winds * -0.3, mer.winds=mer.winds * -0.3)

enter image description here

Upvotes: 2

Related Questions