R - 绘制温度曲线随时间变化

时间:2015-07-07 08:39:23

标签: r plot profile temperature

我有以下数据框,包含多个深度和日期的水温测量值:

wtemp <- structure(list(Date = structure(c(12604, 12604, 12604, 12604, 
12604, 12604, 12604, 12604, 12604, 12604, 12604, 12604, 12604, 
12604, 12604, 12680, 12680, 12680, 12680, 12680, 12680, 12680, 
12680, 12680, 12680, 12680, 12680, 12680, 12680, 12680, 12714, 
12714, 12714, 12714, 12714, 12714, 12714, 12714, 12714, 12714, 
12714, 12714, 12714, 12714, 12714, 12751, 12751, 12751, 12751, 
12751, 12751, 12751, 12751, 12751, 12751, 12751, 12751, 12751, 
12751, 12751, 12770, 12770, 12770, 12770, 12770, 12770, 12770, 
12770, 12770, 12770, 12770, 12770, 12770, 12770, 12770, 12806, 
12806, 12806, 12806, 12806, 12806, 12806, 12806, 12806, 12806, 
12806, 12806, 12806, 12806, 12806, 12848, 12848, 12848, 12848, 
12848, 12848, 12848, 12848, 12848, 12848, 12848, 12848, 12848, 
12848, 12848, 12885, 12885, 12885, 12885, 12885, 12885, 12885, 
12885, 12885, 12885, 12885, 12885, 12885, 12885, 12885, 12918, 
12918, 12918, 12918, 12918, 12918, 12918, 12918, 12918, 12918, 
12918, 12918, 12918, 12918, 12918, 12987, 12987, 12987, 12987, 
12987, 12987, 12987, 12987, 12987, 12987, 12987, 12987, 12987, 
12987, 12987, 13015, 13015, 13015, 13015, 13015, 13015, 13015, 
13015, 13015, 13015, 13015, 13015, 13015, 13015, 13015, 13051, 
13051, 13051, 13051, 13051, 13051, 13051, 13051, 13051, 13051, 
13051, 13051, 13051, 13051, 13051, 13103, 13103, 13103, 13103, 
13103, 13103, 13103, 13103, 13103, 13103, 13103, 13103, 13103, 
13103, 13103), class = "Date"), Depth = structure(c(1L, 7L, 15L, 
16L, 17L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 1L, 7L, 
15L, 16L, 17L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 1L, 
7L, 15L, 16L, 17L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 
1L, 7L, 15L, 16L, 17L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 
12L, 1L, 7L, 15L, 16L, 17L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 
11L, 12L, 1L, 7L, 15L, 16L, 17L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 
10L, 11L, 12L, 1L, 7L, 15L, 16L, 17L, 2L, 3L, 4L, 5L, 6L, 8L, 
9L, 10L, 11L, 12L, 1L, 7L, 15L, 16L, 17L, 2L, 3L, 4L, 5L, 6L, 
8L, 9L, 10L, 11L, 12L, 1L, 7L, 15L, 16L, 17L, 2L, 3L, 4L, 5L, 
6L, 8L, 9L, 10L, 11L, 12L, 1L, 7L, 15L, 16L, 17L, 2L, 3L, 4L, 
5L, 6L, 8L, 9L, 10L, 11L, 12L, 1L, 7L, 15L, 16L, 17L, 2L, 3L, 
4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 1L, 7L, 15L, 16L, 17L, 2L, 
3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 1L, 7L, 15L, 16L, 17L, 
2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L), .Label = c("0", "10", 
"12", "14", "16", "18", "2", "20", "22", "24", "26", "28", "30", 
"32", "4", "6", "8", "AR"), class = "factor"), T_water = c(33L, 
33L, 32L, 32L, 31L, 31L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 
29L, 32L, 32L, 32L, 32L, 31L, 31L, 30L, 30L, 30L, 30L, 30L, 30L, 
30L, 30L, 30L, 33L, 33L, 32L, 32L, 31L, 31L, 30L, 30L, 30L, 30L, 
30L, 30L, 30L, 30L, 30L, 32L, 32L, 32L, 31L, 31L, 31L, 31L, 31L, 
31L, 31L, 31L, 31L, 31L, 30L, 30L, 31L, 31L, 31L, 31L, 31L, 31L, 
31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 32L, 30L, 30L, 30L, 30L, 
30L, 30L, 30L, 30L, 30L, 31L, 31L, 31L, 31L, 31L, 31L, 30L, 30L, 
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 29L, 29L, 28L, 28L, 28L, 
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 
30L, 30L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 
29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 
29L, 29L, 28L, 28L, 28L, 28L, 31L, 31L, 31L, 31L, 30L, 30L, 30L, 
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 32L, 32L, 32L, 32L, 31L, 
31L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 33L, 33L, 32L, 
32L, 31L, 31L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L)), .Names = c("Date", 
"Depth", "T_water"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 17L, 18L, 19L, 20L, 21L, 
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 33L, 34L, 35L, 
36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 49L, 
50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 
63L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 
77L, 78L, 79L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 
91L, 92L, 93L, 94L, 95L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 
104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 113L, 114L, 115L, 
116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L, 
127L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L, 139L, 140L, 
141L, 142L, 143L, 144L, 145L, 149L, 150L, 151L, 152L, 153L, 154L, 
155L, 156L, 157L, 158L, 159L, 160L, 161L, 162L, 163L, 167L, 168L, 
169L, 170L, 171L, 172L, 173L, 174L, 175L, 176L, 177L, 178L, 179L, 
180L, 181L, 185L, 186L, 187L, 188L, 189L, 190L, 191L, 192L, 193L, 
194L, 195L, 196L, 197L, 198L, 199L, 203L, 204L, 205L, 206L, 207L, 
208L, 209L, 210L, 211L, 212L, 213L, 214L, 215L, 216L, 217L), class = "data.frame")

在x轴上,我需要显示日期,在y轴上显示温度,多条线对应于深度。具体来说,我试图重现这个数字:

enter image description here

我正在尝试使用此命令:

library(lattice)
xyplot(T_water ~ Date, data = wtemp, type = "l", auto.key=TRUE)

仅部分有效。我也需要绘制其他线条。它不需要使用晶格,也可以使用其他库(例如ggplot2)。

如何实现?

由于

2 个答案:

答案 0 :(得分:1)

根据评论,我得出以下答案:

xyplot(T_water ~ Date, group = Depth, data = wtemp, t='l')

然而,情节线条太多,我想只保留深度01224。我就是这样做的:

# Select depths
wtemp <- wtemp[wtemp$Depth %in% c("0","12","24"),]

# Drop old depths
wtemp$Depth <- levels(droplevels(wtemp$Depth))

# Plot new data
xyplot(T_water ~ Date, group = Depth, data = wtemp, t='b',auto.key =list(points = FALSE, columns=3, lines = TRUE))

感谢那些帮助过的人。

答案 1 :(得分:1)

这是我的建议。我正在使用正常的绘图功能,这应该有效:

# First get a subset of your dataframe of the rows where depth=0
sub <- wtemp[wtemp$Depth==0,]
# Define line colours
colours <- rainbow(14)
# And draw the respective plot
plot(sub$Date,sub$T_water,type="l",col=colours[1], xaxt='n')
# Draw nicer ticks
minDate <- as.Date("2004-07-05")
maxDate <- as.Date("2005-11-16")
axis.Date(1, at=seq(minDate,maxDate ,"month"), format="%m/%d/%Y")
# Loop through all other subsets and create a line which gets added to the plot 
for(i in 2:28){
    sub <- wtemp[wtemp$Depth==i,]
    lines(sub$Date,sub$T_water,col=colours[i/2+1])
}
# Add a legend: http://www.r-bloggers.com/adding-a-legend-to-a-plot/
legend(as.Date("2005-05-05"),33,           # legend position 
seq(0,28,2),            # what to display: values 0 to 28 with an interval of 2 
lty=1,                  # gives the legend appropriate symbols (lines)
lwd=2.5,                # line width
col=colours)            # gives the legend lines the correct colour as defined above

更新:如果您只想显示所选值:

# Define values to display
valueDsp <- c(0,12,24)
# First get a subset of your dataframe of the rows where depth=0
sub <- wtemp[wtemp$Depth==valueDsp[1],]
# Define line colours
colours <- rainbow(length(valueDsp))
# And draw the respective plot
plot(sub$Date,sub$T_water,type="l",col=colours[1], xaxt='n')
# Draw nicer ticks
minDate <- as.Date("2004-07-05")
maxDate <- as.Date("2005-11-16")
axis.Date(1, at=seq(minDate,maxDate ,"month"), format="%m/%d/%Y")
# Loop through all other subsets and create a line which gets added to the plot 
for(i in 2:length(valueDsp)){
    sub <- wtemp[wtemp$Depth==valueDsp[i],]
    lines(sub$Date,sub$T_water,col=colours[i])
}
# Add a legend: http://www.r-bloggers.com/adding-a-legend-to-a-plot/
legend(as.Date("2005-05-05"),33,           # legend position 
valueDsp,            # what to display: values 0 to 28 with an interval of 2 
lty=1,                  # gives the legend appropriate symbols (lines)
lwd=2.5,                # line width
col=colours)            # gives the legend lines the correct colour as defined above

您可以轻松调整脚本以更改颜色,显示值,图例等。

聚苯乙烯。您是否知道数据集中缺少以下几个月:

2004-08
2005-02
2005-06
2005-10

亲切的问候