如何在 R ggplotly 中显示所有图例?截至目前,它仅显示匹配数据的图例。
这是我到目前为止所做的:
library(ggplot2)
library(plotly)
schoolname <- c("Xavier", "Dakota")
value <- c(34,50)
df <- data.frame(schoolname,value)
ggplot(df, aes(x=schoolname, y=value,
fill = cut(value, breaks =c(0,34,69,100),
labels = c("Failed - <65%",
"Pass - 65%",
"Excellent - 80%")))) +
geom_bar(stat = "identity",position="dodge", width=0.5, color = "#333333") +
geom_text(aes(x=schoolname, y=value + 4.1,
label = paste0(value), tooltip = NULL),
inherit.aes = F, color='black',
position = position_dodge2(2), size=4, vjust=1.5) +
xlab("School Name") +
ylab("Result (%)") +
theme(plot.title = element_text(size = 18, hjust=0, vjust=0)) +
scale_y_continuous(limits = c(0,100))+
scale_fill_manual(values = c("Failed - <65%" = "#ea9999",
"Pass - 65%" = "#ffc8aa",
"Excellent- 80%" = "#d4edbc")) +
theme(legend.position = "top") +
guides(fill = guide_legend(title = "Result", override.aes = list(size = 4))) -> p
如果我使 cut() 函数中的标签与scale_fill_manual() 中的标签完全匹配,并且将 drop = FALSE 添加到scale_fill_manual() 中,我确实会看到图例中的所有标签。确保两组标签中每个标签中的空格数完全相同。例如,选中“失败 - <65%" in your code. There is also some spurious text in your scale_fill_manual(). It starts with "[enter image description here]".
未填充的关卡在图例中以浅灰色显示。我认为这是 ggplot2 设计师故意的。
p <- ggplot(df, aes(x=schoolname, y=value,
fill = cut(value, breaks =c(0,34,69,100),
labels = c("Failed - <65%", "Pass - 65%", "Excellent - 80%"))))+
geom_bar(stat = "identity",position="dodge", width=0.5, color = "#333333") +
geom_text(aes(x=schoolname, y=value + 4.1, label = paste0(value), tooltip = NULL), inherit.aes = F, color='black', position=position_dodge2(2), size=4, vjust=1.5)+
xlab("School Name") +
ylab("Result (%)") +
theme(plot.title = element_text(size = 18, hjust=0, vjust=0)) +
scale_y_continuous(limits = c(0,100))+
scale_fill_manual(values = c("Failed - <65%" = "#ea9999",
"Pass - 65%" = "#ffc8aa",
"Excellent - 80%" = "#d4edbc"), drop = FALSE) +
theme(legend.position = "top")+
guides(fill = guide_legend(title = "Result", override.aes = list(size = 4)))
print(p)
我们可以添加一个具有“优秀”评级的虚拟行(
schoolname == NA
),然后该级别将显示在图例中,而无需绘制。此外,您有一个拼写错误,您的 cut()
中的级别与 scale_fill_...()
中的级别不匹配。另请注意,breaks
中的 cut()
与您在图例中显示的内容不匹配。
library(dplyr)
library(ggplot2)
library(plotly)
df %>%
add_row(value = 100) %>% ## adding the dummy row
ggplot(aes(x=schoolname, y=value, fill = cut(value, breaks =c(0,34,69,100),
labels = c("Failed - <65%",
"Pass - 65%",
"Excellent - 80%")))) +
geom_bar(stat = "identity", position="dodge", width=0.5, color = "#333333") +
geom_text(aes(x=schoolname, y=value + 4.1,
label = paste0(value), tooltip = NULL),
inherit.aes = F, color='black',
position = position_dodge2(2), size=4, vjust=1.5) +
xlab("School Name") +
ylab("Result (%)") +
theme(plot.title = element_text(size = 18, hjust=0, vjust=0)) +
scale_y_continuous(limits = c(0,100)) +
scale_x_discrete(limits = df$schoolname) +
scale_fill_manual(values = c("Failed - <65%" = "#ea9999",
"Pass - 65%" = "#ffc8aa",
"Excellent - 80%" = "#d4edbc"),
drop = FALSE) +
theme(legend.position = "top") +
guides(fill = guide_legend(title = "Result", override.aes = list(size = 4))) -> p
ggplotly(p)
创建于 2024-07-14,使用 reprex v2.0.2
最初我想将其关闭为即使图表中未显示类别也显示整个图例的副本,但这需要不同的方法,因此数据目前不适用于每个级别。