ggplot facet和条件geom_point

时间:2018-04-22 08:30:08

标签: r ggplot2

我正在尝试用ggplot和facet制作密度图。我有不同日期从不同站点收集的数据,并且根据它所在的日期/站点,我想在相应的facet子图中仅绘制具有geom_point的站点。但是,我在条件方面遇到了困难。

按照here指令,我尝试使用一个函数,但得到一个“ggplot2不知道如何处理类数字的数据”错误。当我将其转换为数据帧时,我得到了一个不同的错误。

ff <- function(x){ifelse(2017$Station=="station1", c(-156.6315, 20.829), c(-156.53653, 20.77524))}

ggplot(2017, aes(x=Long, y=Lat) ) +
    stat_density_2d(aes(fill = ..level..), geom = "polygon") +
    scale_fill_distiller(palette="Spectral", direction=-1) +
    geom_point(data=ff(2017), aes(), colour="black") +      
    facet_wrap(~Date)

在使用facet分割数据时,是否有另一种方法可以向geom_point添加条件?

编辑以包含前两天的数据

            Date  Time      Lat      Long      Station
1148 2017-01-26 08:41:09 20.70739 -156.4851    station1
1149 2017-01-26 08:42:53 20.73788 -156.4927    station1
1150 2017-01-26 08:43:27 20.71609 -156.4662    station1
1151 2017-01-26 08:45:18 20.68044 -156.4511    station1
1152 2017-01-26 08:48:28 20.71747 -156.5016    station1
1153 2017-01-26 08:49:13 20.72209 -156.5069    station1
1154 2017-01-26 08:52:21 20.73629 -156.5829    station1
1155 2017-01-26 08:56:50 20.78549 -156.6104    station1
1156 2017-01-26 09:01:05 20.72990 -156.5201    station1
1157 2017-01-26 09:03:24 20.76324 -156.5329    station1
1159 2017-01-26 09:30:10 20.77081 -156.5401    station1
1160 2017-01-26 09:31:06 20.75732 -156.5330    station1
1161 2017-01-26 09:35:09 20.71715 -156.5139    station1
1162 2017-01-26 09:36:34 20.68498 -156.5083    station1
1163 2017-01-26 09:37:30 20.71705 -156.5122    station1
1164 2017-01-26 09:39:34 20.67805 -156.5097    station1
1165 2017-01-26 09:43:28 20.69149 -156.5160    station1
1166 2017-01-26 09:50:18 20.77173 -156.6284    station1
1167 2017-01-26 09:53:48 20.77471 -156.5513    station1
1168 2017-01-26 10:02:00 20.76694 -156.5412    station1
1171 2017-01-26 10:43:39 20.76868 -156.5390    station1
1172 2017-01-26 10:46:20 20.71324 -156.5381    station1
1173 2017-01-26 10:47:26 20.75922 -156.5476    station1
1174 2017-01-26 10:49:47 20.61556 -156.5112    station1
1175 2017-01-26 10:50:15 20.63195 -156.5173    station1
1176 2017-01-26 10:52:47 20.74407 -156.4773    station1
1177 2017-01-26 10:57:54 20.74948 -156.5757    station1
1178 2017-01-26 11:02:19 20.77410 -156.5469    station1
1179 2017-01-26 11:35:12 20.77013 -156.5426    station1
1180 2017-01-26 11:35:34 20.76996 -156.5422    station1
1181 2017-01-26 11:37:03 20.76977 -156.5417    station1
1182 2017-01-26 11:43:38 20.74740 -156.5336    station1
1183 2017-01-26 11:44:15 20.74798 -156.5341    station1
1184 2017-01-26 11:45:20 20.76899 -156.5403    station1
1185 2017-01-26 11:46:20 20.76897 -156.5399    station1
1186 2017-01-26 11:48:10 20.75503 -156.5224    station1
1187 2017-01-26 11:49:41 20.75343 -156.5600    station1
1188 2017-01-26 11:50:36 20.77107 -156.5159    station1
1189 2017-01-26 11:52:53 20.77104 -156.5178    station1
1190 2017-01-26 11:53:59 20.76801 -156.5179    station1
1191 2017-01-26 11:54:49 20.76747 -156.5177    station1
1192 2017-01-26 11:57:29 20.76448 -156.5336    station1
1193 2017-01-26 11:58:07 20.76372 -156.5328    station1
1194 2017-01-26 12:01:03 20.74703 -156.5307    station1
1195 2017-01-26 12:04:21 20.76375 -156.5242    station1
1196 2017-01-26 12:31:03 20.77088 -156.5249    station1
1197 2017-01-26 12:31:26 20.77072 -156.5249    station1
1198 2017-01-26 12:32:53 20.75596 -156.5108    station1
1199 2017-01-26 12:43:20 20.75622 -156.5473    station1
1200 2017-01-26 12:56:23 20.76592 -156.6178    station1
1201 2017-01-26 12:58:28 20.75378 -156.5251    station1
1    2017-01-27 09:04:43 20.77168 -156.5855 station2
3    2017-01-27 09:07:32 20.78687 -156.5941 station2
4    2017-01-27 09:08:24 20.79017 -156.5891 station2
5    2017-01-27 09:15:49 20.82028 -156.6429 station2
6    2017-01-27 09:20:15 20.81687 -156.6544 station2
7    2017-01-27 09:25:44 20.76804 -156.7463 station2
8    2017-01-27 10:06:09 20.79725 -156.6469 station2
9    2017-01-27 10:09:41 20.81033 -156.6519 station2
10   2017-01-27 10:17:02 20.80004 -156.6318 station2
11   2017-01-27 10:19:59 20.79536 -156.7561 station2
12   2017-01-27 10:20:27 20.81162 -156.6923 station2
13   2017-01-27 10:22:28 20.80885 -156.7049 station2
14   2017-01-27 10:23:37 20.80862 -156.7241 station2
15   2017-01-27 10:25:44 20.81664 -156.6949 station2
16   2017-01-27 10:29:21 20.89242 -156.7773 station2
17   2017-01-27 11:04:07 20.79683 -156.6205 station2
18   2017-01-27 11:07:11 20.78653 -156.6084 station2
19   2017-01-27 11:08:54 20.79489 -156.6208 station2
20   2017-01-27 11:11:03 20.68624 -156.5848 station2
21   2017-01-27 11:12:26 20.78802 -156.5954 station2
22   2017-01-27 11:14:59 20.78673 -156.6850 station2
23   2017-01-27 11:22:00 20.83757 -156.8072 station2
24   2017-01-27 11:26:35 20.77718 -156.6970 station2
25   2017-01-27 12:04:06 20.77420 -156.6628 station2
26   2017-01-27 12:06:35 20.79871 -156.6504 station2
27   2017-01-27 12:06:59 20.79895 -156.6508 station2
28   2017-01-27 12:09:30 20.79087 -156.6102 station2
29   2017-01-27 12:12:53 20.82244 -156.7056 station2
30   2017-01-27 12:13:08 20.82628 -156.6616 station2
31   2017-01-27 12:18:52 20.83721 -156.7394 station2
32   2017-01-27 12:26:18 20.79902 -156.6977 station2
33   2017-01-27 13:03:39 20.79930 -156.6084 station2
34   2017-01-27 13:07:25 20.78338 -156.6148 station2
35   2017-01-27 13:09:15 20.75798 -156.5954 station2
36   2017-01-27 13:18:24 20.77019 -156.6830 station2
37   2017-01-27 13:20:42 20.81733 -156.6440 station2
38   2017-01-27 14:08:34 20.79685 -156.5975 station2
39   2017-01-27 14:13:03 20.78625 -156.5941 station2
40   2017-01-27 14:14:45 20.76980 -156.6095 station2
41   2017-01-27 14:18:04 20.81734 -156.6634 station2
42   2017-01-27 14:21:12 20.81628 -156.6588 station2
43   2017-01-27 14:25:42 20.78261 -156.6199 station2
44   2017-01-27 14:28:25 20.81209 -156.7029 station2
45   2017-01-27 14:29:10 20.80157 -156.7335 station2
46   2017-01-27 14:29:10 20.80157 -156.7335 station2

根据站点1或站点2是否在特定日期使用,我只想绘制该位置,而不是两者。

Density Plots

1 个答案:

答案 0 :(得分:2)

您可以创建一个新数据框pts来存储Lat&amp;基于所需条件的Long值,然后将其添加到ggplot以后

library(tidyverse) 

df2017 <- df2017 %>% 
  mutate(Date = as.Date(Date))

pts <- df2017 %>% 
  distinct(Date, .keep_all = TRUE) %>%
  select(Date, Station) %>% 
  mutate(Long = if_else(Station == "station1", -156.53653, -156.6315),
         Lat  = if_else(Station == "station1", 20.77524, 20.829))

ggplot(df2017, aes(x=Long, y=Lat)) +
    stat_density_2d(aes(fill=..level..), geom="polygon") +
    scale_fill_distiller(palette="Spectral", direction=-1) +
    geom_point(data=pts, aes(x=Long, y=Lat), colour="black", 
               shape=17, size=5) +
    facet_wrap(~ Date)

df2017 <- read.table(text = "Nr            Date  Time      Lat      Long      Station
    1148 2017-01-26 08:41:09 20.70739 -156.4851    station1
    1149 2017-01-26 08:42:53 20.73788 -156.4927    station1
    1150 2017-01-26 08:43:27 20.71609 -156.4662    station1
    1151 2017-01-26 08:45:18 20.68044 -156.4511    station1
    1152 2017-01-26 08:48:28 20.71747 -156.5016    station1
    1153 2017-01-26 08:49:13 20.72209 -156.5069    station1
    1154 2017-01-26 08:52:21 20.73629 -156.5829    station1
    1155 2017-01-26 08:56:50 20.78549 -156.6104    station1
    1156 2017-01-26 09:01:05 20.72990 -156.5201    station1
    1157 2017-01-26 09:03:24 20.76324 -156.5329    station1
    1159 2017-01-26 09:30:10 20.77081 -156.5401    station1
    1160 2017-01-26 09:31:06 20.75732 -156.5330    station1
    1161 2017-01-26 09:35:09 20.71715 -156.5139    station1
    1162 2017-01-26 09:36:34 20.68498 -156.5083    station1
    1163 2017-01-26 09:37:30 20.71705 -156.5122    station1
    1164 2017-01-26 09:39:34 20.67805 -156.5097    station1
    1165 2017-01-26 09:43:28 20.69149 -156.5160    station1
    1166 2017-01-26 09:50:18 20.77173 -156.6284    station1
    1167 2017-01-26 09:53:48 20.77471 -156.5513    station1
    1168 2017-01-26 10:02:00 20.76694 -156.5412    station1
    1171 2017-01-26 10:43:39 20.76868 -156.5390    station1
    1172 2017-01-26 10:46:20 20.71324 -156.5381    station1
    1173 2017-01-26 10:47:26 20.75922 -156.5476    station1
    1174 2017-01-26 10:49:47 20.61556 -156.5112    station1
    1175 2017-01-26 10:50:15 20.63195 -156.5173    station1
    1176 2017-01-26 10:52:47 20.74407 -156.4773    station1
    1177 2017-01-26 10:57:54 20.74948 -156.5757    station1
    1178 2017-01-26 11:02:19 20.77410 -156.5469    station1
    1179 2017-01-26 11:35:12 20.77013 -156.5426    station1
    1180 2017-01-26 11:35:34 20.76996 -156.5422    station1
    1181 2017-01-26 11:37:03 20.76977 -156.5417    station1
    1182 2017-01-26 11:43:38 20.74740 -156.5336    station1
    1183 2017-01-26 11:44:15 20.74798 -156.5341    station1
    1184 2017-01-26 11:45:20 20.76899 -156.5403    station1
    1185 2017-01-26 11:46:20 20.76897 -156.5399    station1
    1186 2017-01-26 11:48:10 20.75503 -156.5224    station1
    1187 2017-01-26 11:49:41 20.75343 -156.5600    station1
    1188 2017-01-26 11:50:36 20.77107 -156.5159    station1
    1189 2017-01-26 11:52:53 20.77104 -156.5178    station1
    1190 2017-01-26 11:53:59 20.76801 -156.5179    station1
    1191 2017-01-26 11:54:49 20.76747 -156.5177    station1
    1192 2017-01-26 11:57:29 20.76448 -156.5336    station1
    1193 2017-01-26 11:58:07 20.76372 -156.5328    station1
    1194 2017-01-26 12:01:03 20.74703 -156.5307    station1
    1195 2017-01-26 12:04:21 20.76375 -156.5242    station1
    1196 2017-01-26 12:31:03 20.77088 -156.5249    station1
    1197 2017-01-26 12:31:26 20.77072 -156.5249    station1
    1198 2017-01-26 12:32:53 20.75596 -156.5108    station1
    1199 2017-01-26 12:43:20 20.75622 -156.5473    station1
    1200 2017-01-26 12:56:23 20.76592 -156.6178    station1
    1201 2017-01-26 12:58:28 20.75378 -156.5251    station1
    1    2017-01-27 09:04:43 20.77168 -156.5855 station2
    3    2017-01-27 09:07:32 20.78687 -156.5941 station2
    4    2017-01-27 09:08:24 20.79017 -156.5891 station2
    5    2017-01-27 09:15:49 20.82028 -156.6429 station2
    6    2017-01-27 09:20:15 20.81687 -156.6544 station2
    7    2017-01-27 09:25:44 20.76804 -156.7463 station2
    8    2017-01-27 10:06:09 20.79725 -156.6469 station2
    9    2017-01-27 10:09:41 20.81033 -156.6519 station2
    10   2017-01-27 10:17:02 20.80004 -156.6318 station2
    11   2017-01-27 10:19:59 20.79536 -156.7561 station2
    12   2017-01-27 10:20:27 20.81162 -156.6923 station2
    13   2017-01-27 10:22:28 20.80885 -156.7049 station2
    14   2017-01-27 10:23:37 20.80862 -156.7241 station2
    15   2017-01-27 10:25:44 20.81664 -156.6949 station2
    16   2017-01-27 10:29:21 20.89242 -156.7773 station2
    17   2017-01-27 11:04:07 20.79683 -156.6205 station2
    18   2017-01-27 11:07:11 20.78653 -156.6084 station2
    19   2017-01-27 11:08:54 20.79489 -156.6208 station2
    20   2017-01-27 11:11:03 20.68624 -156.5848 station2
    21   2017-01-27 11:12:26 20.78802 -156.5954 station2
    22   2017-01-27 11:14:59 20.78673 -156.6850 station2
    23   2017-01-27 11:22:00 20.83757 -156.8072 station2
    24   2017-01-27 11:26:35 20.77718 -156.6970 station2
    25   2017-01-27 12:04:06 20.77420 -156.6628 station2
    26   2017-01-27 12:06:35 20.79871 -156.6504 station2
    27   2017-01-27 12:06:59 20.79895 -156.6508 station2
    28   2017-01-27 12:09:30 20.79087 -156.6102 station2
    29   2017-01-27 12:12:53 20.82244 -156.7056 station2
    30   2017-01-27 12:13:08 20.82628 -156.6616 station2
    31   2017-01-27 12:18:52 20.83721 -156.7394 station2
    32   2017-01-27 12:26:18 20.79902 -156.6977 station2
    33   2017-01-27 13:03:39 20.79930 -156.6084 station2
    34   2017-01-27 13:07:25 20.78338 -156.6148 station2
    35   2017-01-27 13:09:15 20.75798 -156.5954 station2
    36   2017-01-27 13:18:24 20.77019 -156.6830 station2
    37   2017-01-27 13:20:42 20.81733 -156.6440 station2
    38   2017-01-27 14:08:34 20.79685 -156.5975 station2
    39   2017-01-27 14:13:03 20.78625 -156.5941 station2
    40   2017-01-27 14:14:45 20.76980 -156.6095 station2
    41   2017-01-27 14:18:04 20.81734 -156.6634 station2
    42   2017-01-27 14:21:12 20.81628 -156.6588 station2
    43   2017-01-27 14:25:42 20.78261 -156.6199 station2
    44   2017-01-27 14:28:25 20.81209 -156.7029 station2
    45   2017-01-27 14:29:10 20.80157 -156.7335 station2
    46   2017-01-27 14:29:10 20.80157 -156.7335 station2", 
                         header = TRUE, stringsAsFactors = FALSE)

reprex package(v0.2.0)创建于2018-04-22。