5

I make plots of environmental measurements along vertical profiles, e.g. down a sediment core or as a function of depth in the ocean. By convention these plots are presented vertically, with the independent variable (depth) along the y axis. Lines should therefore connect points of adjacent y-value.

The "line" geom in ggplot2 seems only to connect points of adjacent x value. Is there a way around this?

This example creates some realistic-looking data and illustrates the problem:

#generate fake data
sites<-factor(c(rep("site A", 10), rep("site B", 10)))
depths<-rep(1:10, 2)
values<-c(runif(10), runif(10)+2)

#make a visually pleasing scatter plot
qplot(values, depths, geom="point", col=sites)

You can see from that plot that we're looking at measurements related by depth. But:

#make a visually meaningless scatter plot
qplot(values, depths, geom="line", col=sites)

connects the points in a meaningless way. Is there any way to connect the points vertically?

1 Answer 1

6
qplot(depths, values, geom="line", group=sites) + coord_flip()

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't know about coord_flip(). Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.