Reputation: 135
I have a graph that shows plant development over time. As time, I use growing degree days (its basically a combined measure of time and temperature). Now I would like to add a second x-axis at the top that shows the 'real' time, ie weeks from the start of the experiment, so that the viewer can have a clearer idea of what happened when. Because of the nature of growing degree days, they can't be converted to weeks in any mathematical way. So I would have to individually add the information, eg 1 week is at 500 gdd, 2 weeks at 800 gdd, 3 weeks at 1400 gdd. This information wouldn't in any way correspond with the content of the graph itself.
Here is my simple code for a graph with growing degree days.
my.df<-data.frame(ccc=seq(0,15,1),gdd=seq(0,1500,100))
ggplot(data=my.df, aes(x=gdd, y=ccc))+
geom_line()
Now all I would need are axis ticks at the top at 500, 800 and 1400 gdd that say 1, 2 and 3 and an axis label that says "weeks". Is there a way of doing that? My only idea so far is to add it in photoshop, but thats cheating :)
Upvotes: 2
Views: 67
Reputation: 60060
You can use sec_axis()
for this. Since you basically just want to label a few positions manually you don't have to worry about a complicated transformation.
ggplot(data=my.df, aes(x=gdd, y=ccc))+
geom_line() +
scale_x_continuous(sec.axis = sec_axis(
trans = ~ .,
name = "Weeks",
breaks = c(500, 800, 1400),
labels = c(1, 2, 3)))
Output:
Upvotes: 5