[英]How to overlay a barplot on a stacked barplot?
I am trying to overlay a bar plot over a stacked bar plot as you can see in the figure below.我正在尝试将条 plot 覆盖在堆叠条 plot 上,如下图所示。 Although this is working well the dotted line type does not appear as such in the legend.尽管这运行良好,但虚线类型并未在图例中显示。
Dotted bar plot on top of stacked barplot堆叠条形图顶部的虚线条 plot
This is my data:这是我的数据:
data = data.frame(treat=c("min","plus","min","plus"),
obs=c(8,7,121,98),
type=c("Tare","Tare","Net","Net"))
data2 = data.frame(treat=c("min","plus"),
obs=c(20,15),
type=c("Sugar","Sugar"))
And the code I use to make the graph:我用来制作图表的代码:
ggplot(data = data, aes(x=treat, y=obs, fill = type)) +
theme_classic() +
geom_bar(stat="identity", col = "black", width = .75) +
geom_bar(data = data2, stat="identity", fill = "transparent", col = "black", size = .8, linetype = "dotted", width = .75, show.legend = F) +
scale_fill_manual(values = c("#F8766D", "#00BFC4", NA),
limits = c("Tare", "Net", "Sugar"))
How do I make the second bar appear with dotted lines in the legend?如何使第二个条在图例中以虚线显示?
Is this what you're looking for?这是你要找的吗?
library(tidyverse)
data = data.frame(treat=c("min","plus","min","plus"),
obs=c(8,7,121,98),
type=c("Tare","Tare","Net","Net"))
data2 = data.frame(treat=c("min","plus"),
obs=c(20,15),
type=c("Sugar","Sugar"))
ggplot(data = data, aes(x=treat, y=obs, fill = type)) +
theme_classic() +
geom_bar(stat="identity", col = "black", width = .75) +
geom_bar(data = data2, stat="identity",
fill = "transparent", col = "black", size = .8,
aes(linetype = "dotted"),
width = .75,
show.legend = T) +
scale_fill_manual(values = c("#F8766D", "#00BFC4", NA),
limits = c("Tare", "Net", "Sugar"),
name="type")+
scale_linetype_manual(name="Sugar", values="dotted")+
guides(fill = guide_legend(keywidth = 1, keyheight = 1),
linetype=guide_legend(keywidth = 3, keyheight = 1))
Created on 2020-12-10 by the reprex package (v0.3.0)由reprex package (v0.3.0) 于 2020 年 12 月 10 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.