ggplot2 中图例的自定义顺序,因此它与图中因子的顺序不匹配
Custom order of legend in ggplot2 so it doesn't match the order of the factor in the plot
我正在尝试制作自定义李克特风格的情节。我喜欢这个情节,但我想在保持情节顺序的同时更改图例的顺序。
数据:
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 的扩展版本。现在应该 运行。
很遗憾,我无法完全复制您的图形,因为我似乎缺少您的 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
我正在尝试制作自定义李克特风格的情节。我喜欢这个情节,但我想在保持情节顺序的同时更改图例的顺序。
数据:
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 的扩展版本。现在应该 运行。
很遗憾,我无法完全复制您的图形,因为我似乎缺少您的 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