2017-12-20 13 views
1

Rプロットにテキストを印刷するのが苦労しています。私のRプロットにテキストを印刷

私は3つのプロットがあり、それぞれにラベル(p値)を追加したいと思います。 次に、カーブを描くことができる5つの値のそれぞれについて、その値をグラフィックに印刷します。

実際、私の姿は次の図のようになります(下の図を参照)。

次のスクリプトを使用してください。

助けが必要ですか?事前に おかげで、 ベスト、

df <- structure(list(lowerA = c(4.1, 4.6, 5.5, 4.7, 5), Group1 = c(4.8,5.4, 6.4, 5.5, 5.7), 
         upperA = c(5.7, 6.3, 7.3, 6.3, 6.6), lowerB = c(6.2, 7.1, 8.7, 7.4, 8.2), 
         Group2 = c(7.3, 8.3, 10, 8.6, 9.5), upperB = c(8.6, 9.6, 11.4, 9.9, 10.8), 
         lowerC = c(18.3, 19.5, 24.3, 22.9, 25.5), Group3 = c(21.4, 22.5, 27.6, 26.3, 29), 
         upperC = c(24.7, 25.8, 31, 29.8, 32.7)), 
       .Names = c("lowerA", "Group1", "upperA", "lowerB","Group2", "upperB", "lowerC", "Group3", "upperC"), 
       row.names = c("year1", "year2", "year3", "year4", "year5"), class = c("tbl_df", "tbl", "data.frame")) 

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

#### Data Base 

colnames(df) <- gsub("A","1",colnames(df)) 
colnames(df) <- gsub("B","2",colnames(df)) 
colnames(df) <- gsub("C","3",colnames(df)) 

colors <- c("#000099", "#CC0000", "#808080") 
labels <- c("red line", "black line","black line") 

######### 
lablist<-as.vector(c("2010","2011","2012","2013","2014")) 
axis(1, labels = FALSE) 

######### 

library(scales) 
plot(df$Group1, type = "n",ylim = c(0,40),frame.plot = FALSE, main = "", xlab = "", ylab = "Survival at hospital discharge (%)",labels = FALSE) 
axis(1, at=1:5, labels=c("2011-2012","2012-2013","2013-2014","2014-2015","2015-2016")) 
axis(2, at=c(0, 10, 20, 30, 40), labels=c("0","10","20","30","40")) 


for(i in 1:3){ 
    lines(df[,paste0("Group",i)], col = colors[i]) 

    polygon(x = c(1:nrow(df),nrow(df):1), 
      y = c(df[,paste0("lower",i)],rev(df[,paste0("upper",i)])), 
      col = alpha(c("white","white","white"), 0.1), 
      # si tu veux garder les couleur des IC le long des courbes mets commande si après: 
      # col = alpha(colors[i], 0.1), 
      border = NA) 

    arrows(x0 = c(1:nrow(df)), 
     x1 = c(1:nrow(df)), 
     y0 = df[,paste0("lower",i)], 
     y1 = df[,paste0("upper",i)], 
     col = colors[i], 
     angle = 90, 
     length = 0.05, 
     code = 3) 
} 

enter image description here

+1

テキストをプロットするには 'text()'を使用します。あまりにも先に線をプロットし、次に白い点をプロットしてテキストの背景として使用し、最後にテキストを上にプロットすることもできます。 – Gregor

+0

グレゴールはどのようにしますか?私のデータフレームで?私は試しましたが、動作しません...ありがとう – Peter

+0

スケールパッケージはggplot2宇宙の一部です。私はそれがベースプロット関数で使用されるべきであることに深刻な疑念を持っています。 –

答えて

1

ここでは、ポイントラベルを取得例です。読者のための練習としてP値ラベルを残しておきます。

plot(df$Group1, type = "n",ylim = c(0,40),frame.plot = FALSE, main = "", xlab = "", ylab = "Survival at hospital discharge (%)", axes = F) 
axis(1, at=1:5, labels=c("2011-2012","2012-2013","2013-2014","2014-2015","2015-2016")) 
axis(2, at=c(0, 10, 20, 30, 40)) 

for(i in 1:3){ 
    lines(df[,paste0("Group",i)], col = colors[i]) 

    arrows(x0 = c(1:nrow(df)), 
     x1 = c(1:nrow(df)), 
     y0 = df[,paste0("lower",i)], 
     y1 = df[,paste0("upper",i)], 
     col = colors[i], 
     angle = 90, 
     length = 0.05, 
     code = 3) 

    points(df[,paste0("Group",i)], col = "white", pch = 15, cex = 2) 
    text(df[,paste0("Group",i)], labels = df[,paste0("Group",i)], col = colors[i], cex = 0.7)  
} 

私は警告を取り除くためにaxes = F代わりのplot()labels = Fを設定し、それは何もしていなかったので、私はpolygonコードを削除しました。テキストと点のサイズをさらに細かく調整するには、cexをさらに調整することをおすすめします。

enter image description here

+0

ニース!ありがとうグレゴール!実際に最も難しいのは、各曲線に "P ="を追加することです。最高、 – Peter

+0

これは基本的に同じです。 'paste()'を使ってラベルに '' P = ''を貼り付けることができます。私が答えて言ったように、私はそれをあなたのための運動として残します。 – Gregor

+0

はいベクトル 'p1 < - c(" 1 "、" 2 "、" 3 ")を定義し、' text(p [i]、labels = p1 [i]、cex = 0.9)を加えることによって、決して適切な場所にはありません。どのように適切な場所を指定しますか?ありがとう。 – Peter

関連する問題