【问题标题】:ggplot add geom_path with different color aesthetic // Error: Error: Insufficient values in manual scaleggplot add geom_path 具有不同的颜色美学//错误:错误:手动比例中的值不足
【发布时间】:2020-07-27 04:46:17
【问题描述】:

我正在尝试将我的 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,这将是最简单的解决方案。数据sn-p在这篇文章的最后。

谢谢!!

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     )

【问题讨论】:

  • 嗨 - 您可以通过 dput(your.data.frame) 或至少其中的一部分发布您的数据,以便我们有一个可重复的示例吗?
  • 当然,我忘记了。在 OP 中添加了一个 sn-p。
  • 感谢您的发帖,但不幸的是,我无法使用该数据生成合理的图表(我认为它仅代表一个日期)。使用sample() 从原始数据框中获取几行可能是一个好主意:(例如,algo_pricing[sample(1:nrow(algo_pricing), 20),] 可以随机获取 20 行),但您需要确保您的示例适用于您发布的数据集.此外,您从代码中得到了什么,它似乎哪里出了问题?您是在生成一个看起来不正确的情节还是收到错误消息?
  • 啊,很抱歉。进行了上面的编辑。我收到“手动比例错误中的值不足”。那是因为颜色已经用两个值定义了。
  • 删除您的 scale_color_manualscale_linetype_manual 电话。这将产生一个情节,但可能不是你想看到的。 ggplot 将在您指定相同的美学时组合比例:例如,您指定将geom_line 的线型和geom_point 的外部颜色应用于相同的因子(as.factor(bbox))。创建了一个图例,指示 bbox 用一种颜色点缀或用另一种颜色纯色(两者结合)。删除那些scale_... 电话,发布你的情节,然后澄清你的问题——可能与传说和标签有关。

标签: r ggplot2 aesthetics


【解决方案1】:

这可能是当天令人费解的情节!投入我的两分钱:我做可视化的次数越多,我越认为如果你很难用 ggplot 完成工作,这可能表明你的可视化可能并不理想。也许考虑减少要在绘图中显示的维度数量。

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

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

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

代码中的其他 cmets。

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 =...)) 控制它的“通常”方式似乎不适用于ggnewscale

reprex package (v0.3.0) 于 2020-04-15 创建

【讨论】:

  • ggnewscale::new_scale_colour() 是我一直在寻找的!!这非常有帮助,非常感谢您的帮助和宝贵的 cmets。我确实意识到情节有点过头了,但我认为学习如何定义几个手动色标仍然很有用。干杯!
【解决方案2】:

试一试,很难复制您的数据框,但我遇到了类似的问题并且以下工作有效。

首先定义您的颜色和值(我也不完全理解您在此处尝试使用“NA”做什么,您需要有颜色而不是 NA)。您还为两个不同的图定义了一种颜色(线和路径确保您分别为这两个图添加)。

还可以看看这个解决方案:[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"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 2018-09-10
    • 2021-08-06
    • 1970-01-01
    • 2021-08-19
    相关资源
    最近更新 更多