【问题标题】:Adding a legend entry for geom_segment为 geom_segment 添加图例条目
【发布时间】:2021-09-12 02:14:56
【问题描述】:

在下面的图表中,我想在图例中为蓝色参考线添加一个项目,在本示例中,我们可以将其称为“任意线”。任何人都可以为我提供一个解决方案来将其纳入传说吗?请注意,最终的情节必须以情节形式呈现。

library(tidyverse)
library(plotly)

dat <- data.frame(peeps= c("Bill", "Bob", "Becky"),
                  vals = c(10, 15, 12),
                  label = c("8% Fake", "12% Pizza", "45% Becky"),
                  grp = c("Bears", "Bears", "Mongoose") %>% as.factor)
p1 <- dat %>% 
  ggplot(aes(x = peeps, y = vals, fill = grp)) +
  geom_bar(stat = "identity") +
  geom_segment(aes(x = 0.55, xend = 3.45, y = 5, yend = 5), color = "blue") +
  scale_y_continuous(expand = c(0, 0)) +
  coord_flip()

ggplotly(p1) %>% 
  layout(legend = list(orientation = "h",
                       xanchor = "center",
                       y = -0.15,
                       x = 0.5))

【问题讨论】:

    标签: r ggplot2 plotly


    【解决方案1】:

    尝试添加:

    scale_fill_manual(name = "", values="blue", label="Arbitrary Line")
    

    【讨论】:

    • 很遗憾没用 - 不过谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 2015-10-06
    • 2017-12-13
    • 2023-01-27
    相关资源
    最近更新 更多