仍然对组,颜色和bspline感到困惑

时间:2018-04-11 19:03:10

标签: r ggplot2 ggforce

我希望将债务与失业联系起来。 我希望这条线是弯曲的,因此我使用了bspline2。 当房屋改变时,线的颜色应该改变。 每年应该有一个点(即1月1日) 我重复了一些观点,因为我认为bspline用三点绘制曲线。

尽管如此,我还没有完全理解为什么情节有差距。很可能与我对小组的部分理解有关。

enter image description here

此外,1月1日的点应该出现在线上。我不认为他们使用的是geom_point,因为如果线条笔直的话就是将它们放在图上。我试过这种变化,但我认为我的克星组受到了干扰。

  stat_bspline2(aes(x = dot_x, y = dot_y, color = ..group.., group = year), size = 1, n = 300, geom = "point") 

总之,

1)为什么情节中存在差距?如何解决?

2)如何获得曲线上的点?

代码:

library(tidyverse)
library(ggforce) 

ggplot(debt_unemp) + 
  stat_bspline2(aes(x = debt, y = unemp, color = ..group.., group = party), size = 1, n = 300, geom = "bspline0") +
  scale_color_gradientn(colours = c("red", "pink", "green", "white"), guide = F) +
  geom_point(aes(x = dot_x, y = dot_y), size = 2)

这是结果 dput(debt_unemp)

structure(list(year = structure(c(5844, 6209, 6574, 6940, 7305, 
7305, 7305, 7670, 8035, 8401, 8766, 9131, 9131, 9496, 9862, 10227, 
10592, 10957, 11323, 11688, 11688, 11688, 12053, 12053, 12418, 
12784, 13149, 13514, 13879, 14245, 14610, 14975, 15340, 15706, 
15706, 16071, 16436, 16801, 17167), class = "Date"), date = structure(c(5844, 
6209, 6574, 6940, 7305, 7305, 7578, 7670, 8035, 8401, 8766, 9131, 
9307, 9496, 9862, 10227, 10592, 10957, 11323, 11688, 11688, 11792, 
12053, 12348, 12418, 12784, 13149, 13514, 13879, 14245, 14610, 
14975, 15340, 15706, 15747, 16071, 16436, 16801, 17167), class = "Date"), 
    fyear = c("1986-1987", "1987-1988", "1988-1989", "1989-1990", 
    "1990-1991", "1990-1991", "1991-1992", "1991-1992", "1992-1993", 
    "1993-1994", "1994-1995", "1995-1996", "1996-1997", "1996-1997", 
    "1997-1998", "1998-1999", "1999-2000", "2000-2001", "2001-2002", 
    "2002-2003", "2002-2003", "2002-2003", "2003-2004", "2004-2005", 
    "2004-2005", "2005-2006", "2006-2007", "2007-2008", "2008-2009", 
    "2009-2010", "2010-2011", "2011-2012", "2012-2013", "2013-2014", 
    "2012-2013", "2014-2015", "2015-2016", "2016-2017", "2017-2018"
    ), debt = c(31.5, 34, 35.5, 35.4, 38.4, 38.4, 46.6, 49.4, 
    61.8, 80.6, 90.7, 101.9, 105.2, 108.8, 112.7, 114.7, 134.4, 
    132.5, 132.1, 132.6, 132.6, 134.4, 138.8, 140.5, 140.9, 152.7, 
    153.7, 156.6, 169.6, 193.6, 214.5, 236.2, 252.8, 268, 270, 
    285.4, 295.4, 301.6, 311.7), unemp = c(7, 6.1, 5, 5, 6.2, 
    6.2, 8.67, 9.5, 10.8, 10.9, 9.6, 8.7, 8.84, 9, 8.4, 7.2, 
    6.3, 5.7, 6.3, 7.1, 7.1, 7.04, 6.9, 6.82, 6.8, 6.6, 6.3, 
    6.4, 6.6, 9.1, 8.7, 7.9, 7.9, 7.6, 7.57, 7.3, 6.8, 6.5, 6
    ), prem = c("Peterson", "Peterson", "Peterson", "Peterson", 
    "Peterson", "Peterson", "Rae", "Rae", "Rae", "Rae", "Rae", 
    "Rae", "Harris", "Harris", "Harris", "Harris", "Harris", 
    "Harris", "Harris", "Harris", "Harris", "Eves", "Eves", "McGuinty", 
    "McGuinty", "McGuinty", "McGuinty", "McGuinty", "McGuinty", 
    "McGuinty", "McGuinty", "McGuinty", "McGuinty", "McGuinty", 
    "Wynne", "Wynne", "Wynne", "Wynne", "Wynne"), party = c("Liberal", 
    "Liberal", "Liberal", "Liberal", "Liberal", "Liberal", "NDP", 
    "NDP", "NDP", "NDP", "NDP", "NDP", "PC", "PC", "PC", "PC", 
    "PC", "PC", "PC", "PC", "PC", "PC", "PC", "Liberal2", "Liberal2", 
    "Liberal2", "Liberal2", "Liberal2", "Liberal2", "Liberal2", 
    "Liberal2", "Liberal2", "Liberal2", "Liberal2", "Liberal2", 
    "Liberal2", "Liberal2", "Liberal2", "Liberal2"), dot_x = c(31.5, 
    34, 35.5, 35.4, 38.4, 38.4, NA, 49.4, 61.8, 80.6, 90.7, 101.9, 
    NA, 108.8, 112.7, 114.7, 134.4, 132.5, 132.1, 132.6, 132.6, 
    NA, 138.8, NA, 140.9, 152.7, 153.7, 156.6, 169.6, 193.6, 
    214.5, 236.2, 252.8, 268, NA, 285.4, 295.4, 301.6, 311.7), 
    dot_y = c(7, 6.1, 5, 5, 6.2, 6.2, NA, 9.5, 10.8, 10.9, 9.6, 
    8.7, NA, 9, 8.4, 7.2, 6.3, 5.7, 6.3, 7.1, 7.1, NA, 6.9, NA, 
    6.8, 6.6, 6.3, 6.4, 6.6, 9.1, 8.7, 7.9, 7.9, 7.6, NA, 7.3, 
    6.8, 6.5, 6)), .Names = c("year", "date", "fyear", "debt", 
"unemp", "prem", "party", "dot_x", "dot_y"), row.names = c(NA, 
-39L), class = c("tbl_df", "tbl", "data.frame"), spec = structure(list(
    cols = structure(list(year = structure(list(format = "%Y"), .Names = "format", class = c("collector_date", 
    "collector")), date = structure(list(format = "%Y-%m-%d"), .Names = "format", class = c("collector_date", 
    "collector")), fyear = structure(list(), class = c("collector_character", 
    "collector")), debt = structure(list(), class = c("collector_number", 
    "collector")), unemp = structure(list(), class = c("collector_number", 
    "collector")), prem = structure(list(), class = c("collector_character", 
    "collector")), party = structure(list(), class = c("collector_character", 
    "collector")), dot_x = structure(list(), class = c("collector_number", 
    "collector")), dot_y = structure(list(), class = c("collector_number", 
    "collector"))), .Names = c("year", "date", "fyear", "debt", 
    "unemp", "prem", "party", "dot_x", "dot_y")), default = structure(list(), class = c("collector_guess", 
    "collector"))), .Names = c("cols", "default"), class = "col_spec"))

0 个答案:

没有答案
相关问题