楼主: dxystata
2713 2

[问答] 如何画出这样的可信区间图 [推广有奖]

版主

大师

36%

还不是VIP/贵宾

-

TA的文库  其他...

Software

中英文Ebook

R学习

威望
2
论坛币
183453 个
通用积分
15259.3479
学术水平
208 点
热心指数
271 点
信用等级
174 点
经验
296789 点
帖子
5536
精华
1
在线时间
13596 小时
注册时间
2006-6-21
最后登录
2024-10-5

初级学术勋章 初级热心勋章 中级热心勋章 初级信用勋章

50论坛币
QQ图片20170306170555.png

关键词:如何
沙发
杰然一生 发表于 2017-3-6 21:21:46 |只看作者 |坛友微信交流群
http://www.sr.bham.ac.uk/~ajrs/R/gallery/plot_cluster_z_vs_kT.txt
中的source("http://www.sr.bham.ac.uk/~ajrs/R/scripts/errorbar.R")
#--Add errorbars (using function in sourced errorbar.R script):
with(A, errorbar(z, 0, 0, Tx, -Tx.le, Tx.ue, bnd=F, col=col))        # bnd=F means input data are errors not confidence bounds

自己研究下

使用道具

藤椅
zhangyangsmith 发表于 2017-5-24 20:57:08 |只看作者 |坛友微信交流群
  1. # Make up some dummy data
  2. set.seed(1705190905)

  3. dfDmmy <-
  4.   merge(
  5.     data.frame( quarter = c(1:4) ),
  6.     data.frame( year = c(2006:2009) )
  7.        )

  8. dfDmmy$mid <-
  9.   1.5*exp( -0.2*( seq.int( nrow(dfDmmy) ) - 1 )*
  10.              exp( rnorm( nrow(dfDmmy), sd = 0.05 ) ) )

  11. dfDmmy$upper <- with( dfDmmy, mid + runif( length(mid) )*mid/2 )

  12. dfDmmy$lower <- with( dfDmmy, mid - runif( length(mid) )*mid )

  13. # Set up plot
  14. par(family = "serif")

  15. plot(
  16.   NA, type = "n",
  17.   xlim = range( seq.int( nrow(dfDmmy) ) ), ylim = c(-0.5, 2.5),
  18.   xlab = NA_character_, ylab = NA_character_, axes = FALSE
  19.     )

  20. # Axes
  21. with(
  22.   dfDmmy,
  23.   {
  24.     axis( side = 1, at = seq_along(quarter), labels = quarter,
  25.           lwd = 0, lwd.ticks = 1 )
  26.    
  27.     lapply(
  28.       unique(year),
  29.       function(i)
  30.         mtext( text = as.character(i), side = 1, line = 2,
  31.                at = mean( which( year == i ) ), adj = 0.5, padj = 1 )
  32.           )
  33.   }
  34.     )

  35. abline( h = par("usr")[3] )

  36. axis( side = 2, at = c(-1:5/2), labels = sprintf("%5.2f", -1:5/2),
  37.       las = 1, lwd = 0, lwd.ticks = 1 )

  38. abline( v = par("usr")[1] )

  39. # Title
  40. # mtext(
  41. #   text = expression( paste( "Statistics with BCPNN methods, ", italic(IC) ) ),
  42. #   side = 3, adj = 0, xpd = FALSE
  43. #      )
  44. # text(
  45. #   x = par("usr")[1] - par("mar")[2]*par("cxy")[1],
  46. #   y = par("usr")[4] + par("cxy")[2]/2,
  47. #   labels = expression( paste( "Statistics with BCPNN methods, ", italic(IC) ) ),
  48. #   adj = c(0, 0), xpd = NA
  49. #     )

  50. windowsFonts( SONG = windowsFont("TT SimSun") )

  51. text(
  52.   x = par("usr")[1] - par("mar")[2]*par("cxy")[1],
  53.   y = par("usr")[4] + par("cxy")[2]/2,
  54.   labels = "BCPNN",
  55.   family = "serif", adj = c(0, 0), xpd = NA
  56.     )

  57. text(
  58.   x = par("usr")[1] - par("mar")[2]*par("cxy")[1] + strwidth("BCPNN"),
  59.   y = par("usr")[4] + par("cxy")[2]/2,
  60.   labels = "\u{6CD5}\u{7EDF}\u{8BA1}\u{91CF}\u{FF0C}",
  61.   family = "SONG", adj = c(0, 0), xpd = NA
  62.     )

  63. text(
  64.   x =
  65.     par("usr")[1] - par("mar")[2]*par("cxy")[1] +
  66.     strwidth("BCPNN\u{6CD5}\u{7EDF}\u{8BA1}\u{91CF}\u{FF0C}") + par("cxy")[1],
  67.   y = par("usr")[4] + par("cxy")[2]/2,
  68.   labels = "IC",
  69.   family = "serif", font = 3, adj = c(0, 0), xpd = NA
  70.     )

  71. # Horizontal reference line
  72. abline( h = 0, lty = 3, col = "blue" )

  73. # Plot the data
  74. with(
  75.   dfDmmy,
  76.   {
  77.     # Intervals
  78.     dvX <- seq_along(mid)
  79.    
  80.     # mapply(
  81.     #   lines,
  82.     #   x =
  83.     #     apply( cbind( seq_along(lower) - 0.1, seq_along(lower) + 0.1 ), 1, I ),
  84.     #   y = apply( cbind( lower, lower ), 1, I )
  85.     #       )
  86.     mapply(
  87.       segments,
  88.       x0 = lapply(dvX - 0.1, I), x1 = lapply(dvX + 0.1, I),
  89.       y0 = lapply(lower, I)
  90.           )
  91.    
  92.    
  93.     # mapply(
  94.     #   lines,
  95.     #   x =
  96.     #     apply( cbind( seq_along(upper) - 0.1, seq_along(upper) + 0.1 ), 1, I ),
  97.     #   y = apply( cbind( upper, upper ), 1, I )
  98.     #       )
  99.     mapply(
  100.       segments,
  101.       x0 = lapply(dvX - 0.1, I), x1 = lapply(dvX + 0.1, I),
  102.       y0 = lapply(upper, I)
  103.           )
  104.    
  105.    
  106.     # mapply(
  107.     #   lines,
  108.     #   x = apply( cbind( seq_along(lower), seq_along(upper) ), 1, I ),
  109.     #   y = apply( cbind( lower, upper ), 1, I )
  110.     #       )
  111.     mapply(
  112.       segments,
  113.       x0 = lapply(dvX, I), y0 = lapply(lower, I), y1 = lapply(upper, I)
  114.           )

  115.     # Points and lines
  116.     points( mid, type = "o" )
  117.   }
  118.     )
复制代码

Output:
ConfidenceIntervals.png

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-10-6 01:34