楼主: duguqiusui
13666 7

[问答] R语言 plot函数 [推广有奖]

  • 3关注
  • 1粉丝

高中生

22%

还不是VIP/贵宾

-

威望
0
论坛币
0 个
通用积分
0
学术水平
1 点
热心指数
0 点
信用等级
0 点
经验
208 点
帖子
13
精华
0
在线时间
20 小时
注册时间
2012-2-13
最后登录
2013-8-28

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

以上图中,图一是我分割的图形,图二是自己绘制的初期的图(在此感谢qoiqpwqr的帮助),图三是最终的结果图,有几点想向大家请教
1、图一中的25区域如何添加标题,我用的title函数,但添加以后称为图二的结果,请问怎么调整一下?
2、关于layout分割图形以后,如何跳到下一个作图区域?比如从图一的1区域到2区域,有时绘图的时候会出现图形区域不对的状况
3、关于图二中的plot函数,如何将坐标的原点绘制在26区域的正中间?
4、如何在图二中添加类似图三的注释,如果用title函数的话如何确定坐标位置?
5、对于一系列数据,如何将符合条件的绘制出来,不符合条件的写入文件中,比如绘图时y大于10就写入文件,小于10就绘制出来,目前我只会用plot把这一列全部数出来,不会筛选
6、plot函数中如何将图二中不同扇区的点用不同的颜色显示出来?
问题有点多,自己刚学R不久,希望大家能多多帮助,在此先感谢大家了

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:plot R语言 Layout Title ITL 图形 分割 layout title 如何

Rplot02.png (8.24 KB)

Rplot02.png

无标题.png (311.09 KB)

无标题.png

Rplot.png (3.65 KB)

Rplot.png

已有 2 人评分经验 学术水平 收起 理由
ltx5151 + 20 有意思
耕耘使者 + 1 鼓励学术交流

总评分: 经验 + 20  学术水平 + 1   查看全部评分

沙发
trier2006 发表于 2012-5-13 21:15:33 |只看作者 |坛友微信交流群
太强大了,等着学习
最好的医生是自己,最好的药物是时间……

使用道具

藤椅
qoiqpwqr 发表于 2012-5-19 00:02:30 |只看作者 |坛友微信交流群
能把程序传上来吗?

使用道具

板凳
chenliangqiang 在职认证  发表于 2012-5-21 09:50:42 |只看作者 |坛友微信交流群
恩 坐等高人

使用道具

报纸
sunlet 发表于 2012-5-31 20:10:25 |只看作者 |坛友微信交流群
强大啊

使用道具

地板
AthenaUchiha 发表于 2015-3-13 17:24:54 |只看作者 |坛友微信交流群
这没想到plot还能做这些

使用道具

7
Frank.p 发表于 2017-4-24 00:09:37 |只看作者 |坛友微信交流群
看不懂楼主要做什么

使用道具

8
zhangyangsmith 发表于 2017-5-24 20:51:19 |只看作者 |坛友微信交流群
Quite an old post but very nice plot. I hope the functions I used below acted the same way as in 2012:

  1. # Data for the outer plots
  2. dfOuter <-
  3.   as.data.frame(
  4.     matrix(
  5.       c(1, 3, 1,
  6.         1, 3, 2,
  7.         1, 3, 3,
  8.         1, 2, 3,
  9.         1, 1, 3,
  10.         2, 1, 3,
  11.         3, 1, 3,
  12.         3, 1, 2,
  13.         3, 1, 1,
  14.         3, 2, 1,
  15.         3, 3, 1,
  16.         2, 3, 1),
  17.       ncol = 3, byrow = TRUE,
  18.       dimnames = list( character(), c("B73", "F1", "Mo17") )
  19.           )
  20.                )

  21. # Dummy data for the inner plot
  22. set.seed(1705171415)

  23. n <- 100

  24. dfDmmy <-
  25.   {
  26.     ang <- runif(n = n, min = 0, max = 2*pi)
  27.    
  28.     rnd <- runif(100)*4
  29.    
  30.     data.frame( x = rnd*sin(ang), y = rnd*cos(ang) )
  31.   }

  32. # Function to convert point into polar coordination and
  33. #   classify into one of the 12 areas
  34. fPlr <- function(x, y)
  35. {
  36.   rnd <- sqrt(x*x + y*y)
  37.   
  38.   ang <- acos( y/sqrt(x*x + y*y) )/pi*180*( (x >= 0)*2 - 1 ) + (x < 0)*360
  39.   # angle between the line connecting the point and the origin and the positive
  40.   #   part of *Y* axis, in [0, 360)
  41.   
  42.   group <- floor(ang/30) + 1
  43.   
  44.   return(group)
  45. }

  46. # Function to produce outer plots
  47. fOutPlot <- function(dv)
  48. {
  49.   # oldPar <- par(no.readonly = TRUE)
  50.   
  51.   # on.exit( par(oldPar) )
  52.   
  53.   # par( mar = c(2, 0, 0, 0), pty = "s" )
  54.   
  55.   dvLmt <-
  56.     c( round( min( c( length(dv), dv ) ) ) - 1,
  57.        round( max( c( length(dv), dv ) ) ) + 1 )
  58.   
  59.   plot( NA, type = "n", xlim = dvLmt, ylim = dvLmt,
  60.         xlab = NA_character_, ylab = NA_character_,
  61.         asp = 1, axes = FALSE, xaxs = "i", yaxs = "i")
  62.       
  63.   abline( h = do.call( ":", lapply(dvLmt, I) ) )
  64.   
  65.   abline( v = do.call( ":", lapply(dvLmt, I) ) )
  66.   
  67.   # axis( side = 1, at = seq_along(dv), labels = names(dv),
  68.   #       tick = FALSE, mgp = c(1, 0, 0), cex.axis = 0.8, padj = 1 )
  69.   
  70.   mtext( text = names(dv), side = 1, line = 0.5, cex = 2/3,
  71.          at = seq_along(dv), adj = 0.5, padj = 0 )
  72.   
  73.   points( seq_along(dv), dv, type = "o", lwd = 2, pch = 16, cex = 2 )
  74. }

  75. # Map the sequnce of lines to plot
  76. ivSq <- c(12, 1, 2, 11, 3, 10, 4, 9, 5, 8, 7, 6)

  77. # Index for lines in dfOuter to plot
  78. iIndx <- 1L

  79. # Split the device
  80. # iNF <-
  81.   layout(
  82.     mat =
  83.       matrix(
  84.         c( 1,  1,  2,  2,  3,  4,  4,  5,  6,  6,  7,  7,
  85.            1,  1,  2,  2,  3,  4,  4,  5,  6,  6,  7,  7,
  86.            8,  8, 25, 25, 25, 25, 25, 25, 25, 25,  9,  9,
  87.            8,  8, 26, 26, 26, 26, 26, 26, 26, 26,  9,  9,
  88.           10, 10, 26, 26, 26, 26, 26, 26, 26, 26, 11, 11,
  89.           10, 10, 26, 26, 26, 26, 26, 26, 26, 26, 11, 11,
  90.           12, 12, 26, 26, 26, 26, 26, 26, 26, 26, 13, 13,
  91.           12, 12, 26, 26, 26, 26, 26, 26, 26, 26, 13, 13,
  92.           14, 14, 26, 26, 26, 26, 26, 26, 26, 26, 15, 15,
  93.           14, 14, 26, 26, 26, 26, 26, 26, 26, 26, 15, 15,
  94.           16, 16, 26, 26, 26, 26, 26, 26, 26, 26, 17, 17,
  95.           16, 16, 19, 19, 20, 21, 21, 22, 23, 23, 17, 17,
  96.           18, 18, 19, 19, 20, 21, 21, 22, 23, 23, 24, 24
  97.           ),
  98.         ncol = 12, byrow = TRUE
  99.             )
  100.         )

  101. # layout.show(iNF)

  102. # Decrease margin size
  103. oldPar <- par(no.readonly = TRUE)
  104.   
  105. on.exit( par(oldPar) )

  106. par( mar = c(2, 0, 0, 0), pty = "s" )

  107. # Area  1
  108. plot.new()

  109. # Area  2
  110. # plot.new()

  111. fOutPlot( unlist( dfOuter[ ivSq[iIndx], ] ) )

  112. if( iIndx < nrow(dfOuter) ) iIndx <- iIndx + 1L

  113. # Area  3
  114. plot.new()

  115. # Area  4
  116. # plot.new()
  117. fOutPlot( unlist( dfOuter[ ivSq[iIndx], ] ) )

  118. if( iIndx < nrow(dfOuter) ) iIndx <- iIndx + 1L

  119. # Area  5
  120. plot.new()

  121. # Area  6
  122. # plot.new()

  123. fOutPlot( unlist( dfOuter[ ivSq[iIndx], ] ) )

  124. if( iIndx < nrow(dfOuter) ) iIndx <- iIndx + 1L

  125. # Area  7
  126. plot.new()

  127. # Area  8
  128. # plot.new()

  129. fOutPlot( unlist( dfOuter[ ivSq[iIndx], ] ) )

  130. if( iIndx < nrow(dfOuter) ) iIndx <- iIndx + 1L

  131. # Area  9
  132. # plot.new()

  133. fOutPlot( unlist( dfOuter[ ivSq[iIndx], ] ) )

  134. if( iIndx < nrow(dfOuter) ) iIndx <- iIndx + 1L

  135. # Area 10
  136. plot.new()

  137. # Area 11
  138. plot.new()

  139. # Area 12
  140. # plot.new()

  141. fOutPlot( unlist( dfOuter[ ivSq[iIndx], ] ) )

  142. if( iIndx < nrow(dfOuter) ) iIndx <- iIndx + 1L

  143. # Area 13
  144. # plot.new()

  145. fOutPlot( unlist( dfOuter[ ivSq[iIndx], ] ) )

  146. if( iIndx < nrow(dfOuter) ) iIndx <- iIndx + 1L

  147. # Area 14
  148. plot.new()

  149. # Area 15
  150. plot.new()

  151. # Area 16
  152. # plot.new()

  153. fOutPlot( unlist( dfOuter[ ivSq[iIndx], ] ) )

  154. if( iIndx < nrow(dfOuter) ) iIndx <- iIndx + 1L

  155. # Area 17
  156. # plot.new()

  157. fOutPlot( unlist( dfOuter[ ivSq[iIndx], ] ) )

  158. if( iIndx < nrow(dfOuter) ) iIndx <- iIndx + 1L

  159. # Area 18
  160. plot.new()

  161. # Area 19
  162. # plot.new()

  163. fOutPlot( unlist( dfOuter[ ivSq[iIndx], ] ) )

  164. if( iIndx < nrow(dfOuter) ) iIndx <- iIndx + 1L

  165. # Area 20
  166. plot.new()

  167. # Area 21
  168. # plot.new()

  169. fOutPlot( unlist( dfOuter[ ivSq[iIndx], ] ) )

  170. if( iIndx < nrow(dfOuter) ) iIndx <- iIndx + 1L

  171. # Area 22
  172. plot.new()

  173. # Area 23
  174. # plot.new()

  175. fOutPlot( unlist( dfOuter[ ivSq[iIndx], ] ) )

  176. if( iIndx < nrow(dfOuter) ) iIndx <- iIndx + 1L

  177. # Area 24
  178. plot.new()

  179. # Area 25
  180. # plot.new()
  181. par(pty = "m")

  182. plot(
  183.   NA, type = "n", xlim = c(0, 1), ylim = c(0, 1),
  184.   xlab = NA_character_, ylab = NA_character_, axes = FALSE
  185.     )

  186. text(
  187.   x = mean( par("usr")[1:2] ), y = mean( par("usr")[3:4] ),
  188.   labels = "2D presentation of 3-line mean pattern",
  189.   cex = par("cex.main"), font = par("font.main"), xpd = TRUE
  190.     )

  191. # Area 26
  192. # plot.new()
  193. par(pty = "s")

  194. plot(
  195.   NA, type = "n", xlim = c(-4, 4), ylim = c(-4, 4), xaxs = "r", yaxs = "r",
  196.   xlab = NA_character_, ylab = NA_character_, axes = F
  197.     )

  198. box()

  199. # Reference lines
  200. invisible(
  201.   lapply(
  202.     seq.int(3) - 1,
  203.     function(i)
  204.     {
  205.       ang <- i/3*pi
  206.       
  207.       lines( x = c(-4, 4)*cos(ang), y = c(-4, 4)*sin(ang), lty = 1 )
  208.     }
  209.         )
  210.          )

  211. invisible(
  212.   lapply(
  213.     seq.int(3) - 1,
  214.     function(i)
  215.     {
  216.       ang <- i/3*pi + pi/6
  217.       
  218.       lines( x = c(-4, 4)*cos(ang), y = c(-4, 4)*sin(ang), lty = 8 )
  219.     }
  220.         )
  221.          )

  222. # Reference circles
  223. invisible(
  224.   lapply(
  225.     seq.int(4),
  226.     function(i)
  227.     {
  228.       ang <- seq.int(360)
  229.       
  230.       # points( i*cos(ang/180*pi), i*sin(ang/180*pi), pch = ".",
  231.       #         cex = 1, col = "#BFBFBF" )
  232.       
  233.       lines( i*cos(ang/180*pi), i*sin(ang/180*pi), lty = "14",
  234.               col = "#BFBFBF" )
  235.     }
  236.         )
  237.          )

  238. # Text for each reference line
  239. svText <-
  240.   do.call(
  241.     "c",
  242.     lapply(
  243.       seq( nrow(dfOuter) ),
  244.       function(i)
  245.       {
  246.         ivOdr <- order( unlist( dfOuter[i, ] ) )
  247.         paste0(
  248.           do.call(
  249.             "paste0",
  250.             lapply(
  251.               seq.int( length(ivOdr) - 1 ),
  252.               function(j)
  253.                 paste0(
  254.                   names(dfOuter)[ ivOdr[j] ],
  255.                   ifelse(
  256.                     dfOuter[ i, ivOdr[j] ] == dfOuter[ i, ivOdr[j + 1] ],
  257.                     "=", "<"
  258.                         )
  259.                       )
  260.                   )
  261.                  ),
  262.           names(dfOuter)[ tail(ivOdr, 1) ]
  263.               )
  264.       }
  265.           )
  266.          )

  267. invisible(
  268.   lapply(
  269.     seq_along(svText),
  270.     function(i)
  271.     {
  272.       rnd <- 4L
  273.       
  274.       ang <- (i - 1)*pi/6
  275.       
  276.       dX <- rnd*cos(ang)
  277.       
  278.       dY <- rnd*sin(ang)
  279.       
  280.       # Enough space to the border?
  281.       bdr <-
  282.         abs( dX + sign(dX)*( strwidth(svText) + par("cxy")[1] ) ) >
  283.         max( abs( par("usr")[1:2] ) )
  284.       
  285.       text(
  286.         x =
  287.           ifelse(
  288.             bdr,
  289.             ifelse( dX > 0, par("usr")[2] - par("cxy")[1],
  290.                     par("usr")[1] + par("cxy")[1] ),
  291.             dX
  292.                 ),
  293.         y = dY + ifelse( dY > 0, par("cxy")[2]/2, -1*par("cxy")[2]/2 ),
  294.         labels = svText[i],
  295.         adj =
  296.           c( ifelse( bdr, as.integer(dX > 0), 0.5 ), ifelse(dY > 0, 0, 1) )
  297.           )
  298.     }
  299.        )
  300.          )

  301. # Number for each region
  302. invisible(
  303.   lapply(
  304.     seq.int(12),
  305.     function(i)
  306.     {
  307.       ang <- (i/6 - 1/12)*pi
  308.       
  309.       rnd <-
  310.         min(
  311.           abs(
  312.             c( 4.5,
  313.                (par("usr")[1:2] + c(1, -1)*par("cxy")[1])/sin(ang),
  314.                (par("usr")[3:4] + c(1, -1)*par("cxy")[2])/cos(ang) )
  315.              )
  316.            )
  317.       
  318.       text( x = rnd*sin(ang), y = rnd*cos(ang),
  319.             labels = as.character(i), adj = c(0.5, 0.5) )
  320.     }
  321.         )
  322.          )

  323. # Finally add the points
  324. with( dfDmmy, points( x, y, pch = 20, col = rainbow(12)[ fPlr(x, y) ] ) )
复制代码


Output:
ThreeLineMean.png

使用道具

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

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

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

GMT+8, 2024-4-27 01:45