簡體   English   中英

將水平線添加到R中ggplot2中的堆積條形圖中,並在圖例中顯示

[英]Add horizontal lines to stacked barplot in ggplot2 in R, and show in legend

我有一個堆積的條形圖,類似下面的例子。

我想在每個條形圖上添加一組或兩組水平線(指定顏色和線條類型),每個條形圖的不同值,並將這些添加到圖例中。

Titanic.df <- as.data.frame(Titanic)

Titanic.ag <- aggregate( Freq ~ Sex + Class + Age, data=Titanic.df, sum, subset = Survived == "Yes")

bars <- rep(c(0.5, NA, 0.7, NA, 0.6, NA, 0.9, NA), 2)

ggplot(Titanic.ag, aes(x = Class, y = Freq, fill = Sex)) + 
  geom_bar(position = "fill", stat = "identity") + 
  facet_grid(~Age) +
  geom_errorbar(aes(y = bars, ymin = bars, ymax = bars,  col = "Ref1")) + 
  scale_fill_manual(values = c("darkgreen", "darkblue") ) + 
  labs(col = "Reference",
       fill= "",
       y = "Proportion",
       x = "Class")

泰坦尼克號堆積條形圖與水平線

我已嘗試在幾個問題上使用geom_errorbar(),但我遇到兩件事:

如果我為誤差線添加一個值向量,那么ggplot需要與數據幀中的長度相同(例如Titanic.ag中的16),但是堆疊時只有8個柱。 這就是為什么我在上面的bars使用了NA。 還有其他選擇嗎?

更重要的是,我想控制顏色和線型,但如果我將它們添加到geom_bar(),我就會失去我的傳奇。 例如

  geom_errorbar(aes(y = bars, ymin=bars, ymax=bars,  col = "Ref1"), col = "red", linetype = 2)

geom_segment()是另一種選擇嗎?

編輯錯別字,澄清了不同的horziontal線的價值。

我不完全確定你需要這個但是。 通過單獨的geom_abline調用,您可以根據需要輕松自定義每一行。

ggplot(Titanic.ag, aes(x = Class, y = Freq, fill = Sex)) + 
  geom_bar(position = "fill", stat = "identity") + 
  facet_grid(~Age) +
  geom_abline(slope=0, intercept=0.5,  col = "red",lty=2) +
  geom_abline(slope=0, intercept=0.75,  col = "lightblue",lty=2) +
  scale_fill_manual(values = c("darkgreen", "darkblue") ) + 
  labs(col = "Reference",
       fill= "",
       y = "Proportion",
       x = "Class")

產生這個情節

在此輸入圖像描述

此外,也許可以幫助標簽。

UPDATE

好的,現在我明白了...使用相同方法的快速近似將是使用多個條形向量和錯誤條調用。 這有點令人筋疲力盡,但可能會工作,data.frame可能無法正常工作,因為您希望每個欄都可以多次自定義。 此外,考慮到NA被刪除,因此他們不能按照人們的意願工作......也許使用高於1的數字並將ylim調整為1,這樣他們就不會顯示

bars1 <- rep(c(0.5, NA, 0.7, NA, 0.6, NA, 0.9, NA), 2)
bars2 <- rep(c(NA, 0.9, 0.1, 0.1, NA, NA, NA, 0.5), 2)
bars3 <- rep(c(0.1, NA, NA, NA, NA, NA, NA, 0.1), 2)

ggplot(Titanic.ag, aes(x = Class, y = Freq, fill = Sex)) + 
  geom_bar(position = "fill", stat = "identity") + 
  facet_grid(~Age) +
  geom_errorbar(aes(y = bars1, ymin = bars1, ymax = bars1), color="Orange",lty=2) + 
  geom_errorbar(aes(y=bars2,ymin=bars2,ymax=bars2),color="White",lty=2)+
  geom_errorbar(aes(y=bars3,ymin=bars3,ymax=bars3),color="Purple",lty=2)+
  scale_fill_manual(values = c("darkgreen", "darkblue") ) + 
  labs(col = "Reference",
       fill= "",
       y = "Proportion",
       x = "Class")

在此輸入圖像描述

上面的答案並沒有產生一個傳奇。 它確實改變了線型和顏色,但我也在原始問題中展示了這一點。 經過一番搜索,我找到了添加圖例的方法,所以我發布了這里。

library(ggplot2)

Titanic.df <- as.data.frame(Titanic)

Titanic.ag <- aggregate( Freq ~ Sex + Class + Age, data=Titanic.df, sum, subset = Survived == "Yes")

bars <- rep(c(0.5, NA, 0.7, NA, 0.6, NA, 0.9, NA), 2)

ggplot(Titanic.ag, aes(x = Class, y = Freq, fill = Sex)) + 
  geom_bar(position = "fill", stat = "identity") + 
  facet_grid(~Age) +
  geom_errorbar(aes(y = bars, ymin = bars, ymax = bars,  col = "Ref1"), linetype = 2, size = 1) + 
  scale_fill_manual(values = c("darkgreen", "darkblue") ) + 

  scale_colour_manual(name='', values=c("Ref1" = "darkred"), guide ="legend") +
  guides(col = guide_legend(override.aes = list(linetype=2), title = "Reference")) + 

  labs(fill= "",
       y = "Proportion",
       x = "Class")

這可以適用於添加另一個geom_errorbar()。

我仍然得到警告,由於工作輪與在NAS中geom_errorbar()為載體yyminymax ,這得到更多方面更復雜,但它的工作原理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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