[英]GGplot is adding geom_line item as a geom_bar item in legend
我正在制作沉积物剖面粒度分布图,堆叠条形图表示沙子、淤泥和粘土,并添加一条线显示每个深度的中值。 该图看起来不错,但我最终输出的图例混淆了我的一些项目。
这是我的代码的细分:
GS_as = data.frame(Depth = c(10,30,50,70,90),
clay = c(0.99,0,0,2.86,3.62),
silt = c(55.48,81.48,53.26,79.5,70.71),
sand = c(43.53,18.52,46.74,17.64,25.67))
long = melt(GS_as,id = "Depth")
df = data.frame(Depth = c(10,30,50,70,90),
value = c(34.8,24.84,48.9,12.7,19.73),
variable = c("median","median","median","median","median"))
ggplot(long,aes(x=Depth,y=value,fill=variable)) +
geom_bar(stat="identity") + coord_flip() +
scale_y_continuous(position = "right") +
scale_x_continuous(breaks = seq(10,900,by = 20),trans='reverse') +
scale_fill_grey() +
geom_line(data=df, aes(x= Depth, y = value,group=variable,colour=variable)) +
geom_point(data=df,aes(x= Depth, y = value,group=variable,colour=variable))
最终输出给了我这张图1
现在,如何从粒度的图例灰度中删除中值,以及如何从灰度中的每个框中删除点? 这些点只应与中值一起作为单独的变量呈现。 我已经搜索了很长时间以找到解决方案,但没有得到任何地方。 我猜我以一种奇怪的不直观的方式得到了我的最终图表。
此外,如果可能的话,我还希望中线和点为黑色,删除变量标题并将所有项目分组为 1 级。
感谢您提供的任何帮助。
要解决出现在fill
图例中的中位数的第一个问题,您可以将fill
区域设置为geom_bar
。 对于黑色,您可以通过scale_color_manual
设置颜色。 可以通过实验室设置或删除图例标题,最后(据我了解)您可以通过theme
选项“将所有项目分组为 1 级”,方法是删除图例之间的间距和它们周围的几乎所有边距。
library(ggplot2)
ggplot(long, aes(x = Depth, y = value)) +
geom_bar(aes(fill = variable), stat = "identity") +
coord_flip() +
scale_y_continuous(position = "right") +
scale_x_continuous(breaks = seq(10, 900, by = 20), trans = "reverse") +
scale_fill_grey() +
geom_line(data = df, aes(x = Depth, y = value, group = variable, colour = variable)) +
geom_point(data = df, aes(x = Depth, y = value, group = variable, colour = variable)) +
scale_color_manual(values = c("black")) +
labs(fill = NULL, color = NULL) +
theme(legend.spacing.y = unit(0, "pt"), legend.margin = margin(1, 0, 0, 0))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.