Home > front end >  Proportion of locations per day
Proportion of locations per day

Time:12-08

I need to calculate the proportion of estimated locations considered to be area-restricted search (ARS) per day, in order to calculate the effect of bathymetry on these locations. How can I do it?

I was trying to do it this way:

AA_jub_day$YMD <- as.Date(AA_jub_day$YMD, "%Y,%m,%d")

AA_jub_day %>%
  group_by(YMD, States) %>%
  summarise(n = n()) %>%
  mutate(proportion = n / sum(n))

but I can't think of a way out.

Part of my data:

> dput(AA_jub_day)
structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L), levels = c("24641.05", "84485.18"), class = "factor"), 
    sex = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L), levels = "F", class = "factor"), bat = structure(list(
        lon = c(-25.3777, -25.39, -25.3903, -25.3882, -25.3781, 
        -25.3655, -25.357, -25.3464, -25.3405, -25.3146, -25.3453, 
        -25.1972, -25.3031, -25.3106, -25.3536, -25.3753, -25.4123, 
        -25.4445, -25.4745, -25.5087, -25.5394, -25.5558, -25.5853, 
        -25.6008, -25.6122, -25.6245, -25.6513, -25.6137, -25.6156, 
        -25.6111, -25.6002, -25.579, -25.5574, -25.5209, -25.4856, 
        -25.4473, -25.4045, -25.3689, -25.323, -25.2882, -25.2507, 
        -25.2277, -25.1884, -25.1536, -25.1088, -25.0734, -25.0399, 
        -25.0107, -24.9833, -24.9502, -24.925, -24.895, -24.8798, 
        -24.869, -24.8586, -24.8365, -24.8082, -24.7842, -24.7491, 
        -24.7139, -24.6901, -24.6699, -24.6677, -24.6758, -24.6809, 
        -24.7007, -24.7127, -24.7281, -24.761, -24.7944, -24.8197, 
        -24.8468, -24.876, -24.8893, -24.9023, -24.9063, -24.9035, 
        -24.8963, -24.8839, -24.8736, -24.858, -24.8385, -24.8206, 
        -24.8102, -24.7996, -24.7906, -24.7723, -24.7761, -24.7776, 
        -24.7882, -24.7986, -24.8973, -24.8776, -24.9252, -24.9508, 
        -24.9791, -24.9981, -25.0204, -25.034, -25.0399, -25.057, 
        -25.0611, -25.0772, -25.0942, -25.1058, -25.1525, -25.1343, 
        -25.2867, -25.3316, -25.3101, -25.346, -25.339, -25.3323, 
        -25.3242, -25.3095, -25.2926, -25.2834, -25.2732, -25.2537, 
        -25.2532, -25.2497, -25.2408, -25.2273, -25.2171, -25.2059, 
        -25.2032, -25.2044, -25.2002, -25.2077, -25.215, -25.236, 
        -25.2596, -25.2841, -25.3168, -25.3434, -25.3588, -25.3768, 
        -25.3783, -25.384, -25.3825, -25.3862, -25.38, -25.3814, 
        -25.3797, -25.3735, -25.3682, -25.3697, -25.3554, -25.3608, 
        -25.3528, -25.3461, -25.3247, -25.2732, -25.1666, -25.001, 
        -24.7011, -24.5, -24.3071, -24.1333, -23.9702, -23.8077, 
        -23.6466, -23.492, -23.3301, -23.1621, -22.9882, -22.8111, 
        -22.6421, -22.4867, -22.3643, -22.2831, -22.2881, -32.4976, 
        -32.3018, -32.3006, -32.3196, -32.2, -32.0984, -32.1799, 
        -32.105, -32.1, -32.1914, -32.3485, -32.2756, -32.0612, 
        -32.2, -32.3142, -32.2925, -32.3052, -32.4001, -32.6027, 
        -32.6365, -32.5536, -32.6969, -32.6, -32.6391, -32.6043, 
        -32.6003, -32.6999, -32.7996, -32.796, -32.7745, -32.8, 
        -32.7959, -32.8, -32.8902, -32.8102, -32.8025, -32.9, 
        -32.9547, -32.8922, -32.9037, -33, -32.9976, -33.2185, 
        -33.2038, -33.1993, -33.1764, -33.4992, -33.7724, -33.8533, 
        -33.8165), lat = c(-51.10237, -51.26221, -51.40003, -51.52113, 
        -51.6407, -51.7335, -51.81135, -51.86744, -51.90831, 
        -51.90496, -51.95854, -51.86818, -51.91713, -51.91975, 
        -51.94013, -51.96147, -51.97386, -51.98679, -52.00712, 
        -52.01164, -52.01192, -52.00469, -51.98914, -51.97125, 
        -51.96493, -51.93765, -51.93972, -51.92433, -51.93924, 
        -51.93265, -51.93407, -51.92938, -51.92087, -51.92695, 
        -51.92436, -51.92642, -51.92709, -51.93443, -51.9387, 
        -51.94619, -51.96074, -51.9723, -51.97661, -51.97988, 
        -51.99178, -52.00543, -52.00891, -52.02929, -52.03479, 
        -52.04538, -52.04194, -52.05639, -52.083, -52.12501, 
        -52.17056, -52.22105, -52.2813, -52.35125, -52.42871, 
        -52.51073, -52.59679, -52.68098, -52.77137, -52.83009, 
        -52.8793, -52.91822, -52.94645, -52.95834, -52.96825, 
        -52.98495, -53.00797, -53.04348, -53.08647, -53.15579, 
        -53.23911, -53.34846, -53.46345, -53.58262, -53.71471, 
        -53.84055, -53.95568, -54.07277, -54.17033, -54.25261, 
        -54.33583, -54.40233, -54.47618, -54.55129, -54.62013, 
        -54.70346, -54.73378, -54.86469, -54.84629, -54.82948, 
        -54.81127, -54.77046, -54.73622, -54.70363, -54.66628, 
        -54.6441, -54.61579, -54.60819, -54.57326, -54.55675, 
        -54.53017, -54.50315, -54.50552, -54.37702, -54.63709, 
        -54.55168, -54.57639, -54.55191, -54.54964, -54.53068, 
        -54.53434, -54.52177, -54.51151, -54.50976, -54.49487, 
        -54.47239, -54.46204, -54.44922, -54.43296, -54.42153, 
        -54.41122, -54.38624, -54.36768, -54.34922, -54.33558, 
        -54.32353, -54.31257, -54.3136, -54.31156, -54.30638, 
        -54.29904, -54.30367, -54.30436, -54.31581, -54.31551, 
        -54.30532, -54.28682, -54.27965, -54.26185, -54.24197, 
        -54.2184, -54.20706, -54.19907, -54.21091, -54.23894, 
        -54.27463, -54.31654, -54.3661, -54.4227, -54.46113, 
        -54.49632, -54.48143, -54.46736, -54.44317, -54.40677, 
        -54.36131, -54.31327, -54.27372, -54.23395, -54.20004, 
        -54.16649, -54.13125, -54.08323, -54.02996, -53.97498, 
        -53.89665, -53.79555, -53.62964, -51.0732, -51.21091, 
        -51.36132, -51.43744, -51.40205, -51.44579, -51.44605, 
        -51.51899, -51.54554, -51.53881, -51.5233, -51.53217, 
        -51.57179, -51.47281, -51.50629, -51.53546, -51.54331, 
        -51.57086, -51.58298, -51.5249, -51.541, -51.55685, -51.53322, 
        -51.55736, -51.53553, -51.47042, -51.26269, -51.26486, 
        -51.40776, -51.30361, -51.40762, -51.37622, -51.38322, 
        -51.37248, -51.39924, -51.40906, -51.40928, -51.38056, 
        -51.43946, -51.40637, -51.41941, -51.3849, -51.12589, 
        -51.21625, -51.25356, -51.28712, -51.14904, -51.18806, 
        -51.24735, -51.17164), depth = c(-2490L, -2088L, -3104L, 
        -3045L, -3118L, -3246L, -3386L, -3386L, -3364L, -3442L, 
        -3462L, -3357L, -3442L, -3442L, -3364L, -3462L, -3216L, 
        -3216L, -2643L, -2812L, -2307L, -2131L, -2131L, -1943L, 
        -1943L, -1982L, -1982L, -1982L, -1982L, -1982L, -1982L, 
        -2126L, -2126L, -2527L, -2527L, -3062L, -3062L, -3364L, 
        -3442L, -3526L, -3523L, -3523L, -3477L, -3477L, -3370L, 
        -3370L, -3676L, -3676L, -3622L, -3622L, -3643L, -3643L, 
        -3781L, -3781L, -3793L, -4005L, -4395L, -4513L, -4162L, 
        -3732L, -3296L, -3214L, -3094L, -2617L, -2031L, -2031L, 
        -1937L, -1937L, -2452L, -2452L, -3492L, -3621L, -4029L, 
        -3974L, -4020L, -3768L, -3962L, -4124L, -4500L, -4568L, 
        -4624L, -4637L, -4542L, -4535L, -4542L, -4476L, -4429L, 
        -4471L, -4506L, -4423L, -4455L, -4379L, -4379L, -4379L, 
        -4426L, -4398L, -4369L, -4369L, -4446L, -4446L, -4446L, 
        -4507L, -4573L, -4573L, -4415L, -4417L, -4417L, -4529L, 
        -4573L, -4567L, -4566L, -4566L, -4566L, -4560L, -4560L, 
        -4560L, -4560L, -4560L, -4465L, -4384L, -4384L, -4384L, 
        -4384L, -4384L, -4384L, -4498L, -4498L, -4498L, -4672L, 
        -4672L, -4672L, -4672L, -4688L, -4688L, -4688L, -4688L, 
        -4688L, -4688L, -4688L, -4688L, -4688L, -4688L, -4777L, 
        -4777L, -4777L, -4817L, -4817L, -4777L, -4777L, -4777L, 
        -4688L, -4529L, -4468L, -4336L, -4420L, -4444L, -4591L, 
        -4539L, -4595L, -4451L, -4507L, -4600L, -4414L, -3862L, 
        -3145L, -4371L, -5015L, -4305L, -4280L, -4565L, -4439L, 
        -3924L, -4182L, -3738L, -3523L, -3404L, -3536L, -3472L, 
        -3386L, -3207L, -2940L, -3064L, -3336L, -3165L, -3232L, 
        -3386L, -3165L, -3165L, -2823L, -3071L, -3470L, -3673L, 
        -3617L, -3424L, -3617L, -3470L, -3673L, -3683L, -3930L, 
        -3955L, -3742L, -3869L, -3742L, -3827L, -3827L, -3448L, 
        -3677L, -3555L, -3115L, -3207L, -3115L, -3448L, -2762L, 
        -3207L, -3722L, -2330L, -3002L, -2427L, -2435L, -2834L, 
        -2793L, -2686L)), row.names = c(1473L, 1474L, 1475L, 
    1476L, 1477L, 1478L, 1479L, 1480L, 1481L, 1482L, 1483L, 1484L, 
    1485L, 1486L, 1487L, 1488L, 1489L, 1490L, 1491L, 1492L, 1493L, 
    1494L, 1495L, 1496L, 1497L, 1498L, 1499L, 1500L, 1501L, 1502L, 
    1503L, 1504L, 1505L, 1506L, 1507L, 1508L, 1509L, 1510L, 1511L, 
    1512L, 1513L, 1514L, 1515L, 1516L, 1517L, 1518L, 1519L, 1520L, 
    1521L, 1522L, 1523L, 1524L, 1525L, 1526L, 1527L, 1528L, 1529L, 
    1530L, 1531L, 1532L, 1533L, 1534L, 1535L, 1536L, 1537L, 1538L, 
    1539L, 1540L, 1541L, 1542L, 1543L, 1544L, 1545L, 1546L, 1547L, 
    1548L, 1549L, 1550L, 1551L, 1552L, 1553L, 1554L, 1555L, 1556L, 
    1557L, 1558L, 1559L, 1560L, 1561L, 1562L, 1563L, 1564L, 1565L, 
    1566L, 1567L, 1568L, 1569L, 1570L, 1571L, 1572L, 1573L, 1574L, 
    1575L, 1576L, 1577L, 1578L, 1579L, 1580L, 1581L, 1582L, 1583L, 
    1584L, 1585L, 1586L, 1587L, 1588L, 1589L, 1590L, 1591L, 1592L, 
    1593L, 1594L, 1595L, 1596L, 1597L, 1598L, 1599L, 1600L, 1601L, 
    1602L, 1603L, 1604L, 1605L, 1606L, 1607L, 1608L, 1609L, 1610L, 
    1611L, 1612L, 1613L, 1614L, 1615L, 1616L, 1617L, 1618L, 1619L, 
    1620L, 1621L, 1622L, 1623L, 1624L, 1625L, 1626L, 1627L, 1628L, 
    1629L, 1630L, 1631L, 1632L, 1633L, 1634L, 1635L, 1636L, 1637L, 
    1638L, 1639L, 1640L, 1641L, 1642L, 1643L, 1644L, 6417L, 6418L, 
    6419L, 6420L, 6421L, 6422L, 6423L, 6424L, 6425L, 6426L, 6427L, 
    6428L, 6429L, 6430L, 6431L, 6432L, 6433L, 6434L, 6435L, 6436L, 
    6437L, 6438L, 6439L, 6440L, 6441L, 6442L, 6443L, 6444L, 6445L, 
    6446L, 6447L, 6448L, 6449L, 6450L, 6451L, 6452L, 6453L, 6454L, 
    6455L, 6456L, 6457L, 6458L, 6459L, 6460L, 6461L, 6462L, 6463L, 
    6464L, 6465L, 6466L), class = "data.frame"), States = c("TRANS", 
    "TRANS", "TRANS", "IND", "IND", "IND", "IND", "IND", "IND", 
    "IND", "IND", "IND", "IND", "ARS", "ARS", "ARS", "ARS", "ARS", 
    "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", 
    "ARS", "ARS", "IND", "IND", "IND", "IND", "IND", "IND", "IND", 
    "IND", "IND", "IND", "IND", "IND", "IND", "IND", "IND", "IND", 
    "IND", "IND", "IND", "IND", "IND", "IND", "IND", "IND", "IND", 
    "IND", "IND", "IND", "IND", "IND", "IND", "IND", "IND", "IND", 
    "IND", "IND", "IND", "IND", "IND", "IND", "IND", "IND", "IND", 
    "IND", "TRANS", "TRANS", "TRANS", "TRANS", "TRANS", "IND", 
    "IND", "IND", "IND", "IND", "IND", "IND", "IND", "IND", "IND", 
    "IND", "IND", "IND", "IND", "IND", "IND", "IND", "IND", "IND", 
    "IND", "IND", "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", 
    "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", 
    "IND", "IND", "IND", "IND", "IND", "IND", "IND", "IND", "IND", 
    "IND", "IND", "IND", "IND", "IND", "IND", "IND", "IND", "IND", 
    "IND", "IND", "IND", "IND", "IND", "IND", "IND", "IND", "IND", 
    "IND", "IND", "IND", "IND", "IND", "IND", "IND", "IND", "IND", 
    "IND", "IND", "TRANS", "TRANS", "TRANS", "TRANS", "TRANS", 
    "TRANS", "TRANS", "TRANS", "TRANS", "TRANS", "TRANS", "TRANS", 
    "TRANS", "TRANS", "TRANS", "TRANS", "TRANS", "TRANS", "IND", 
    "TRANS", "IND", "IND", "IND", "IND", "IND", "IND", "IND", 
    "IND", "IND", "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", 
    "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", 
    "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", 
    "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", "ARS", 
    "ARS", "ARS", "IND", "IND", "IND", "IND"), YMD = structure(c(13144, 
    13144, 13145, 13145, 13145, 13145, 13146, 13146, 13146, 13146, 
    13147, 13147, 13147, 13147, 13148, 13148, 13148, 13148, 13149, 
    13149, 13149, 13149, 13150, 13150, 13150, 13150, 13151, 13151, 
    13151, 13151, 13152, 13152, 13152, 13152, 13153, 13153, 13153, 
    13153, 13154, 13154, 13154, 13154, 13155, 13155, 13155, 13155, 
    13156, 13156, 13156, 13156, 13157, 13157, 13157, 13157, 13158, 
    13158, 13158, 13158, 13159, 13159, 13159, 13159, 13160, 13160, 
    13160, 13160, 13161, 13161, 13161, 13161, 13162, 13162, 13162, 
    13162, 13163, 13163, 13163, 13163, 13164, 13164, 13164, 13164, 
    13165, 13165, 13165, 13165, 13166, 13166, 13166, 13166, 13167, 
    13167, 13167, 13167, 13168, 13168, 13168, 13168, 13169, 13169, 
    13169, 13169, 13170, 13170, 13170, 13170, 13171, 13171, 13171, 
    13171, 13172, 13172, 13172, 13172, 13173, 13173, 13173, 13173, 
    13174, 13174, 13174, 13174, 13175, 13175, 13175, 13175, 13176, 
    13176, 13176, 13176, 13177, 13177, 13177, 13177, 13178, 13178, 
    13178, 13178, 13179, 13179, 13179, 13179, 13180, 13180, 13180, 
    13180, 13181, 13181, 13181, 13181, 13182, 13182, 13182, 13182, 
    13183, 13183, 13183, 13183, 13184, 13184, 13184, 13184, 13185, 
    13185, 13185, 13185, 13186, 13186, 13186, 13186, 13187, 13187, 
    17907, 17908, 17908, 17908, 17908, 17909, 17909, 17909, 17909, 
    17910, 17910, 17910, 17910, 17911, 17911, 17911, 17911, 17912, 
    17912, 17912, 17912, 17913, 17913, 17913, 17913, 17914, 17914, 
    17914, 17914, 17915, 17915, 17915, 17915, 17916, 17916, 17916, 
    17916, 17917, 17917, 17917, 17917, 17918, 17918, 17918, 17918, 
    17919, 17919, 17919, 17919, 17920), class = "Date")), class = "data.frame", row.names = c("13981", 
"13982", "13983", "13984", "13985", "13986", "13987", "13988", 
"13989", "13990", "13991", "13992", "13993", "13994", "13995", 
"13996", "13997", "13998", "13999", "14000", "14001", "14002", 
"14003", "14004", "14005", "14006", "14007", "14008", "14009", 
"14010", "14011", "14012", "14013", "14014", "14015", "14016", 
"14017", "14018", "14019", "14020", "14021", "14022", "14023", 
"14024", "14025", "14026", "14027", "14028", "14029", "14030", 
"14031", "14032", "14033", "14034", "14035", "14036", "14037", 
"14038", "14039", "14040", "14041", "14042", "14043", "14044", 
"14045", "14046", "14047", "14048", "14049", "14050", "14051", 
"14052", "14053", "14054", "14055", "14056", "14057", "14058", 
"14059", "14060", "14061", "14062", "14063", "14064", "14065", 
"14066", "14067", "14068", "14069", "14070", "14071", "14072", 
"14073", "14074", "14075", "14076", "14077", "14078", "14079", 
"14080", "14081", "14082", "14083", "14084", "14085", "14086", 
"14087", "14088", "14089", "14090", "14091", "14092", "14093", 
"14094", "14095", "14096", "14097", "14098", "14099", "14100", 
"14101", "14102", "14103", "14104", "14105", "14106", "14107", 
"14108", "14109", "14110", "14111", "14112", "14113", "14114", 
"14115", "14116", "14117", "14118", "14119", "14120", "14121", 
"14122", "14123", "14124", "14125", "14126", "14127", "14128", 
"14129", "14130", "14131", "14132", "14133", "14134", "14135", 
"14136", "14137", "14138", "14139", "14140", "14141", "14142", 
"14143", "14144", "14145", "14146", "14147", "14148", "14149", 
"14150", "14151", "14152", "19261", "19262", "19263", "19264", 
"19265", "19266", "19267", "19268", "19269", "19270", "19271", 
"19272", "19273", "19274", "19275", "19276", "19277", "19278", 
"19279", "19280", "19281", "19282", "19283", "19284", "19285", 
"19286", "19287", "19288", "19289", "19290", "19291", "19292", 
"19293", "19294", "19295", "19296", "19297", "19298", "19299", 
"19300", "19301", "19302", "19303", "19304", "19305", "19306", 
"19307", "19308", "19309", "19310"))

In the end I want to generate a plot like this enter image description here

CodePudding user response:

When you said you wanted to "calculate the effect of bathymetry", I assumed that you were referring to the variable bat$depth. I don't see anything immediately wrong with your original code, you just needed to add bat$depth to your group_by argument. You can also add a month variable (done using lubridate) and use other visualization packages to recreate the plot you included as an example; I used a combination of cowplot and patchwork to get a plot that combine the geom_smooth() line and the colorful dots that indicate month.

library(tidyverse)
library(lubridate)
library(cowplot)
library(patchwork)

df <- df %>%
  mutate(YMD = as.Date(df$YMD, "%Y,%m,%d")) %>%
  group_by(YMD, States, bat$depth) %>%
  summarise(n = n()) %>%
  mutate(proportion = n / sum(n)) %>%
  mutate(month = month(YMD, label = TRUE, abbr = FALSE))

p1 <- ggplot(df, aes(x = `bat$depth`, y = proportion))  
  geom_smooth(color = "black")  
  theme_bw()   
  theme(plot.margin = unit(c(0,0,0,0), "cm"))
 
p2 <- ggplot(df, aes(x = `bat$depth`, y = month))  
  geom_point(aes(color = month), shape = 15)  
  coord_fixed(ratio = 150)  
  xlab("")   ylab("")  
  theme_bw()   
  theme(legend.title=element_blank())  
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank())  
  theme(axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank())  
  theme(plot.margin = unit(c(0,0,0,0), "cm"))   
  theme(legend.position = "right")   
  theme(legend.key.size = unit(0.01, "cm"),
        legend.margin = margin(0,0,0,0))

getleg <- get_legend(p2)
p2 <- p2   theme(legend.position = "none")
p3 <- p2/p1   theme(aspect.ratio = 1/2)
final <- p3   inset_element(getleg, .01, .01, .2, .2)
final

enter image description here

  • Related