简体   繁体   English

GGplot 在图例中添加 geom_line 项目作为 geom_bar 项目

[英]GGplot is adding geom_line item as a geom_bar item in legend

I'm making sediment profile grain size distribution graphs, with stacked bar charts representing sand, silt and clay and an added line showing the median value for each depth.我正在制作沉积物剖面粒度分布图,堆叠条形图表示沙子、淤泥和粘土,并添加一条线显示每个深度的中值。 The graph looks good, yet the legend of my final output is mixing up some of my items.该图看起来不错,但我最终输出的图例混淆了我的一些项目。

Here is a breakdown of my code:这是我的代码的细分:

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))

The final output is giving me this graph 1最终输出给了我这张图1

Now, how do I remove median from the legend grayscale of grain sizes, and how do i remove the points from each box in grayscale?现在,如何从粒度的图例灰度中删除中值,以及如何从灰度中的每个框中删除点? The points should only be presented with the median as a separate variable.这些点只应与中值一起作为单独的变量呈现。 I've searched long to find a solution, but have not gotten anywhere.我已经搜索了很长时间以找到解决方案,但没有得到任何地方。 I'm guessing I got to my final graph by a strange unintuitive way.我猜我以一种奇怪的不直观的方式得到了我的最终图表。

Additionally, if its possible I would also like the median line and points to be black, remove the variables title and group all the items under 1 level.此外,如果可能的话,我还希望中线和点为黑色,删除变量标题并将所有项目分组为 1 级。

I appreciate any help you can give.感谢您提供的任何帮助。

To fix your first issue with the median showing up in the fill legend you could make fill a locale aes of geom_bar .要解决出现在fill图例中的中位数的第一个问题,您可以将fill区域设置为geom_bar For a black color you could set the color via scale_color_manual .对于黑色,您可以通过scale_color_manual设置颜色。 The legend titles could be set or removed via labs and finally (and as far as I understand you) you could "group all the items under 1 level" via theme options by removing the spacing between the legends and almost all the margin around them.可以通过实验室设置或删除图例标题,最后(据我了解)您可以通过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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM