简体   繁体   English

纯粹将x轴添加到ggplot线图中

[英]Add secondary x-axis to ggplot line graph, purely for information

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. 现在,我想在顶部添加第二个x轴,以显示“真实”时间,即从实验开始几周,以便观众可以更清楚地了解何时发生了什么。 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. 因此,我将不得不单独添加信息,例如1周是500 gdd,2周是800 gdd,3周是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". 现在,我需要做的是在顶部的轴刻度线分别为500、800和1400 gdd,分别是1、2和3,以及轴标签为“ weeks”。 Is there a way of doing that? 有办法吗? My only idea so far is to add it in photoshop, but thats cheating :) 到目前为止,我唯一的想法是将其添加到photoshop中,但是多数民众赞成在作弊:)

You can use sec_axis() for this. 您可以sec_axis()使用sec_axis() 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: 输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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