簡體   English   中英

在 barplot ggplot 上覆蓋線 plot -

[英]Overlaying line plot on barplot ggplot -

我正在嘗試將一條線 plot 覆蓋到條形圖上。 我可以分別 plot :

##plot sites
ggplot(graph, aes(x = Month, y = Anopheles_pos))
+ geom_col(size = 1, color = "darkblue", fill = "white") 

在此處輸入圖像描述

##plot line
ggplot(graph, aes(x = Month, y = Mean_EVI)) +
 geom_line(size = 1.5, color = "blue", group = 1)

在此處輸入圖像描述

但是,當我嘗試將 plot 線放到條形圖上時,它在底部是一條平線。 我試圖通過將第二個 y 軸(右側)固定為與線相同的比例來解決這個問題,但這並沒有解決線的繪制方式。

##plot together
    ggplot(graph) + 
      geom_col(aes( x = factor(Month, levels = month.name), y = Anopheles_pos), size = 1,
                    color = "darkblue", fill = "white") + 
      geom_line(aes(x = factor(Month, levels = month.name), y = Mean_EVI), size = 1.5, 
                color = "red", group = 1) +
     scale_y_continuous(sec.axis = sec_axis(~./50, name = "Mean_EVI"))

在此處輸入圖像描述

另一個小問題是我無法弄清楚如何使 x 軸為 0-100,因為 Anopheles_pos 值是百分比。

提前致謝!!

數據:

Mean_EVI: c(0.5687068, 0.5663895, 0.5653846, 0.6504931, 0.584727, 0.5799395, 0.617363, 0.581645, 0.6190386, 0.5208025, 0.6097692, 0.)

Anopheles_pos: c(33L, 42L, 38L, 31L, 54L, 47L, 22L, 15L, 2L, 15L, 12L, 19L)

您需要將 Mean_EVI 值放大 50 以匹配sec.axis調用的./50部分。

Mean_EVI <- c(0.6190386, 0.5208025, 0.6097692, 0.5689, 0.5687068, 0.5663895, 0.5653846, 0.6504931, 0.584727, 0.5799395, 0.617363, 0.581645)

Anopheles_pos <- c(2L, 15L, 12L, 19L, 33L, 42L, 38L, 31L, 54L, 47L, 22L, 15L)

graph <- data.frame(Mean_EVI, Anopheles_pos, Month = 1:12)

ggplot(graph) + 
  geom_col(aes(x = factor(Month, labels = month.name), y = Anopheles_pos), size = 1,
           color = "darkblue", fill = "white") + 
  geom_line(aes(x =  factor(Month, labels = month.name), y = Mean_EVI*50), size = 1.5, 
            color = "red", group = 1) +
  scale_y_continuous(sec.axis = sec_axis(~./50, name = "Mean_EVI")) +
  coord_cartesian(ylim = c(0, 100))

在此處輸入圖像描述

暫無
暫無

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

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