stat_density_2d errors when trying to use with facet_wrap

840 views Asked by At

I'm trying to plot a variable (rd2200w) vs elevation (Z). I'm also trying to overlay and fill density contours. This works great for the whole dataset. When I try to facet by another variable I run into errors. I've read of similar issues here (https://github.com/tidyverse/ggplot2/issues/3875), and am of the understanding the issue may be that the statistic is calculated on the whole dataset and then cannot be drawn on the subsets that each panel reflects? I clearly don't fully understand and am hoping someone can help with a facet wrap solution?

My dataset is posted below (sorry for the length, I don't know how else to do it!?)

Z   rd2200w Alteration_Domain
2086.42324  2212.87 a
2086.42324  2213.02 a
2069.478712 2211.34 a
2069.478712 2213.16 a
2069.478712 2214.1  a
2052.696713 2214.36 c
2052.696713 2212.73 c
2052.696713 2215.48 c
2036.87584  2215.05 c
2036.87584  2217.96 c
2036.87584  2215.11 c
2021.472239 2214.68 c
2021.472239 2213.25 c
2021.472239 2215.17 c
2021.472239 2214.33 c
2006.998792 2210.31 d
2006.998792 2210.06 d
2006.998792 2208.45 d
2006.030051 2207.68 d
2006.030051 2208.5  d
2006.030051 2207.5  d
2000.944162 2211.52 d
2000.944162 2211.63 d
2000.944162 2211.62 d
2000.352154 2210.97 d
2000.352154 2211.01 d
2000.352154 2211.14 d
1999.933135 2209.05 d
1999.933135 2209.02 d
1999.933135 2209.15 d
1999.798588 2211.63 d
1999.798588 2209.96 d
1999.798588 2208.89 d
1994.324434 2207.89 d
1994.324434 2208.89 d
1994.324434 2209.21 d
2036.366055 2212.26 c
2036.366055 2215.62 c
2036.366055 2214.66 c
1970.103275 2215.93 c
1970.103275 2212.93 c
1970.103275 2216.18 c
1846.501458 2214.54 d
1846.501458 2213.88 d
1846.501458 2212.54 d
1850.226434 2208.72 d
1850.226434 2207.37 d
1850.226434 2208.63 d
1846.212016 2209.39 d
1846.212016 2209.19 d
1846.212016 2210.82 d
1836.310846 2214.16 c
1836.310846 2208.71 c
1827.806748 2214.69 c
1827.806748 2216.12 c
1827.806748 2216.26 c
1819.311479 2219.91 c
1819.311479 2211.56 c
1819.311479 2212.59 c
1852.636502 2208.66 d
1852.636502 2208.29 d
1852.636502 2207.75 d
1861.854882 2215.05 d
1861.854882 2215.66 d
1861.854882 2215.57 d
1878.248559 2212.3  d
1878.248559 2208.61 d
1878.248559 2210.25 d
1887.487585 2205.72 d
1887.487585 2207.14 d
1887.487585 2206.97 d
1896.071566 2216.15 c
1896.071566 2214.04 c
1896.071566 2216.47 c
1896.071566 2213.62 c
1896.071566 2214.87 c
1896.071566 2214.57 c
1904.678364 2214.1  c
1904.678364 2215.39 c
1904.678364 2216.37 c
1904.678364 2215.36 c
1904.678364 2214.81 c
1904.678364 2215.51 c
1770.292975 2209.43 c
1770.292975 2211.98 c
1770.292975 2215.52 c
1753.475712 2224.34 e
1736.676562 2214.12 e
1736.676562 2214.2  e
1736.676562 2214.33 e
1736.676562 2214.37 e
1692.016018 2215.09 e
1692.016018 2214.46 e
1692.016018 2215.17 e
1678.038773 2209.9  e
1678.038773 2211.71 e
1678.038773 2210.08 e
1678.038773 2212.31 e
1678.038773 2211.33 e
1678.038773 2212.26 e
1658.580255 2202.69 e
1658.580255 2206.77 e
1658.580255 2207.25 e
1641.630159 2200.5  e
1641.630159 2204.86 e
1641.630159 2202.75 e
1622.333283 2205.65 e
1622.333283 2207.01 e
1622.333283 2204.37 e
1605.654075 2204.22 e
1605.654075 2206.97 e
1605.654075 2206.53 e
1605.654075 2198.82 e
1605.654075 2198.25 e
1605.654075 2218.21 e
1586.267349 2202.4  e
1586.267349 2202.33 e
1586.267349 2206.22 e
1524.883238 2202.07 e
1524.883238 2207.86 e
1524.883238 2202.37 e
2044.450809 2215.64 c
2044.450809 2208.13 c
2044.450809 2213.49 c
1954.622099 2214.4  c
1954.622099 2214.74 c
1954.622099 2214.42 c
1567.485924 2204.66 e
1567.485924 2204.38 e
1567.485924 2202.21 e
1553.802898 2201.77 f
1553.802898 2203.27 f
1553.802898 2204.54 f
1553.802898 2193.04 f
1553.802898 2199.35 f
1552.260692 2218.06 f
1552.260692 2217.63 f
1552.260692 2218.8  f
1552.260692 2214.99 f
1552.260692 2214.39 f
1552.260692 2215.4  f
1552.260692 2200.37 f
1552.260692 2196.59 f
1552.260692 2199.83 f
1550.349482 2213.47 e
1551.359072 2214.81 e
1551.359072 2215.15 e
1551.359072 2213.87 e
1547.31312  2203.7  e
1547.31312  2204.52 e
1547.31312  2206.07 e
1544.679434 2206.42 f
1544.679434 2207    f
1544.679434 2207.31 f
1543.891891 2207.19 f
1543.891891 2206.25 f
1543.891891 2207.83 f
1543.549917 2204.65 f
1543.549917 2205.54 f
1543.549917 2207.8  f
1543.297954 2201.11 f
1543.297954 2196.27 f
1543.297954 2198.84 f
1542.204783 2208.69 e
1542.204783 2207.49 e
1542.204783 2209.14 e
1541.29177  2207.13 f
1541.29177  2206.72 f
1541.29177  2206.4  f
1541.29177  2205.94 f
1541.29177  2206.83 f
1541.29177  2205.41 f
1539.129195 2202.89 f
1539.129195 2199.09 f
1539.129195 2200.16 f
1716.161148 2215.57 c
1716.161148 2212.06 c
1716.161148 2212.41 c
1698.280996 2193.41 e
1698.280996 2214.33 e
1680.110242 2203.45 e
1680.110242 2207.21 e
1680.110242 2215.54 e
1661.077303 2213.5  e
1661.077303 2221.35 e
1651.471116 2208.24 e
1651.471116 2213.97 e
1651.471116 2201.88 e
1651.471116 2207.92 e
1634.979634 2205.24 e
1634.979634 2212.06 e
1634.979634 2212.91 e
1614.816289 2207.17 e
1614.816289 2194.55 e
1614.816289 2205.04 e
1599.061183 2207.7  e
1599.061183 2207.82 e
1599.061183 2206.65 e
1580.994972 2197.91 e
1580.994972 2216.6  e
1564.253712 2207.63 e
1564.253712 2208.14 e
1564.253712 2205.59 e
1826.557925 2208.94 c
1826.557925 2209.58 c
1826.557925 2208.88 c
2078.806219 2210.19 a
2078.806219 2215.22 a
2078.806219 2210.89 a
2078.806219 2209.93 a
2078.806219 2212.08 a
2078.806219 2211.17 a
2002.605431 2212.7  c
2002.605431 2212.7  c
2002.605431 2208.08 c
1915.708709 2208.38 c
1915.708709 2208.29 c
1915.708709 2208.44 c
1433.320612 2209.03 f
1433.320612 2204.06 f
1432.979881 2214.44 f
1432.979881 2200.42 f
1432.979881 2216.36 f
1432.979881 2199.55 f
1429.332222 2207.74 f
1429.332222 2207.47 f
1429.332222 2207.49 f
1429.332222 2207.66 f
1428.412952 2209.12 e
1428.412952 2209.02 e
1427.078871 2202.98 f
1427.078871 2200.56 f
1427.078871 2200.05 f
1427.078871 2200.36 f
1427.078871 2197.93 f
1426.7392   2200.79 f
1426.7392   2199.61 f
1426.7392   2200.46 f
1426.244474 2206.39 f
1426.244474 2207.22 f
1426.244474 2207.58 e
1425.251198 2198.55 e
1425.251198 2200.76 e
1425.251198 2198.95 e
1422.512816 2205.66 e
1422.512816 2206.19 e
1422.512816 2205.36 e
1414.590981 2200.19 e
1414.590981 2200.99 e
1414.590981 2202.1  e
1436.406655 2201.95 e
1436.406655 2205.39 e
1436.406655 2205.07 e
1470.407999 2205.14 e
1470.407999 2200.99 e
1470.407999 2205.74 e
1487.529851 2204.55 e
1487.529851 2204.42 e
1487.529851 2203.47 e
1521.842924 2200.06 e
1521.842924 2208.35 e
1521.842924 2205.62 e
1539.867238 2204.95 e
1539.867238 2205.06 e
1539.867238 2206.13 e
1548.420466 2207.26 e
1548.420466 2208.15 e
1548.420466 2206.39 e
1281.453083 2216.41 e
1281.453083 2208.91 e
1281.453083 2214.07 e
1990.521576 2211.05 c
1990.521576 2210.03 c
1990.521576 2209.88 c
2006.799967 2214.74 c
2006.799967 2212.98 c
2006.799967 2217.04 c
2006.799967 2215.49 c
2006.799967 2216.19 c
2006.799967 2214.57 c
2023.649534 2207.98 c
2023.649534 2208.52 c
2023.649534 2210.61 c
2023.649534 2210.64 c
2023.649534 2209.8  c
2041.512766 2213.35 b
2041.512766 2213.44 b
2041.512766 2213.51 b
2058.450516 2214.36 a
2058.450516 2214.16 a
2058.450516 2214.13 a
1958.054076 2213.82 c
1958.054076 2215.22 c
1958.054076 2215.23 c
1958.054076 2214.85 c
1958.054076 2217.13 c
1958.054076 2214.17 c
2090.54446  2207.8  b
2082.399869 2212.65 a
2082.399869 2213.09 a
2082.399869 2212.86 a
2081.167137 2213.15 a
2081.167137 2213.77 a
2081.167137 2213.24 a
2075.849801 2215.13 a
2075.849801 2214.33 a
2075.849801 2215.48 a
2024.94658  2216.34 c
2024.94658  2216.92 c
2024.94658  2215.11 c
2007.333387 2210.97 c
2007.333387 2210.07 c
2007.333387 2210.92 c
1991.675574 2214.91 c
1991.675574 2213.79 c
1991.675574 2214.2  c
1991.675574 2215.96 c
1991.675574 2217.99 c
1991.675574 2218.18 c
1972.848345 2214.21 c
1972.848345 2215.01 c
1972.848345 2214.96 c
1906.36169  2221.72 c
1906.36169  2221.95 c
1906.36169  2212.99 c
2075.592427 2212.9  a
2075.592427 2213.14 a
2075.592427 2212.98 a
2059.91593  2213.1  c
2059.91593  2212.83 c
2059.91593  2214.16 c
2042.231806 2213.9  c
2042.231806 2214.08 c
2042.231806 2213.8  c
1974.09126  2212.95 c
1974.09126  2211.65 c
1974.09126  2211.69 c
1956.487613 2217.53 c
1956.487613 2213.54 c
1956.487613 2212.9  c
1956.487613 2211.17 c
1956.487613 2210.28 c
1956.487613 2210.67 c
1949.289203 2215.77 c
1949.289203 2217.49 c
1949.289203 2214.14 c
1939.619145 2210.88 d
1939.619145 2210.02 d
1939.619145 2210.7  d
1935.600964 2208.49 d
1935.600964 2209.42 d
1935.600964 2209.36 d
1931.499535 2209.03 d
1931.499535 2210.11 d
1931.499535 2207.79 d
1931.342475 2209.39 d
1931.342475 2210.06 d
1931.342475 2208.87 d
1930.561456 2209.49 c
1930.561456 2210.09 c
1930.561456 2209.6  c
1922.62739  2213.81 c
1922.62739  2217.36 c
1922.62739  2218.86 c
1915.376441 2212.43 c
1915.376441 2214.83 c
1915.376441 2211.07 c
1926.047366 2214.06 c
1926.047366 2216.09 c
1926.047366 2215.99 c
1907.046192 2208.04 c
1888.039238 2216.1  c
1888.039238 2221.34 c
1888.039238 2211.39 c
1870.995724 2212.77 c
1870.995724 2215.04 c
1870.995724 2221.4  c
1802.301141 2204.58 e
1802.301141 2196.49 e
1802.301141 2204.3  e
1802.301141 2214.38 e
1802.301141 2204.73 e
1802.301141 2205.14 e
1783.093429 2214.02 e
1768.422942 2208.3  e
1768.422942 2215.48 e
1748.689796 2214.92 e
1748.689796 2206.65 e
1737.14726  2207.98 e
1737.14726  2212.26 e
1737.14726  2211.62 e
1751.42176  2205.95 e
1751.42176  2207.32 e
1751.42176  2206.67 e
1854.018233 2212.16 c
1854.018233 2210.95 c
1854.018233 2213.44 c
1850.523689 2208.11 c
1850.523689 2208.99 c
1850.523689 2208.47 c
1849.50749  2214.8  d
1849.50749  2215.69 d
1849.50749  2212.25 d
1849.232748 2212.16 d
1849.232748 2217.12 d
1849.232748 2213.86 d
1848.330099 2216.97 d
1848.330099 2212.04 d
1847.989999 2214.57 d
1847.989999 2216.7  d
1847.989999 2218.15 d
1847.418842 2208.86 d
1847.418842 2207.6  d
1847.418842 2208.68 d
1843.509153 2211.66 c
1843.509153 2210.04 c
1838.417083 2214.74 c
1838.417083 2210.38 c
1838.417083 2214.08 c
1836.980277 2215.7  d
1836.980277 2207.49 d
1836.980277 2214.46 d
1835.948565 2217.59 d
1835.948565 2210.66 d
1835.948565 2215.06 d
1835.195552 2215.03 d
1835.195552 2210.24 d
1835.195552 2212.88 d
1830.722615 2214.91 c
1830.722615 2213.79 c
1830.722615 2211.48 c
2069.843563 2214.25 a
2069.843563 2213.85 a
2069.843563 2212.85 a
2016.050359 2213.37 c
2016.050359 2213.07 c
2016.050359 2214.64 c
1998.668644 2209.15 c
1998.668644 2208.74 c
1998.668644 2209.13 c
1983.667382 2215.03 c
1983.667382 2215.87 c
1983.667382 2213.73 c
1967.456487 2213.87 c
1967.456487 2214.75 c
1967.456487 2212.67 c
1904.820267 2207.64 c
1904.820267 2208.9  c
1873.68313  2213.58 c
1873.68313  2211.84 c
1873.68313  2211.19 c
2049.023764 2212.58 a
2049.023764 2213.16 a
2049.023764 2211.92 a
2043.757236 2209.72 b
2043.757236 2209.76 b
2043.757236 2209.74 b
2042.43493  2208.75 b
2042.43493  2208.8  b
2041.707688 2214.55 b
2041.707688 2214.52 b
2041.707688 2214.55 b
2041.400464 2212.21 b
2041.400464 2212    b
2041.400464 2212.12 b
2040.424368 2215.36 b
2040.424368 2215.11 b
2040.424368 2215.47 b
2038.814469 2208.74 c
2038.814469 2209.24 c
2038.814469 2210.66 c
2038.814469 2209.08 c
2038.814469 2209.3  c
2038.814469 2209.32 c
2030.906327 2214.57 c
2030.906327 2216.94 c
2030.906327 2210.73 c
2030.906327 2210.05 c
2030.906327 2212.69 c
2030.906327 2209.94 c
1952.346787 2208.49 c
1952.346787 2209.02 c
1952.346787 2208.53 c
1950.374811 2212.78 c
1950.374811 2213.6  c
1950.374811 2212.97 c
1947.947718 2216.37 d
1947.947718 2214.44 d
1947.947718 2212.99 d
1947.947718 2207.48 d
1947.947718 2208.37 d
1947.947718 2208.49 d
1947.189868 2204.77 d
1947.189868 2212.23 d
1947.189868 2214.43 d
1947.017102 2209.53 d
1947.017102 2208.11 d
1947.017102 2208.66 d
1947.017102 2209.47 d
1947.017102 2214.91 d
1947.017102 2213.72 d
1946.812929 2212.32 d
1946.812929 2216.47 d
1946.812929 2212.33 d
1945.37991  2210.6  d
1945.37991  2210.49 d
1945.37991  2209.37 d
1938.253248 2193.53 c
1938.253248 2223.03 c
1938.253248 2218.65 c
1938.253248 2216.75 c
1929.883681 2208.76 c
1929.883681 2209.2  c
1929.883681 2208.71 c
1929.883681 2214.29 c
1929.883681 2212.43 c
1929.883681 2216.1  c
1864.002651 2211.79 c
1864.002651 2217.61 c
1864.002651 2213.03 c
1847.197268 2217.06 c
1847.197268 2217.39 c
1847.197268 2219.79 c
1830.357006 2222.58 c
1830.357006 2218.49 c
1830.357006 2216.12 c
1812.898532 2205.18 c
1812.898532 2205.26 c
1812.898532 2204.95 c
1749.430946 2207.57 e
1749.430946 2207.53 e
1749.430946 2207.36 e
1749.430946 2211.16 e
1749.430946 2209.51 e
1749.430946 2208.85 e
1735.074729 2209.3  e
1735.074729 2211.94 e
1735.074729 2209.44 e
1716.544599 2207.49 e
1716.544599 2206.48 e
1716.544599 2204.47 e
1716.544599 2207.16 e
1716.544599 2206.24 e
1716.544599 2205.98 e
1699.474015 2205.31 e
1699.474015 2203.09 e
1699.474015 2205.86 e
1682.46826  2208.8  e
1682.46826  2209.29 e
1682.46826  2208.77 e
1795.936141 2211.44 c
1795.936141 2210.3  c
1795.936141 2218.7  c
1788.663215 2208.24 e
1788.663215 2204.71 e
1788.663215 2209.33 e
1784.802085 2209.41 e
1784.802085 2202.88 e
1784.802085 2211.46 e
1784.802085 2218.17 e
1784.802085 2205.62 e
1784.802085 2207    e
1781.594307 2208.31 e
1781.594307 2206.13 e
1780.871866 2219.97 f
1780.871866 2215.35 f
1780.871866 2217.74 f
1780.871866 2208.51 f
1780.871866 2207.12 f
1780.871866 2208.12 f
1781.404182 2209.4  e
1781.404182 2209.91 e
1781.404182 2212.22 e
1778.185732 2212.69 e
1778.185732 2214.08 e
1778.185732 2216.05 e
1779.165427 2211.07 f
1779.165427 2210.2  f
1779.165427 2209.28 f
1777.856391 2208.38 f
1777.856391 2209.05 f
1777.856391 2203.38 f
1776.404152 2206.35 e
1776.404152 2205.89 e
1776.404152 2208.87 e
1775.948296 2207.76 e
1775.948296 2207.78 e
1775.948296 2207.72 e
1767.513399 2207.25 e
1767.513399 2208.18 e
1767.513399 2207.25 e
2074.095482 2213.73 a
2074.095482 2213.88 a
2074.095482 2213.32 a
2056.083804 2211.53 a
2056.083804 2211.68 a
2056.083804 2211.58 a
2038.344756 2213.94 c
2038.344756 2213.74 c
2038.344756 2214.48 c
2021.932713 2212.28 c
2021.932713 2212.23 c
2021.932713 2212.67 c
2016.060062 2210.56 c
2016.060062 2212.3  c
2016.060062 2215.02 c
2012.707347 2209.73 d
2012.707347 2212.87 d
2012.707347 2212.17 d
2009.066258 2211.13 d
2009.066258 2211.08 d
2009.066258 2211.36 d
2001.676277 2214.78 d
2001.676277 2214.54 d
2001.676277 2214.34 d
2004.716891 2211.22 d
2004.716891 2210.99 d
2004.716891 2211.53 d
1999.261546 2207.99 c
1992.70531  2209.61 c
1992.70531  2209.54 c
1992.70531  2209.07 c
2000.272031 2210.03 d
2000.272031 2210.3  d
2000.272031 2210.1  d
2097.487206 2213.72 a
2097.487206 2214.08 a
2097.487206 2214.12 a
1968.252232 2210.31 c
1968.252232 2210.99 c
1968.252232 2210.79 c
1894.834055 2210.77 c
1894.834055 2209.91 c
1894.834055 2210.8  c
1894.834055 2214.24 c
1894.834055 2214.07 c
1894.834055 2213.09 c
2035.327734 2210.02 a
1902.456757 2208.28 c
1902.456757 2208.27 c
2087.11071  2211.88 b
2087.11071  2211.91 b
2087.11071  2211.83 b
2085.508544 2215.73 b
2085.508544 2216.14 b
2085.508544 2215.89 b
2084.044941 2216.18 b
2084.044941 2215.93 b
2084.044941 2215.99 b
2079.831668 2213.05 a
2079.831668 2213.33 a
2079.831668 2212.99 a

Code:

dat <- read_csv("10_Source files/dat.csv")
dat$Alteration_Domain <- factor(dat$Alteration_Domain, levels = c("a","b","c","d","e","f"))

ggplot(dat, mapping = aes(y = Z, x = rd2200w))+
                    stat_density_2d(aes(alpha = ..level..), geom = "polygon", show.legend = FALSE)+                 
                    stat_density_2d(geom = "contour", color = "black", alpha = .25, show.legend = FALSE)+
                    scale_alpha(range = c(0.05,0.2))+
                    geom_point(aes(color = Alteration_Domain, shape = Alteration_Domain), alpha =0.5, size = 3)+
                    scale_shape_manual(values=c(0,15,1,16,5,18))+
                    scale_color_manual(values=c("green4","green4","gray1","gray1","royalblue1","royalblue1"))+
                    #facet_wrap(~Alteration_Domain)+
                    labs(x="rd_rd2200w_wvl", y="Elevation",title = "Z vs rd2200w (AlOH); No Int from rdmin1group or rdmin2group or rdmin3group", hjust = .5)+
                    stat_quadrant_counts(quadrants = 0L, label.x = "right", 
                        aes(label = sprintf("%i observations", stat(count))))+
                    guides(colour = FALSE, alpha = FALSE)+
                    theme_bw(base_size = 20)

This produces the following plot which is great: enter image description here

When I uncomment out the facet_wrap line in the code above I get the below image which is appropriately faceted but with no contours:

enter image description here

And the following warnings:

enter image description here

I'm hoping to achieve the faceted version with contouring in the same style as the first image on every facet.

I'm stumbling through learning R for analysis of my data and would appreciate any help. Also, if there's a better way to post a data example, I'd be happy to.

Thanks for your time!

2

There are 2 answers

1
Allan Cameron On

In the latest version of ggplot there is the option to use geom_density_2d_filled. You can set the number of bins at which to break the contours, though the step size is fixed across groups or facets. Inevitably because of the disparity in number of points in each facet, the number of contour lines will vary so much that the high density plots look messy if the low density plots look good, and the low density plots look empty if the high density facets look good.

Personally, I would lose the contour lines and use just the alpha density fill. This would give the following plot:

library(ggplot2)
library(ggpmisc)

ggplot(dat, aes(y = Z, x = rd2200w)) +
  geom_density2d_filled(aes(alpha = as.numeric(..nlevel..),
                            fill  = Alteration_Domain), 
                        bins = 1000) +
  geom_point() +
  scale_alpha(range = c(0.05,0.5)) +
  scale_fill_manual(values = rep(c("green4", "gray1", "royalblue1"), each = 2)) +
  stat_quadrant_counts(quadrants = 0L, label.x = "right", 
                       aes(label = sprintf("%i observations", stat(count)))) +
  facet_wrap(~Alteration_Domain, scales = "free") +
  labs(x = "rd_rd2200w_wvl", y = "Elevation",
       title = paste("Z vs rd2200w (AlOH); No Int from rdmin1group or", 
                     "rdmin2group or rdmin3group", sep = "\n"), hjust = .5) +
  guides(colour = FALSE, alpha = FALSE) +
  theme_bw(base_size = 20)

enter image description here

Which I think is just as informative but easier on the eye.

0
Rob On

It seems like this isn't the correct application of facet_wrap, so I've made the plot I'm after by making a loop that splits the datasets and makes each plot individually and then I've combined them into a grid plot at the end.

dat <- read_csv("10_Source files/dat.csv")
dat$Alteration_Domain <- factor(dat$Alteration_Domain, levels = 
c("a","b","c","d","e","f"))

altdom <- levels(dat$Alteration_Domain)
shape <- c(0,15,1,16,5,18)
color <- c("green4","green4","gray1","gray1","royalblue1","royalblue1")

L <- list()

for(i in seq_along(altdom)){

dat1 <- dat %>%
filter(Alteration_Domain == altdom[i])

xmin <- min(dat1$rd2200w) ; xmax <- max(dat1$rd2200w)
ymin <- min(dat1$Z) ; ymax <- max(dat1$Z)
    
L[[i]] <- ggplot(dat1, mapping = aes(y = Z, x = rd2200w))+
                    stat_density_2d(aes(alpha = ..level..), geom = "polygon", show.legend = FALSE)+                 
                    stat_density_2d(geom = "contour", color = "black", alpha = .25, show.legend = FALSE)+
                    scale_alpha(range = c(0.05,0.2))+
                    geom_point(aes(color = Alteration_Domain, shape = Alteration_Domain), alpha =0.5, size = 3, show.legend = FALSE)+
                    scale_shape_manual(values= shape[i])+
                    scale_color_manual(values=color[i])+
                    labs(title = paste(altdom[i]))+
                    stat_quadrant_counts(quadrants = 0L, label.x = "right", 
                        aes(label = sprintf("%i observations", stat(count))))+
                    guides(colour = FALSE, alpha = FALSE)+
                    theme_bw(base_size = 20)+
                    theme(axis.title = element_blank(), plot.title = element_text(hjust = 0.5,size = 20))+
                    scale_x_continuous(limits = c(xmin-10,xmax+10)) + 
                    scale_y_continuous(limits = c(ymin-50,ymax+50)) +
                    coord_cartesian(xlim = c(xmin,xmax), ylim = c(ymin,ymax))
                    
                    }
                    
y.grob <- textGrob("Elevation", gp=gpar(fontsize=20), rot=90)
x.grob <- textGrob("rd_2200_wvl", gp=gpar(fontsize=20))
    
####    make plot grid
p7 <- plot_grid(L[[1]],L[[2]],L[[3]],L[[4]],L[[5]],L[[6]],align = "hv",nrow = 2, axis = "l")

grid.arrange(arrangeGrob(p7, left = y.grob, bottom = x.grob))

Which produces: enter image description here

This serves my purposes for now, thanks everyone for taking a look and helping!