【问题标题】:print text on my R plots在我的 R 图上打印文本
【发布时间】:2018-06-03 02:23:37
【问题描述】:

我很难在我的 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)
}

【问题讨论】:

  • 使用text() 绘制文本。您可能也想先绘制线条,然后绘制白点作为文本的背景,最后将文本绘制在顶部。
  • 格雷戈尔你会怎么做?用我的数据框?我试过了,但它不起作用...谢谢
  • scales 包是 ggplot2 宇宙的一部分。我非常怀疑它应该与基本绘图功能一起使用。

标签: r plot survival-analysis


【解决方案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)      
}

我在plot() 中设置了axes = F 而不是labels = F 以消除警告,并且我删除了polygon 代码,因为它没有做任何事情。您可能需要进一步调整文本和点的cex,使其大小合适。

【讨论】:

  • 不错!谢谢格雷戈尔!实际上,现在最难的部分是在每条曲线上添加“P =” - 如第一个图所示。有什么想法吗?最好的,
  • 基本一样。您可以使用paste()"P = " 粘贴在您的标签上。正如我在答案中所说,我将把它作为练习留给你。
  • 是的,当然!通过定义向量p1 &lt;- c("1","2","3") 并添加text(p[i], labels = p1[i], cex = 0.9) ,它会打印值,但永远不会在正确的位置。您将如何指定正确的位置?谢谢。
  • 指定你想要的 x 和 y 坐标。请参阅?text 寻求帮助。
  • 哦,这就是我实际上同时做的,但分别在循环之外!它完美无缺!再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-02
  • 1970-01-01
  • 2023-03-17
  • 2018-01-06
  • 2020-08-08
相关资源
最近更新 更多