2

这是我的数据框的前 10 行:

head(test.data,10)
# A tibble: 10 x 5
       date             o2.permeg co2.ppm  apo        o2.spike
      <time>              <dbl>   <dbl>    <dbl>       <chr>
1  2015-01-01 00:00:00   -685.09 413.023 -354.1816        N
2  2015-01-01 00:02:00   -695.10 412.894 -364.8690        N
3  2015-01-01 00:04:00   -687.84 412.979 -357.1627        N
4  2015-01-01 00:06:00   -683.23 412.866 -353.1460        N
5  2015-01-01 00:08:00   -683.28 412.755 -353.7788        N
6  2015-01-01 00:10:00   -685.40 412.647 -356.4659        N
7  2015-01-01 00:12:00   -687.80 412.659 -358.8029        N
8  2015-01-01 00:14:00   -662.79 412.665        NA        Y
9  2015-01-01 00:16:00   -684.17 412.762 -354.6321        N
10 2015-01-01 00:18:00   -680.37 412.720 -351.0526        N

如您所见,最后一列名为 o2.spike,其中包含字符 N 和 Y。N 表示数据点不是尖峰,Y 表示它是尖峰。在这个示例中,只有 1 个 Y,但在真实帧中,有负载,并且是随机放置的。

我的愿望是在一个图中绘制所有数据点,而那些标有 Y 的点将以不同的颜色绘制。

供您参考,这是我用来绘制所有内容的当前代码。前 3 个变量以红色、绿色和蓝色绘制,我希望将“Y”行绘制为例如粉红色。

library(openair)
test.data$yr_day <- format(as.Date(test.data$date), "%Y-%m-%d")
dir.create(daily) # where "daily" is the path of the folder I want to save the plots into
for (d in unique(test.data$yr_day)) {
mypath <- file.path(daily, paste(name, d, ".png", sep = "" )) 
png(filename = mypath, width = 963, height = 690) 
timePlot(subset(test.data, yr_day == d), 
       plot.type = "p",
       pollutant = c("co2.ppm", "o2.permeg", "apo"), 
       y.relation = "free",
       date.pad = TRUE,
       pch = c(19,19,19),
       cex = 0.2,
       xlab = paste("Time of day in hours on", d),
       ylab = "CO2, O2, and APO concentrations",
       name.pol = c("CO2 (ppm)", "O2 (per meg)", "APO (per meg)"),
       date.breaks = 24,
       date.format = "%H:%M"
  )
 dev.off()
} 

一个示例图(包含与非尖峰颜色相同的所有尖峰)如下: 在此处输入图像描述

那么如何绘制与其他颜色不同的尖峰呢?非常感谢!

编辑:正如塞巴斯蒂安所问,我已经添加了这个(不知道你们如何从中提取数据)

dput(head(test.data,20))

structure(list(date = structure(c(1420070400, 1420070520, 1420070640, 
1420070760, 1420070880, 1420071000, 1420071120, 1420071240, 1420071360, 
1420071480, 1420071600, 1420071720, 1420071840, 1420071960, 1420072080, 
1420072200, 1420072320, 1420072440, 1420072560, 1420072680), class =    c("POSIXct", 
"POSIXt"), tzone = "GMT"), o2.permeg = c(-685.09, -695.1, -687.84, 
-683.23, -683.28, -685.4, -687.8, -662.79, -684.17, -680.37,     
-684.66, -686.13, -683.27, -680.77, -682.16, -692.54, NA, NA, 
NA, NA), co2.ppm = c(413.023, 412.894, 412.979, 412.866, 412.755, 
412.647, 412.659, 412.665, 412.762, 412.72, 412.692, 412.71, 
412.757, 412.838, 412.922, 413.019, NA, NA, NA, NA), apo =   c(-354.181646778043, 
-364.868973747017, -357.162673031026, -353.145990453461, -353.778806682578, 
-356.465871121718, -358.802863961814, NA, -354.632052505966, 
-351.052577565632, -355.489594272076, -356.86508353222, -353.75830548926, 
-350.833007159904, -351.781957040573, -361.652649164678, NA, 
NA, NA, NA), o2.spike = c("N", "N", "N", "N", "N", "N", "N", 
"Y", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N"
)), .Names = c("date", "o2.permeg", "co2.ppm", "apo", "o2.spike"
), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"
))
4

1 回答 1

2

不幸的是,没有数据,要回答这个问题并不容易。ggplot2 解决方案可能是:

g1 <- ggplot(data=test.data, aes(x=date, y=o2.permeg, col=o2.spike)) + geom_point()
g1

将数据框的一列传递给“aes”中的参数“col”,可以使您使用不同颜色映射该列中的每个不同值。它甚至创建了一个图例,名称与不同的颜色相关联。

我用另一个数据框(“iris”,包含在基础 R 中)尝试了这个,它有效,希望它会有所帮助。

编辑:

要进行并排绘图,您可以使用 ggplot 创建 3 个绘图,并使用“cowplot”包提供的函数 plot_grid()。

library(cowplot)
g1 <- ggplot(data=test.data, aes(x=date, y=o2.permeg, col=o2.spike)) + geom_point()
g2 <- ggplot(data=test.data, aes(x=date, y=co2.ppm, col=o2.spike)) + geom_point()
g3 <- ggplot(data=test.data, aes(x=date, y=apo, col=o2.spike)) + geom_point()
plot_grid(g1, g2, g3, nrow=3, ncol=1)
于 2016-07-15T14:20:51.027 回答