ggplot add geom_path with different color aesthetic // Error: Error: Insufficient values in manual scale

ggplot add geom_path with different color aesthetic // Error: Error: Insufficient values in manual scale

我正在尝试将我的 ggplot 中的 geom_points 与 geom_path 连接起来。线条的颜色应与 geom_point 填充颜色相同。但是,geom_path 不知道填充和颜色用于不同的分组。

我还使用

以黑色轮廓突出显示某些 geom_points

scale_color_manual(values = c("NA", "black"), labels = c("No Buy Box", "Buy Box"))

我能做什么?事实上,我想通过 seller_id 绘制不同颜色(填充)的点,如果 bbox = 1,则用 color = black 突出显示这些点中的某些点,另外使用 geom_path 连接颜色中的点.我假设在我如何根据子采样对图表进行分层方面存在一些更普遍的问题。 geom_path 不知道 fill,这本来是最简单的解决方案。数据片段位于此 post.

的末尾

谢谢!!

ggplot(data = subset(algo_pricing,bbox_product == 9200000096286280), aes(x = bbox_time2)) +
  geom_point(mapping = aes(y = price_total, colour = as.factor(bbox), fill = seller_id), shape = 21) +
  geom_line(data = subset(algo_pricing, bbox ==1 & bbox_product == 9200000096286280), 
            mapping = aes(y = bbox_price, linetype = as.factor(bbox)),colour = "black") +
  geom_path(mapping = aes(y = price_total, colour = seller_id), linetype = "dotted") +
  scale_linetype_manual(values = "dotted", labels = "Buy Box Price") +
  scale_color_manual(values = c("NA", "black"), labels = c("No Buy Box", "Buy Box"))
example <- wrapr::build_frame(
   "bbox_time2"           , "bbox_price", "price_total", "seller_id"            , "bbox", "min_price", "bbox_product" |
     as.Date("2019-01-07"), 151         , 169.9        , "linkerlisse"          , 0L    , 129.5      , 4.641e-308     |
     as.Date("2019-01-18"), 125         , 169.9        , "linkerlisse"          , 0L    , 112        , 4.641e-308     |
     as.Date("2019-01-20"), 125         , 169.9        , "goedslapennl"         , 0L    , 118.5      , 4.641e-308     |
     as.Date("2019-01-14"), 120         , 169.9        , "decoware"             , 0L    , 114.3      , 4.641e-308     |
     as.Date("2019-01-18"), 125         , 169.9        , "goedslapennl"         , 0L    , 112        , 4.641e-308     |
     as.Date("2019-01-19"), 125         , 125          , "bol.com"              , 1L    , 125        , 4.641e-308     |
     as.Date("2019-01-20"), 125         , 169.9        , "decoware"             , 0L    , 121        , 4.641e-308     |
     as.Date("2019-01-19"), 125         , 169.9        , "decoware"             , 0L    , 124.2      , 4.641e-308     |
     as.Date("2019-01-10"), 135         , 120.3        , "hetbestebeddengoed.nl", 0L    , 120.3      , 4.641e-308     |
     as.Date("2019-01-11"), 135         , 135          , "bol.com"              , 1L    , 115.5      , 4.641e-308     |
     as.Date("2018-12-31"), 151         , 151          , "bol.com"              , 1L    , 143.8      , 4.641e-308     |
     as.Date("2019-01-17"), 125         , 169.9        , "goedslapennl"         , 0L    , 116.2      , 4.641e-308     |
     as.Date("2019-01-20"), 125         , 169.9        , "goedslapennl"         , 0L    , 119.8      , 4.641e-308     |
     as.Date("2019-01-17"), 125         , 169.9        , "goedslapennl"         , 0L    , 115.5      , 4.641e-308     |
     as.Date("2019-01-22"), 112.3       , 112.3        , "hetbestebeddengoed.nl", 1L    , 112.3      , 4.641e-308     |
     as.Date("2019-01-01"), 151         , 169.9        , "linkerlisse"          , 0L    , 142.1      , 4.641e-308     |
     as.Date("2019-01-21"), 125         , 127.5        , "sleepworld"           , 0L    , 117.8      , 4.641e-308     |
     as.Date("2018-12-31"), 151         , 151          , "bol.com"              , 1L    , 142.8      , 4.641e-308     |
     as.Date("2019-01-18"), 125         , 169.9        , "smulderstextiel.nl"   , 0L    , 125        , 4.641e-308     |
     as.Date("2019-01-01"), 151         , 169.9        , "linkerlisse"          , 0L    , 141.2      , 4.641e-308     )

试一试,很难复制你的数据框,但我遇到了类似的问题,下面的方法有效。

首先定义你的颜色和值(而且我不明白你在这里试图用 "NA" 做什么,你需要有颜色而不是 NAs)。您还为两个不同的图定义了一种颜色(请确保为这两个图分别添加线和路径)。

另请参阅此解决方案:[plot below showing 2 legends when controling scale color manual


cl <- c("black" = "Buy Box", "blue" = "No Buy Box")
ggplot(data = subset(algo_pricing,bbox_product == 9200000096286280), aes(x = bbox_time2)) +
  geom_point(mapping = aes(y = price_total, colour = as.factor(bbox), fill = seller_id), shape = 21) +
  geom_line(data = subset(algo_pricing, bbox ==1 & bbox_product == 9200000096286280), 
            mapping = aes(y = bbox_price, linetype = as.factor(bbox)),colour = "Buy Box") +
  geom_path(mapping = aes(y = price_total, colour = seller_id), linetype = "dotted", colour = "No Buy Box") +
  scale_color_manual(values = c("blue", "black"))

今天的剧情大概就是这样了!投入我的两分钱:我做可视化的次数越多,我就越认为如果你努力用 ggplot 完成工作,这可能表明你的可视化可能不理想。也许考虑减少要在绘图中显示的维度数量。

然而,这是一个很好的练习来映射和控制美学。

它需要一些数据整理、因子水平控制以及控制每次绘制多少点。我通过每次删除重复条目来解决这个问题。您需要决定如何管理它。

我还为价格添加了一点抖动,以便您可以更好地看到线条。它会稍微扭曲值,但您可以更改抖动量。

代码中的其他注释。

library(tidyverse)
example <- example %>% 
  distinct(seller_id, bbox_time2,.keep_all = TRUE) %>%
  mutate(bbox_sell  = paste(seller_id, bbox, sep = '_'),
         price_total = jitter(price_total, amount = 1)) %>%
  arrange(bbox_time2, seller_id)   # arranging is important for geom_path


ggplot( # setting the general aesthetics. You could do this in each geom call, but I am a bit lazy, so I define the main aesthetics here. 
  data = example,
  aes(
    x = bbox_time2,
    y = price_total,
    group = seller_id
  )
) +
  geom_point(
    aes(colour = seller_id) # color aesthetic matches the following geom_path.
# also, the shape defaults to 16. This avoids messing with both fill and color aesthetic. 
  ) +
  geom_path(
    aes(colour = seller_id),
    linetype = "dotted"
  ) +
  geom_path(
    data = filter(example, bbox == 1),
    aes(linetype = "box1", group = bbox_sell),
    colour = "black".   # color defined outside of aesthetic! 
  ) +
  scale_linetype_manual(name = NULL, values = "dotted", labels = "Buy Box Price") +
  ggnewscale::new_scale_colour() + # now here's an option how to easily create two color scales. 
  geom_point(
    data = filter(example, bbox == 1),
    aes(color = as.character(bbox)), # you can now use a new color scale. 
    shape = 21, # using a different shape for the highlighted points
  ) +
  scale_color_manual(name = NULL, values = "black", labels = "Buy Box") 

图例顺序当然是奇特的。 Controlling legend order is quite a thing,用 + guides(xxx = guide_legend(order =...)) 控制它的 'usual' 方法似乎不适用于 ggnewscale

reprex package (v0.3.0)

于 2020-04-15 创建