library(tidyverse)
delta <- tibble(
type = c("alpha", "beta", "gamma"),
a = rnorm(3, 5),
b = rnorm(3, 6)
) %>%
mutate(delta = abs(a - b)) %>%
gather(`a`, `b`, `delta`, key = "letter", value = "value")
ggplot(delta %>% filter(letter != "delta"), aes(type, value, fill = letter)) +
geom_col(position = "dodge") +
geom_col(data = delta %>% filter(letter == "delta"), width = 0.5) +
scale_color_manual("grey", "black", "blue")
我希望a
和b
条为灰色和黑色。 delta
栏变为蓝色。如何使用scale_color_manual()
执行此操作?看来我上面的语法已关闭。
答案 0 :(得分:6)
有两件事需要更改:
由于您使用过fill = letter
,因此您应该使用scale_fill_manual
而不是scale_color_manual
(如果您使用color = letter
会比较合适)。 / p>
手动颜色values
应作为向量提供。
library(tidyverse)
delta <- tibble(
type = c("alpha", "beta", "gamma"),
a = rnorm(3, 5),
b = rnorm(3, 6)
) %>%
mutate(delta = abs(a - b)) %>%
gather(`a`, `b`, `delta`, key = "letter", value = "value")
ggplot(delta %>% filter(letter != "delta"), aes(type, value, fill = letter)) +
geom_col(position = "dodge") +
geom_col(data = delta %>% filter(letter == "delta"), width = 0.5) +
scale_fill_manual(values = c("grey", "black", "blue"))
由reprex package(v0.2.1)于2018-10-08创建