簡體   English   中英

GGplot 在圖例中添加 geom_line 項目作為 geom_bar 項目

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM