正确指定因子以防止ggplot图例按字母顺序排序(我知道之前已经有人问过)

时间:2018-10-28 22:14:30

标签: r ggplot2 legend

我知道这个问题已经被问过很多次了,这让我对为什么感到困惑的感觉更好一些。 我试图使颜色顺序与为绘制而选择的变量保持一致,并与图例保持一致。

我看着herehere寻找希望解决的问题,但这不是一个不同的问题,或者我必须被误解或犯了一个我看不见的错误。

我指定了因子变量Group的顺序,但是颜色以字母顺序而不是因子顺序改变。因为我将组指定为一个因子并指定了级别顺序(“ B”始终始终首先作为“ group_1”),所以我本来以为“ B”的颜色将是一致的,但是它会发生变化。

如果有人能让我知道我在想什么,我将不胜感激!

可复制的示例

library(tidyverse)

# Sample data
Group <- c("A", "B", "C")
Value <- c(3, 3, 5)

# Create data frame
mydata   <- data.frame(Group, Value)

# Create variable for group selected for plotting
group_1 <- "B"
group_2 <- "A"

# Make a pyramid plot, making one group negative numbers for bar chart
pyramid <- mydata %>%
  filter(Group == group_1 | Group == group_2) %>%
  mutate(Value = ifelse(Group == group_2, Value * -1, Value)) %>%
    # Thought this would keep color ordering consistent
  mutate(Group = factor(Group, levels = c(group_1, group_2)))         


pyramid_plot <- ggplot(pyramid, aes(x    = Group,
                                    y    = Value,
                                    fill = Group)
                       ) +
  geom_bar(data = subset(pyramid, Group == group_1),
           stat  = "identity"
           ) +
  geom_bar(data = subset(pyramid, Group == group_2),
           stat  = "identity"
           ) +
  coord_flip() +
  scale_fill_manual(name = "Group",
                    values = c("#1f78b4", "#33a02c"),   # blue, green
                    breaks = c(group_1, group_2),
                    labels = c(group_1, group_2)
                    )
pyramid_plot

plot1

# Now do another plot, keeping group_1 the same but changing group_2
group_1 <- "B"
group_2 <- "C"

pyramid <- mydata %>%
  filter(Group == group_1 | Group == group_2) %>%
  mutate(Value = ifelse(Group == group_2, Value * -1, Value)) %>%
  # Thought this would keep color ordering consistent
  mutate(Group = factor(Group, levels = c(group_1, group_2)))         


pyramid_plot2 <- ggplot(pyramid, aes(x    = Group,
                                    y    = Value,
                                    fill = Group)
                       ) +
  geom_bar(data = subset(pyramid, Group == group_1),
           stat  = "identity"
           ) +
  geom_bar(data = subset(pyramid, Group == group_2),
           stat  = "identity"
           ) +
  coord_flip() +
  scale_fill_manual(name = "Group",
                    values = c("#1f78b4", "#33a02c"),   # blue, green
                    breaks = c(group_1, group_2),
                    labels = c(group_1, group_2)
                    )
pyramid_plot2

plot2

1 个答案:

答案 0 :(得分:0)

Group <- c("A", "B", "C")
Value <- c(3, 3, 5)

mydata   <- data.frame(Group, Value)

group_1 <- "B"
group_2 <- "A"

pyramid <- mydata %>%
  filter(Group == group_1 | Group == group_2) %>%
  mutate(Value = ifelse(Group == group_2, Value * -1, Value)) %>%
  mutate(Group = factor(Group, levels = c(group_1, group_2)))         


ggplot(pyramid, aes(x    = Group, y = Value, fill = Group)) +
  geom_bar(stat  = "identity") +
  coord_flip() +
  scale_fill_manual(name = "Group",
                    values = c("#1f78b4", "#33a02c"))

输出:

enter image description here

group_1 <- "B"
group_2 <- "C"

pyramid <- mydata %>%
  filter(Group == group_1 | Group == group_2) %>%
  mutate(Value = ifelse(Group == group_2, Value * -1, Value)) %>%
  mutate(Group = factor(Group, levels = c(group_1, group_2)))         


ggplot(pyramid, aes(x = Group, y = Value, fill = Group)) +
  geom_bar(stat  = "identity") +
  coord_flip() +
  scale_fill_manual(name = "Group",
                    values = c("#1f78b4", "#33a02c"))

第二个输出:

enter image description here

B始终为蓝色。

我认为问题在于两次调用geom_bar并设置数据子集。 Fill已经为您做到了,因此无需进一步分割数据。