Tsukuba.R#9ã§ã®çºè¡¨ã«åãã¦ggplot2ã®åå¼·ãå§ããï¼ããã¾ã§èª¿ã¹ããã¨ã軽ãã¾ã¨ãã¦ã¿ãï¼
ggplot2ã¨ã¯
é«åº¦ãª2Dã°ã©ãã£ãã¯ã®æç»ã«ã¤ãã¦çµ±ä¸çãªæä½ãæä¾ãã¦ãããã©ã¤ãã©ãªï¼ãThe Grammar of Graphicsãã¨ããææ³ã«åºã¥ãã¦ãã¦ï¼ã¦ã¼ã¶ããã¼ã¿ã®ã©ã®é¨åã«æ³¨ç®ãããããè¨è¿°ãã¦ããã ãã§ç°¡åã«ããããªã°ã©ããå¾ãããããã«ãªã£ã¦ããï¼
ggplot2ã®ã¤ã³ã¹ãã¼ã«
ãã¤ãéãï¼
> install.packages("ggplot2")
ggplot2ã§å³ãæç»ãã
ggplot2ã§å³ãæç»ããæ¹æ³ã¯ä¸»ã«ä»¥ä¸ã®2éãï¼
- qploté¢æ°ã使ã
- èªåãå¾ããã°ã©ãã«åããã¦ã¬ã¤ã¤ã¼ãªãã¸ã§ã¯ããçµã¿åããã
ããã§ã¯ä»¥ä¸ã®ggplot2ããã±ã¼ã¸ä»å±ã®diamondsãµã³ãã«ãã¼ã¿ã使ãï¼ããããã®æ¹æ³ã§ããã¤ãå³ãæç»ãã¦ã¿ãï¼
> library(ggplot2) > head(diamonds) carat cut color clarity depth table price x y z 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31 4 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48 > dim(diamonds) [1] 53940 10
ãã®ãã¼ã¿ã®è©³ç´°ãç¥ãããå ´åã¯ãã«ããåç §ãããï¼
> ?diamonds
qploté¢æ°ã使ã
ggplot2ã«ã¯qploté¢æ°(Quick PLOT)ãç¨æããã¦ããï¼ããã¯Rã®ploté¢æ°ã¨ä¼¼ãã¤ã³ã¿ã¼ãã§ã¤ã¹ãæã£ã¦ãã¦ï¼å¼æ°ãæå®ãã¦ãããã¨ã§ä»»æã®å³ãæç»ãããã¨ãã§ããããã«ãªã£ã¦ããï¼
以ä¸ã«qploté¢æ°ã使ã£ãå ´åã®ä¾ã示ãï¼
> qplot(carat, price, data = diamonds, colour = clarity, log = "xy")
> qplot(depth, data = diamonds, binwidth = 0.2, xlim = c(55, 70), facets = ~ cut)
å¤(é¢æ£å¤ï¼é£ç¶å¤)ã¨è²ã®å¯¾å¿é¢ä¿ãã°ã©ãã®å¡ä¾ãªã©ã¯qploté¢æ°ããããªã«ãã£ã¦ãããï¼
Rã®ploté¢æ°ã«æ £ãã¦ãã¦ï¼ãã¤ggplot2ãåãã¦è§¦ããããªäººã«ã¯ããªãæ±ããããã®ã§ã¯ãªããã¨æãï¼ãããï¼æç»ããã°ã©ããè¤éã§å¼æ°ãå¤ããªãå ´åã¯ï¼æ¬¡ã«èª¬æããæ¹æ³ãæ¤è¨ãã¦ã¿ãæ¹ããããããããªãï¼
èªåãå¾ããã°ã©ãã«åããã¦ã¬ã¤ã¤ã¼ãªãã¸ã§ã¯ããçµã¿åããã
ãã¡ãã®æ¹ãggplot2ã®æ¬é ã¨è¨ã£ã¦ããã ããï¼
ãã®æ¹æ³ã§ã¯ï¼ggploté¢æ°ã§çæããggplotãªãã¸ã§ã¯ãã«ã¬ã¤ã¤ã¼ãªãã¸ã§ã¯ããçµã¿åããã¦ãããã¨ã§å³ã®æç»ãè¡ãï¼åã¬ã¤ã¤ã¼ãªãã¸ã§ã¯ãã¯ããããå³ã«ããã以ä¸ã®æ§æè¦ç´ ã表ç¾ããï¼
- Geoms
- Statistics
- Scales
- Coordinate systems
- Faceting
- Position adjustments
ããã®è©³ç´°ã«ã¤ãã¦ã¯Tsukuba.R#9ã®çºè¡¨ã§è©±ãã§ããã°ã¨æãï¼
以ä¸ã«ã¬ã¤ã¤ã¼ã使ã£ãå ´åã®ä¾ã示ãï¼
> ggplot(diamonds, aes(x = carat, y = price)) + geom_point(aes(colour = clarity)) + scale_x_log10() + scale_y_log10()
> ggplot(diamonds, aes(x = depth)) + geom_histogram(binwidth = 0.2) + xlim(55, 70) + facet_wrap(~ cut)
qploté¢æ°ã®æã¨å ¨ãåãå³ãæç»ãã¦ããï¼ãã¼ã¿ããçæããggplotãªãã¸ã§ã¯ãã«å¯¾ãã¦ã¬ã¤ã¤ã¼ãªãã¸ã§ã¯ãã追å ãã¦ãããã¨ã§å³ãä½æãã¦ããã®ãä¸ã®ä¾ããåããã¨æãï¼
ã¾ãï¼ä»¥ä¸ã®ããã«ggplotãªãã¸ã§ã¯ããå¤æ°ã«æ ¼ç´ãã¦ããã¬ã¤ã¤ã¼ã追å ãã¦ããï¼ã°ã©ããå°ããã¤ä½ãä¸ãã¦ãããã¨ãã§ããï¼
> d <- ggplot(diamonds, aes(x = depth)) > d <- d + geom_histogram(binwidth = 0.2) > print(d) > d <- d + xlim(55, 70) > print(d) > d <- d + facet_wrap(~ cut) > print(d)
ã¬ã¤ã¤ã¼ãªãã¸ã§ã¯ããçæããé¢æ°ã®ååã¯ã°ã©ãã®æ§æè¦ç´ ãç´æ¥è¡¨ãã¦ããï¼åé¢æ°ã«ã¯ä¸è²«ããå½åè¦å(geom_Aãscale_A_Bãªã©)ãããã®ã§è¦ããããï¼ã¾ãï¼æ§æè¦ç´ ã®ä¸é¨ãå¤æ´ãã¦ã°ã©ããå°ãã ãä¿®æ£ããã¨ãã£ããã¨ãæ軽ã«è¡ããï¼ãããï¼ã³ã¼ããæ¯è¼çåé·ã«ãªã£ã¦ãã¾ãå¾åãããããï¼ç°¡åãªå³ãæç»ããå ´åã¯qploté¢æ°ã®æ¹ãæ±ããããã ããï¼
以ä¸
ggplot2ã§å³ãæç»ããã«ã¯ï¼æ軽ã«ä½¿ããqploté¢æ°ã使ãæ¹æ³ã¨ï¼ã¬ã¤ã¤ã¼ãªãã¸ã§ã¯ããéãåããã¦ããæ¹æ³ã®2ã¤ãããï¼ããããä¸é·ä¸çãªã®ã§ï¼ãã¾ã使ãåãã¦ããã¨ããã ããï¼
åèè³æ
- ggplot2
- æ¬å®¶ï¼åãªã½ã¼ã¹ã¸ã®ãªã³ã¯ããªãã¡ã¬ã³ã¹ããï¼
- 一粒で3回おいしいggplot2 - Slideshare
- id:syou6162ã®Tsukuba.R#6ã®çºè¡¨è³æï¼
- http://blip.tv/file/3362248
- ggplot2ä½è ã«ããè¬ç¾©ã®é²ç»ï¼