如何在y = 0的上方和下方用不同的颜色填充geom_polygon? [英] How to fill geom_polygon with different colors above and below y = 0?

查看:31
本文介绍了如何在y = 0的上方和下方用不同的颜色填充geom_polygon?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下多边形图:

ggplot(df, aes(x=year,y=afw)) +geom_polygon() +scale_x_continuous("", expand=c(0,0),breaks=seq(1910,2010,10)) +主题_bw()

但是,我想用两种不同的颜色填充它.例如,0 上方的黑色区域为红色,0 下方的黑色区域为蓝色.不幸的是,使用 fill=col 不会填充正确的区域.

我尝试了以下代码(我添加了 geom_line 以说明填充的边框应该在哪里):

ggplot(df, aes(x=year,y=afw)) +geom_line() +geom_polygon(aes(fill=col), alpha=0.5) +scale_x_continuous("", expand=c(0,0),breaks=seq(1910,2010,10)) +主题_bw()

给出:

如您所见,它的填充量超出了预期.我该如何解决这个问题?

数据:

df <- structure(list(year = c(1901, 1901, 1901, 1902, 1903, 1904, 1905, 1906, 1907, 1908, 1909, 1910, 19121, 19, 19, 19)1915年,1916年,1917年,1918年,1919年,1920年,1921年,1922年,1923年,1924年,1925年,1926年,1927年,1928年,1929年,1930年,1931年,1932年,1933年,1934年,1935年,1936年,1937年,1938年,1939年,1940年,1941年,1942年,1943年,1944年,1945年,1946年,1947年,1948年,1949年,1950年,1951年,1952年,1953年,1954年,1955年,1956年,1957年,1958年,1959年,1960年,1961年,1962年,1963年,1964年,1965年,1966年,1967年,1968年,1969年,1970年,1971年,1972年,1973年,1974年,1975年,1976年,1977年,1978年,1979年,1980年,1981年,1982年,1983年,1984年,1985年,1986年,1987年,1988年,1989年,1990年,1991年,1992年,1993年,1994年,1995年,1996年,1997年,1998年,1999年,2000年,2001年,2002年,2003年,2004年,2005年,2006年,2007年,2008年,2009年,2010年,2011年,2012年,2013年,2013年,2013年),AFW = C(0,0,-0.246246074793035,-2.39463317156723,-2.39785897801884,0.840850699400514,-0.843020268341422,-3.02043962318013,-0.033342848986583,-2.04947188124465,-0.00431059092206709,2.49568940907793,1.96988295746503,2.26665715101342,0.986011989723095,1.79568940907793,2.06665715101342,-0.601084784470454,-3.21076220382529,2.65052811875535,0.46988295746503,-1.09140736511562,0.0505281187553526,1.41827005423922,-2.80108478447045,0.611818441335997,-1.83011704253497,-0.30753639737368,-4.43011704253497,-0.897858978018841,1.98601198972309,-0.965600913502712,0.0795603768198685,0.308592634884385, -5.33011704253497,4.00214102198116,-0.594633171567228,0.0698829574650297,-1.60753639737368,-2.81398801027691,-2.21398801027691,-2.4365686554382,1.53439908649729,1.06665715101342,-1.87205252640594,-0.688181558664002,0.0569797316585783,-3.51398801027691,0.979560376819868,0.289237796174707,1.24085069940051,-4.39140736511562,1.13117328004567,-1.72689123608336,2.20214102198116,2.27310876391664,1.46665715101342,2.18278618327148,-0.23011704253497,1.50536682843277,1.17633457036826,-0.0785041393091639,-1.54947188124465,-3.85269768769626,-4.31398801027691,-0.80753639737368,1.27956037681987,1.2376248929489,0.195689409077933,-3.38172994576078,-4.88172994576078,-0.675278332857551,2.25375392520697,0.0924636026263199,-0.446246074793035,4.06988295746503,0.350528118755352,-1.48172994576078,1.81504424778761,-1.42689123608336,2.22472166714245,0.376334570368256,-3.88495575221239,0.211818441335998,0.586011989723094,1.14407650585213,2.55697973165858,1.92794747359406,1.20214102198116,3.83439908649729,1.64407650585213,0.986011989723095,0.753753925206965,0.508592634884385,1.911818441336,2.11504424778761,-4.06560091350271,-2.58495575221239,1.80859263488438,1.37956037681987,1.58923779617471,1.88601198972309,-0.323665429631744,-0.291407365115615,0.818270054239223,0.0569797316585783,0.795689409077933,3.32472166714245,0.595689409077933,-0.733342848986583,-0.955923494147874, -4.32689123608336, 3.29891521552955, 1.85697973165858, 2.74407650585213, 0, 0), col = 结构(c(1L, 2L, 1 L, 1 L, 1 L, 2 L, 1 L, 1 L, 1 L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L,1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L,2L、2L、1L、2L、2L、1L、2L、1L、2L、2L、1L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、11L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L), .Label = c("B","A"), class = "factor")), .Names = c("year", "afw", "col"), class = c("tbl_df", "data.frame"), row.names =c(NA, -117L))

注意:正如您在数据中看到的,1901 年和 2013 年都有 3 行.我这样做是因为我想正确填充.虽然黑色填充是正确的,但我似乎没有得到带有颜色的可行解决方案.

原始数据集:

orig <- structure(list(year = c(1901, 1902, 1903, 1904, 1905, 1906, 1907, 1908, 1909, 1910, 1911, 1912, 1916, 1916, 1914, 1906)1917年,1918年,1919年,1920年,1921年,1922年,1923年,1924年,1925年,1926年,1927年,1928年,1929年,1930年,1931年,1932年,1933年,1934年,1935年,1936年,1937年,1938年,1939年,1940年,1941年,1942年,1943年,1944年,1945年,1946年,1947年,1948年,1949年,1950年,1951年,1952年,1953年,1954年,1955年,1956年,1957年,1958年,1959年,1960年,1961年,1962年,1963年,1964年,1965年,1966年,1967年,1968年,1969年,1970年,1971年,1972年,1973年,1974年,1975年,1976年,1977年,1978年,1979年,1980年,1981年,1982年,1983年,1984年,1985年,1986年,1987年,1988年,1989年,1990年,1991年,1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 0, 2, 1, 0, 2, 1, 0, 2, 0, 1, 0, 20.246246074793035,-2.39463317156723,-2.39785897801884,0.840850699400514,-0.843020268341422,-3.02043962318013,-0.033342848986583,-2.04947188124465,-0.00431059092206709,2.49568940907793,1.96988295746503,2.26665715101342,0.986011989723095,1.79568940907793,2.06665715101342,-0.601084784470454,-3.21076220382529,2.65052811875535,0.46988295746503,-1.09140736511562,0.0505281187553526,1.41827005423922,-2.80108478447045,0.611818441335997,-1.83011704253497,-0.30753639737368,-4.43011704253497,-0.897858978018841,1.98601198972309,-0.965600913502712,0.0795603768198685,0.308592634884385,-5.33011704253497,4.00214102198116,-0.594633171567228,0.0698829574650297,-1.60753639737368,-2.81398801027691,-2.21398801027691,-2.4365686554382,1.53439908649729,1.06665715101342,-1.87205252640594,-0.688181558664002,0.0569797316585783,-3.51398801027691,0.979560376819868,0.289237796174707,1.24085069940051,-4.39140736511562,1.13117328004567,-1.72689123608336,2.20214102198116,2.27310876391664,1.46665715101342,2.18278618327148,-0.23011704253497,1.50536682843277,1.17633457036826,-0.0785041393091639,-1.54947188124465,-3.85269768769626,-4.31398801027691,-0.80753639737368,1.27956037681987,1.2376248929489,0.195689409077933, -3.38172994576078,-4.88172994576078,-0.675278332857551,2.25375392520697,0.0924636026263199,-0.446246074793035,4.06988295746503,0.350528118755352,-1.48172994576078,1.81504424778761,-1.42689123608336,2.22472166714245,0.376334570368256,-3.88495575221239,0.211818441335998,0.586011989723094,1.14407650585213,2.55697973165858,1.92794747359406,1.20214102198116,3.83439908649729,1.64407650585213,0.986011989723095,0.753753925206965,0.508592634884385,1.911818441336,2.11504424778761,-4.06560091350271,-2.58495575221239,1.80859263488438,1.37956037681987,1.58923779617471,1.88601198972309,-0.323665429631744,-0.291407365115615,0.818270054239223,0.0569797316585783,0.795689409077933,3.32472166714245,0.595689409077933,-0.733342848986583,-0.955923494147874,-4.32689123608336,3.29891521552955, 1.85697973165858, 2.74407650585213)), .Names = c("year", "afw"), class = c("tbl_df", "data.frame"), row.names = c(NA, -113L))

解决方案

获取两个连续时间步长的 y 值符号不同的索引.在这些点之间使用线性插值来生成新的 x 值,其中 y 为零.

首先,一个较小的示例,以便更容易地感受线性插值以及将哪些点添加到原始数据中:

#原始数据d <- data.frame(x = 1:6,y = c(-1, 2, 1, 2, -1, 1))# 强制到 data.table图书馆(数据表)设置DT(d)# 确保数据按 x 排序设置顺序(d,x)# 添加分组变量# 在这个例子中只跟踪原始点和插值点d[ , g := 原点"]# 插值d2 = d[ , {ix = .I[c(FALSE, abs(diff(sign(d$y))) == 2)]如果(长度(ix)){pred_x = sapply(ix, function(i) approx(x = y[c(i-1, i)], y = x[c(i-1, i)], xout = 0)$y)rbindlist(.(.SD, data.table(x = pred_x, y = 0, g = "new")))} else .SD}]d2# x y grp# 1 1.000000 -1 原点# 2 2.000000 2 原点# 3 3.000000 1 原件# 4 4.000000 2 原件# 5 5.000000 -1 原件# 6 6.000000 1 原件# 13 1.333333 0 新# 11 4.666667 0 新# 12 5.500000 0 新

用颜色区分原始点和新点的绘图:

ggplot(data = d2, aes(x = x, y = y)) +geom_area(data = d2[y <= 0], 填充 = 红色", alpha = 0.2) +geom_area(data = d2[y >= 0], 填充 = 蓝色", alpha = 0.2) +geom_point(aes(color = g), size = 4) +scale_color_manual(values = c("red", "black")) +主题_bw()


应用于 OP 的数据:

d = as.data.table(orig)# setorder(d, 年)d2 = d[ , {ix = .I[c(FALSE, abs(diff(sign(d$afw))) == 2)]如果(长度(ix)){pred_yr = sapply(ix, function(i) approx(afw[c(i-1, i)], year[c(i-1, i)], xout = 0)$y)rbindlist(.(.SD, data.table(year = pred_yr, afw = 0)))} else .SD}]ggplot(数据 = d2, aes(x = 年, y = afw)) +geom_area(数据= d2[afw <= 0],填充=红色")+geom_area(data = d2[afw >= 0], 填充 = 蓝色") +主题_bw()


回复


对于改编自@kohske 的回答此处(归功于他),请参阅以前的编辑.

Considering the following polygon plot:

ggplot(df, aes(x=year,y=afw)) +
  geom_polygon() +
  scale_x_continuous("", expand=c(0,0), breaks=seq(1910,2010,10)) +
  theme_bw()

However, i want to fill this with two different colors. For example red for the black areas above 0 and blue for the black areas below 0. Unfortunately, using fill=col doesn't fill the correct areas.

I tried the following code (I added the geom_line in order to illustrate where the border of the fill should be):

ggplot(df, aes(x=year,y=afw)) +
  geom_line() +
  geom_polygon(aes(fill=col), alpha=0.5) +
  scale_x_continuous("", expand=c(0,0), breaks=seq(1910,2010,10)) +
  theme_bw()

which gives:

As you can see, it's filling a lot more than it's supposed to do. How can I solve this?

The data:

df <- structure(list(year = c(1901, 1901, 1901, 1902, 1903, 1904, 1905, 1906, 1907, 1908, 1909, 1910, 1911, 1912, 1913, 1914, 1915, 1916, 1917, 1918, 1919, 1920, 1921, 1922, 1923, 1924, 1925, 1926, 1927, 1928, 1929, 1930, 1931, 1932, 1933, 1934, 1935, 1936, 1937, 1938, 1939, 1940, 1941, 1942, 1943, 1944, 1945, 1946, 1947, 1948, 1949, 1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959, 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2013, 2013), afw = c(0, 0, -0.246246074793035, -2.39463317156723, -2.39785897801884, 0.840850699400514, -0.843020268341422, -3.02043962318013, -0.033342848986583, -2.04947188124465, -0.00431059092206709, 2.49568940907793, 1.96988295746503, 2.26665715101342, 0.986011989723095, 1.79568940907793, 2.06665715101342, -0.601084784470454, -3.21076220382529, 2.65052811875535, 0.46988295746503, -1.09140736511562, 0.0505281187553526, 1.41827005423922, -2.80108478447045, 0.611818441335997, -1.83011704253497, -0.30753639737368, -4.43011704253497, -0.897858978018841, 1.98601198972309, -0.965600913502712, 0.0795603768198685, 0.308592634884385, -5.33011704253497, 4.00214102198116, -0.594633171567228, 0.0698829574650297, -1.60753639737368, -2.81398801027691, -2.21398801027691, -2.4365686554382, 1.53439908649729, 1.06665715101342, -1.87205252640594, -0.688181558664002, 0.0569797316585783, -3.51398801027691, 0.979560376819868, 0.289237796174707, 1.24085069940051, -4.39140736511562, 1.13117328004567, -1.72689123608336, 2.20214102198116, 2.27310876391664, 1.46665715101342, 2.18278618327148, -0.23011704253497, 1.50536682843277, 1.17633457036826, -0.0785041393091639, -1.54947188124465, -3.85269768769626, -4.31398801027691, -0.80753639737368, 1.27956037681987, 1.2376248929489, 0.195689409077933, -3.38172994576078, -4.88172994576078, -0.675278332857551, 2.25375392520697, 0.0924636026263199, -0.446246074793035, 4.06988295746503, 0.350528118755352, -1.48172994576078, 1.81504424778761, -1.42689123608336, 2.22472166714245, 0.376334570368256, -3.88495575221239, 0.211818441335998, 0.586011989723094, 1.14407650585213, 2.55697973165858, 1.92794747359406, 1.20214102198116, 3.83439908649729, 1.64407650585213, 0.986011989723095, 0.753753925206965, 0.508592634884385, 1.911818441336, 2.11504424778761, -4.06560091350271, -2.58495575221239, 1.80859263488438, 1.37956037681987, 1.58923779617471, 1.88601198972309, -0.323665429631744, -0.291407365115615, 0.818270054239223, 0.0569797316585783, 0.795689409077933, 3.32472166714245, 0.595689409077933, -0.733342848986583, -0.955923494147874, -4.32689123608336, 3.29891521552955, 1.85697973165858, 2.74407650585213, 0, 0), col = structure(c(1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L), .Label = c("B", "A"), class = "factor")), .Names = c("year", "afw", "col"), class = c("tbl_df", "data.frame"), row.names = c(NA, -117L))

Note: as you can see in the data, there are 3 rows for both 1901 and 2013. I did this because I wanted to get the fill right. Although the black fill is correct, I seem not to get a working solution with colors.

The original dataset:

orig <- structure(list(year = c(1901, 1902, 1903, 1904, 1905, 1906, 1907, 1908, 1909, 1910, 1911, 1912, 1913, 1914, 1915, 1916, 1917, 1918, 1919, 1920, 1921, 1922, 1923, 1924, 1925, 1926, 1927, 1928, 1929, 1930, 1931, 1932, 1933, 1934, 1935, 1936, 1937, 1938, 1939, 1940, 1941, 1942, 1943, 1944, 1945, 1946, 1947, 1948, 1949, 1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959, 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013), afw = c(-0.246246074793035, -2.39463317156723, -2.39785897801884, 0.840850699400514, -0.843020268341422, -3.02043962318013, -0.033342848986583, -2.04947188124465, -0.00431059092206709, 2.49568940907793, 1.96988295746503, 2.26665715101342, 0.986011989723095, 1.79568940907793, 2.06665715101342, -0.601084784470454, -3.21076220382529, 2.65052811875535, 0.46988295746503, -1.09140736511562, 0.0505281187553526, 1.41827005423922, -2.80108478447045, 0.611818441335997, -1.83011704253497, -0.30753639737368, -4.43011704253497, -0.897858978018841, 1.98601198972309, -0.965600913502712, 0.0795603768198685, 0.308592634884385, -5.33011704253497, 4.00214102198116, -0.594633171567228, 0.0698829574650297, -1.60753639737368, -2.81398801027691, -2.21398801027691, -2.4365686554382, 1.53439908649729, 1.06665715101342, -1.87205252640594, -0.688181558664002, 0.0569797316585783, -3.51398801027691, 0.979560376819868, 0.289237796174707, 1.24085069940051, -4.39140736511562, 1.13117328004567, -1.72689123608336, 2.20214102198116, 2.27310876391664, 1.46665715101342, 2.18278618327148, -0.23011704253497, 1.50536682843277, 1.17633457036826, -0.0785041393091639, -1.54947188124465, -3.85269768769626, -4.31398801027691, -0.80753639737368, 1.27956037681987, 1.2376248929489, 0.195689409077933, -3.38172994576078, -4.88172994576078, -0.675278332857551, 2.25375392520697, 0.0924636026263199, -0.446246074793035, 4.06988295746503, 0.350528118755352, -1.48172994576078, 1.81504424778761, -1.42689123608336, 2.22472166714245, 0.376334570368256, -3.88495575221239, 0.211818441335998, 0.586011989723094, 1.14407650585213, 2.55697973165858, 1.92794747359406, 1.20214102198116, 3.83439908649729, 1.64407650585213, 0.986011989723095, 0.753753925206965, 0.508592634884385, 1.911818441336, 2.11504424778761, -4.06560091350271, -2.58495575221239, 1.80859263488438, 1.37956037681987, 1.58923779617471, 1.88601198972309, -0.323665429631744, -0.291407365115615, 0.818270054239223, 0.0569797316585783, 0.795689409077933, 3.32472166714245, 0.595689409077933, -0.733342848986583, -0.955923494147874, -4.32689123608336, 3.29891521552955, 1.85697973165858, 2.74407650585213)), .Names = c("year", "afw"), class = c("tbl_df", "data.frame"), row.names = c(NA, -113L))

解决方案

Get the indices where the y value of two consecutive time steps have different sign. Use linear interpolation between these points to generate new x values where y is zero.

First, a smaller example to make it easier to get a feeling for the linear interpolation and which points are added to the original data:

# original data
d <- data.frame(x = 1:6,
                y = c(-1, 2, 1, 2, -1, 1))

# coerce to data.table
library(data.table)
setDT(d)

# make sure data is ordered by x
setorder(d, x)

# add a grouping variable
# only to keep track of original and interpolated points in this example
d[ , g := "orig"]

# interpolation
d2 = d[ , {
  ix = .I[c(FALSE, abs(diff(sign(d$y))) == 2)]
  if(length(ix)){
    pred_x = sapply(ix, function(i) approx(x = y[c(i-1, i)], y = x[c(i-1, i)], xout = 0)$y)
    rbindlist(.(.SD, data.table(x = pred_x, y = 0, g = "new")))} else .SD
}]

d2   
#           x  y  grp
# 1  1.000000 -1 orig
# 2  2.000000  2 orig
# 3  3.000000  1 orig
# 4  4.000000  2 orig
# 5  5.000000 -1 orig
# 6  6.000000  1 orig
# 13 1.333333  0  new
# 11 4.666667  0  new
# 12 5.500000  0  new

Plot with original and new points differentiated by color:

ggplot(data = d2, aes(x = x, y = y)) +
  geom_area(data = d2[y <= 0], fill = "red", alpha = 0.2) +
  geom_area(data = d2[y >= 0], fill = "blue", alpha = 0.2) +
  geom_point(aes(color = g), size = 4) +
  scale_color_manual(values = c("red", "black")) +
  theme_bw()


Apply on OP's data:

d = as.data.table(orig)
# setorder(d, year)

d2 = d[ , {
  ix = .I[c(FALSE, abs(diff(sign(d$afw))) == 2)]
  if(length(ix)){
    pred_yr = sapply(ix, function(i) approx(afw[c(i-1, i)], year[c(i-1, i)], xout = 0)$y)
    rbindlist(.(.SD, data.table(year = pred_yr, afw = 0)))} else .SD}]

ggplot(data = d2, aes(x = year, y = afw)) +
  geom_area(data = d2[afw <= 0], fill = "red") +
  geom_area(data = d2[afw >= 0], fill = "blue") +
  theme_bw()


In reply to @Jason Whythe's comment, the method above can be modified to account for grouped data. The interpolation is made within each group, and the plot is facetted by group:

# data grouped by 'id' 
d = data.table(
  id = rep(c("a", "b", "c"), c(6, 5, 4)),
  x = as.numeric(c(1:6, 1:5, 1:4)),
  y = c(-1, 2, 1, 2, -1, 1,
        0, -2, 0, -1, -2, 
        2, 1, -1, 1.5))

# again, this variable is just added for illustration 
d[ , g := "orig"]

d2 = d[ , {
  ix = .I[c(FALSE, abs(diff(sign(.SD$y))) == 2)]
  if(length(ix)){
    pred_x = sapply(ix, function(i) approx(x = d$y[c(i-1, i)], y = d$x[c(i-1, i)], xout = 0)$y)
    rbindlist(.(.SD, data.table(x = pred_x, y = 0, g = "new")))} else .SD
}, by = id]

ggplot(data = d2, aes(x = x, y = y)) +
  facet_wrap(~ id) +
  geom_area(data = d2[y <= 0], fill = "red", alpha = 0.2) +
  geom_area(data = d2[y >= 0], fill = "blue", alpha = 0.2) +
  geom_point(aes(color = g), size = 4) +
  scale_color_manual(values = c("red", "black")) +
  theme_bw()


For an alternative base solution adapted from @kohske's answer here (credits to him), see previous edits.

这篇关于如何在y = 0的上方和下方用不同的颜色填充geom_polygon?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆