簡體   English   中英

在具有置信區間上限和下限值時繪制ggplot()

[英]Plotting a ggplot() when having the confidence interval upper and lower values

請耐心等待我,因為我對R很新,特別是ggplot()。 這是我第二次詢問有關使用此功能的事情。 我想創建一個類似下面的圖:

在此輸入圖像描述

為此目的,我一直在使用這樣的腳本:


df <- tibble::tribble(~Proportion, ~Error, ~Time,
                      0.351 , 0.154, "Day",
                      0.223 , 0.157 , "Night")

dfnew <- df %>% 
  mutate(ymin = Proportion - Error,
         ymax = Proportion + Error)

p <-   ggplot(data = dfnew, aes(x = Time, y = Proportion)) +
  geom_point() + geom_line(aes(group = 1), color="lightblue",size=2) + 
  geom_errorbar(aes(x = Time, ymin = ymin, ymax = ymax),color="purple",width=0.1,size=1)


p<-p+theme(axis.text=element_text(size=12),
        axis.title=element_text(size=14))

但是,我現在面臨的問題是,包含上限值和下限值的置信區間數據,而不是上面腳本中的錯誤值。 我的數據如下:

> df
# A tibble: 2 x 4
  Average Lower Upper Time 
    <dbl> <dbl> <dbl> <chr>
1   0.351 0.284 0.421 Day  
2   0.223 0.178 0.275 Night

關於如何將這兩個LowerUpper值實現到誤差條中的任何想法?

任何輸入都表示贊賞!

這有幫助嗎?

使用新數據:

df2 <- read.table(
  header = T,
  stringsAsFactors = F,
  text = "  Average Lower Upper Time
1   0.351 0.284 0.421 Day  
2   0.223 0.178 0.275 Night"
)

並稍微調整繪圖數據源:

ggplot(data = df2, aes(x = Time, y = Average)) +
  geom_point() +
  geom_line(aes(group = 1), color="lightblue",size=2) + 
  geom_errorbar(aes(x = Time, ymin = Lower, ymax = Upper),
                color="purple",width=0.1,size=1)

我得到了這張圖:

在此輸入圖像描述

哪個看起來糾正了我的眼睛,但我可能會錯過你所接受的東西。 讓我知道..

暫無
暫無

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

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