ggplot2 中图例的自定义顺序,因此它与图中因子的顺序不匹配
Posted
技术标签:
【中文标题】ggplot2 中图例的自定义顺序,因此它与图中因子的顺序不匹配【英文标题】:Custom order of legend in ggplot2 so it doesn't match the order of the factor in the plot 【发布时间】:2021-12-08 19:25:26 【问题描述】:我正在尝试制作自定义李克特风格的情节。我喜欢这个情节,但我想在保持情节顺序的同时改变图例的顺序。
数据:
df <- structure(list(Benefit = structure(c(1L, 1L, 2L, 2L, 3L, 3L,
3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L,
8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L), .Label = c("Medical: Importance",
"Medical:Satisfaction", "Dental: Importance", "Dental: Satisfaction",
"Vision: Importance", "Vision: Satisfaction", "401K: Importance",
"401K: Satisfaction", "EAP: Importance", "EAP: Satisfaction"
), class = "factor"), value = structure(c(1L, 2L, 5L, 6L, 1L,
2L, 4L, 5L, 6L, 9L, 1L, 2L, 4L, 5L, 6L, 9L, 1L, 2L, 3L, 4L, 5L,
6L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 9L), .Label = c("Very Important",
"Important", "Not at all Important", "Less Important", "Strongly Satisfied",
"Satisfied", "Strongly Dissatisfied", "Dissatisified", "N/A"), class = "factor"),
Percent = c(-80.7, -19.3, -50, -50, -64.3, -33.9, 1.8, -43.6,
-50.9, 5.5, -52.7, -41.8, 5.5, -33.9, -51.8, 14.3, -75, -17.3,
5.8, 1.9, -50, -30.8, 19.2, -13.7, -39.2, 5.9, 41.2, -13.2,
-45.3, 41.5)), class = "data.frame", row.names = c(4L, 9L,
24L, 29L, 2L, 7L, 12L, 22L, 27L, 42L, 5L, 10L, 15L, 25L, 30L,
45L, 1L, 6L, 16L, 11L, 21L, 26L, 41L, 3L, 8L, 18L, 13L, 23L,
28L, 43L))
这是 ggplot2 制作我喜欢的情节的代码:
col4 <- c("#81A88D","#ABDDDE","#B40F20","#F4B5BD","orange","#F3DF6C","gray")
p <- ggplot(df, aes(x=Benefit, y = Percent, fill = value, label=abs(Percent))) +
geom_bar(stat="identity", width = .5, position = position_stack(reverse = TRUE)) +
geom_col(position = 'stack') +
scale_x_discrete(limits = rev(levels(df$Benefit))) +
geom_text(position = position_stack(vjust = 0.5),
angle = 45, color="black") +
coord_flip() +
scale_fill_manual(values = col4) +
scale_y_continuous(breaks=(seq(-100,100,25)), labels=abs(seq(-100,100,by=25)), limits=c(-100,100)) +
theme_minimal() +
theme(
axis.title.y = element_blank(),
legend.position = c(0.85, 0.8),
legend.title=element_text(size=14),
axis.text=element_text(size=12, face="bold"),
legend.text=element_text(size=12),
panel.background = element_rect(fill = "transparent",colour = NA),
plot.background = element_rect(fill = "transparent",colour = NA),
#panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank()
)+
labs(fill="") + ylab("") + ylab("Percent") +
annotate("text", x = 9.5, y = 50, label = "Importance") +
annotate("text", x = 8.00, y = 50, label = "Satisfaction")
p
剧情如下:
主要是图例的顺序。例如,对于 EAP:重要性我的情节从左到右,“非常重要”->“重要”->“不太重要”->“一点也不重要”。但是传说非常重要”->“重要”->“一点也不重要”->“不太重要”。
我希望图例与情节相匹配。我尝试过使用指南和各种比例功能,但无法正常工作。数据按照图例的顺序排列,但这是我可以让绘图看起来像我想要的那样的唯一方法。我需要一个自定义图例!
提前感谢您的帮助!
编辑以删除对“med”数据框的引用,这是 df 的扩展版本。这应该现在运行。
【问题讨论】:
【参考方案1】:很遗憾,我无法完全重现您的身材,因为我似乎遗漏了您的 med
数据。
但是,相应地更改数据框中的级别应该可以解决问题。只需在ggplot()
命令之前执行以下操作:
levels(df$value) <- c("Very Important", "Important", "Less Important",
"Not at all Important", "Strongly Satisfied",
"Satisfied", "Strongly Dissatisfied", "Dissatisified", "N/A")
编辑
能够重现您的示例,我想出了以下解决方案,有点老套。
p <- ggplot(df, aes(x=Benefit, y = Percent, fill = value, label=abs(Percent))) +
geom_bar(stat="identity", width = .5, position = position_stack(reverse = TRUE)) +
geom_col(position = 'stack') +
scale_x_discrete(limits = rev(levels(df$Benefit))) +
geom_text(position = position_stack(vjust = 0.5),
angle = 45, color="black") +
coord_flip() +
scale_fill_manual(labels = c("Very Important", "Important", "Less Important",
"Not at all Important", "Strongly Satisfied",
"Satisfied", "N/A"),values = col4) +
scale_y_continuous(breaks=(seq(-100,100,25)), labels=abs(seq(-100,100,by=25)), limits=c(-100,100)) +
theme_minimal() +
theme(
axis.title.y = element_blank(),
legend.position = c(0.85, 0.8),
legend.title=element_text(size=14),
axis.text=element_text(size=12, face="bold"),
legend.text=element_text(size=12),
panel.background = element_rect(fill = "transparent",colour = NA),
plot.background = element_rect(fill = "transparent",colour = NA),
#panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank()
)+
labs(fill="") + ylab("") + ylab("Percent") +
annotate("text", x = 9.5, y = 50, label = "Importance") +
annotate("text", x = 8.00, y = 50, label = "Satisfaction") +
guides(fill = guide_legend(override.aes = list(fill = c("#81A88D","#ABDDDE","#F4B5BD","#B40F20","orange","#F3DF6C","gray")) ) )
p
【讨论】:
谢谢,我解决了上面的问题,所以你应该能够复制。不幸的是,这种方法不起作用。我以前试过,现在也试过了。看来我需要一个不涉及格式化数据本身的解决方案 - 或者至少不格式化“值”。数据当前的格式是给我想要的图表,而不是我想要的图例。感谢您的回复! 您的代码现在可以正常运行。希望我的编辑有所帮助。 行得通!有时我们得有点老套才能得到我们想要的东西。在转向这里之前,我昨天已经闲逛了几个小时。非常感谢您的帮助!以上是关于ggplot2 中图例的自定义顺序,因此它与图中因子的顺序不匹配的主要内容,如果未能解决你的问题,请参考以下文章