gpt4 book ai didi

r - 对于堆叠条和闪避条,如何从图例中删除闪避条元素?

转载 作者:行者123 更新时间:2023-12-02 01:33:20 24 4
gpt4 key购买 nike

感谢combine stacked bars and dodged bars ,我使用显示的数据框创建了下面的图。但是现在,由于轴标题命名了条形图,除了一个堆叠条形图之外,我如何删除图例元素?也就是说,图例是否可以只显示 Big8 条的线段?

> dput(combo)
structure(list(firm = structure(c(12L, 1L, 11L, 13L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("Avg.", "Co", "Firm1",
"Firm2", "Firm3", "Firm4", "Firm5", "Firm6", "Firm7", "Firm8",
"Median", "Q1", "Q3"), class = "factor"), metric = structure(c(5L,
1L, 4L, 6L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Avg.",
"Big8", "Co", "Median", "Q1", "Q3"), class = "factor"), value = c(0.0012,
0.0065, 0.002, 0.0036, 0.0065, 0.000847004466666667, 0.000658907411111111,
0.0002466389, 8.41422555555556e-05, 8.19149222222222e-05, 7.97185555555556e-05,
7.82742555555556e-05, 7.56679888888889e-05), grp = structure(c(1L,
2L, 3L, 6L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("Q1",
"Avg.", "Median", "Co", "Big8", "Q3"), class = "factor")), .Names = c("firm",
"metric", "value", "grp"), row.names = c(NA, -13L), class = "data.frame")

这是绘图代码。

ggplot(combo, aes(x=grp, y=value, fill=firm)) + 
geom_bar(stat="identity") +
labs(x = "", y = "") +
theme(legend.position = "bottom") +
guides(fill = guide_legend(nrow = 2))

情节,理想情况下图例中的元素集会较少。

enter image description here

最佳答案

您可以为 scale_fill_discrete 手动设置 breaks:

library(ggplot2)
ggplot(combo, aes(x=grp, y=value, fill=firm)) +
geom_bar(stat="identity") +
labs(x = "", y = "") +
theme(legend.position = "bottom") +
guides(fill = guide_legend(nrow = 2)) +
scale_fill_discrete(breaks = combo$firm[combo$metric=="Big8"])

enter image description here

我不能 100% 确定您要保留哪些标签,但是手动输入的矢量 combo$firmcombo$metric 都可以。

关于r - 对于堆叠条和闪避条,如何从图例中删除闪避条元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32850427/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com