【发布时间】:2022-05-01 12:03:11
【问题描述】:
一天以来,我一直在尝试修改我认为在 GGPlot 上简单明了的图例,但没有取得太大进展(我在 Stackexchange 上进行了广泛搜索,但看不到明显的相似之处)。
简单地说:我展示了一个收入与预测的条形图,其中水平线代表预测。我设法获得了带有零宽度误差线的水平条。这些列被捕获在图例中,但我不知道如何为错误栏添加图例:
这是一个可重现的示例。我在最终图像中添加了我需要的完整美学设置,以防它们引起我遗漏的一些冲突:
df = data.frame(Team=c("Dunder Mifflin","Saber","Wernham Hogg",
"OsCorp","Queen Industries","Stark Industries"),
Industry=c("Paper","Paper","Paper",
"Tech","Tech","Tech"),
Revenue=c(20,100,10,
50,150,200),
Forecast=c(30,120,5,
70,120,120))
g1 = ggplot(df) +
geom_col(aes(x=Team,y = Revenue,fill=Industry)) +
geom_errorbar(aes(x=Team,y=Forecast, ymax=Forecast, ymin=Forecast)) +
# Everything from here probably irrelevant aesthetic settings,
# included in case there is some clash I am unaware of
scale_fill_hue(c = 40) +
facet_grid(cols = vars(Industry),
scales = "free_x",
space = "free_x",
switch="x") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.line = element_line(),
strip.placement = "outside",
strip.text.x = element_text(size=10),
strip.background.x = element_rect(colour = "grey", fill = "white"),
text=element_text(family="Roboto",face="bold"))
print(g1)
【问题讨论】: