ggplot2ã®qplot颿°ã®ã¾ã¨ã
Rã®ã¹ããã°ã©ãã£ãã¯ã¹ã©ã¤ãã©ãªãggplot2ãã®qplot颿°ã«ã¤ãã¦ã¾ã¨ãã¦ã¿ãï¼å°ãé å¼µããããï¼
é¢é£è¨äºï¼ggplot2の概要 - ぬいぐるみライフ?
qplot颿°ã¨ã¯
ggplot2ã«ã¯qplot(Quick PLOTã®ç¥)ã¨ããã¹ãããªé¢æ°ãç¨æããã¦ããï¼ããã使ãã¨ï¼ããã©ã«ãã®è¨å®ã®ã¾ã¾ã§ãRã®baseã©ã¤ãã©ãª(plot颿°ãªã©)ã¨æ¯ã¹ã¦ããããªå³ãæããä¸ã«ï¼è²ä»ããå¡ä¾ã®è¨å®ãããç¨åº¦ãããªã«ãã£ã¦ãããï¼
以éï¼ggplot2ã®qplot颿°ã«ã¤ãã¦è©³ããè¦ã¦ãããã¨ã«ãããï¼
ãã®è¨äºã®æ§æ
æåã«ï¼qplot颿°ã«ã¤ãã¦ä»¥ä¸ã®ãã¨ããã§ãã¯ããï¼
- qplot颿°ã®åºæ¬
- qplot颿°ã®dataãã©ã¡ã¼ã¿
次ã«ï¼ä»¥ä¸ã®ã°ã©ãã«ã¤ãã¦baseã©ã¤ãã©ãªã¨ggplot2ã®qplot颿°ã®éã§ã·ã³ã¿ãã¯ã¹ã®éããæç»ãããã°ã©ããæ¯è¼ãã¦ããï¼
- æ£å¸å³
- æãç·ã°ã©ã
- æ£ã°ã©ã
- ãã¹ãã°ã©ã
- å¯åº¦ãããã
- ç®±ã²ãå³
- æ¡ä»¶ä»ããããã
æå¾ã«baseã©ã¤ãã©ãªã®ä½æ°´æºä½å³é¢æ°ã¨ggplot2ã®ã¬ã¤ã¤ã¼ã®å¯¾å¿é¢ä¿ã確èªããï¼
- 使°´æºä½å³é¢æ°ã¨ã¬ã¤ã¤ã¼
ã¾ãï¼ãã®è¨äºã§ã¯ggplot2ããã±ã¼ã¸ä»å±ã®diamondsãã¼ã¿ã¨economicsãã¼ã¿ããµã³ãã«ã¨ãã¦ä½¿ã£ã¦ããï¼
> 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) > head(economics) date pce pop psavert uempmed unemploy 1 1967-06-30 507.8 198712 9.8 4.5 2944 2 1967-07-31 510.9 198911 9.8 4.7 2945 3 1967-08-31 516.7 199113 9.0 4.6 2958 4 1967-09-30 513.3 199311 9.8 4.9 3143 5 1967-10-31 518.5 199498 9.7 4.7 3066 6 1967-11-30 526.2 199657 9.4 4.8 3018 > dim(economics) [1] 478 6
åãµã³ãã«ãã¼ã¿ã®è©³ç´°ã«ã¤ãã¦ã¯ãã«ããåç §ï¼
> ?diamonds > ?economics
qplot颿°ã®åºæ¬
qplot颿°ã¯baseã©ã¤ãã©ãªã®plot颿°ã®ãããªæè¦ã§ä½¿ããã¨ãã§ããï¼
> qplot(carat, price, data = diamonds, colour = clarity)
è²ã¨ãã¼ã¿ã®å¯¾å¿ä»ãã軸ã®ã©ãã«ãªã©ã¯qplot颿°ããããªã«ãã£ã¦ãããï¼
> qplot(carat, data = diamonds, geom = "density", colour = cut)
ãã¼ãï¼ããããªå³ï¼
(TODO:è²ãªã©ã®æå®æ¹æ³ã«ã¤ãã¦æ¸ã)
qplot颿°ã®dataãã©ã¡ã¼ã¿
Rã§ä½å³ããéã«ï¼æç»å¯¾è±¡ã®ãã¼ã¿ããã¼ã¿ãã¬ã¼ã ã§ããã±ã¼ã¹ã¯ããªãå¤ãï¼ãã¼ã¿ãã¬ã¼ã ã®ååã¯$ã使ããã¨ã§ç°¡åã«ã¢ã¯ã»ã¹ã§ããï¼
> 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 > head(diamonds$price) [1] 326 326 327 334 335 336
ãããï¼ãã¼ã¿ãã¬ã¼ã ã®åãdiamonds$ãã®ããã«æå®ãã¤ã¤qplot颿°ã§ã°ã©ããä½ããã¨ããã¨ï¼ä»¥ä¸ã®ããã«ã³ã¼ããåé·ã«ãªãï¼èªåçã«ã¤ãããã軸ã®ã©ãã«ãå¡ä¾ã«"diamonds$"ãã¤ãã¦ç ©ããããªãã¨ãã£ãåé¡ãçºçããï¼
> qplot(diamonds$carat, diamonds$price, colour = diamonds$clarity)
qplot颿°ã®dataãã©ã¡ã¼ã¿ã使ãã¨ï¼ä»¥ä¸ã®ããã«ãã¼ã¿ãã¬ã¼ã ã®åã®ååã ãã§ãããããããã¼ã¿ãæå®ã§ããããã«ãªãï¼ãããããã¨ã§åºæ¥ä¸ãã£ãå³ã®ã©ãã«ãå¡ä¾ããã£ããããï¼
> qplot(carat, price, data = diamonds, colour = clarity)
qplot颿°ã§ãã¼ã¿ãã¬ã¼ã ãã°ã©ãåããå ´åã¯ï¼dataãã©ã¡ã¼ã¿ãç©æ¥µçã«ä½¿ã£ã¦ããããã«ããæ¹ãããã ããï¼
æ£å¸å³
以éï¼æ§ã ãªå³ã«ã¤ãã¦baseã©ã¤ãã©ãªã¨qplot颿°ã®å ·ä½ä¾ã確èªããªããæ¯è¼ãã¦ãããï¼
æ£å¸å³ã¯ãã¼ã¿ãç¹ã§å¹³é¢ä¸ã«ããããããã ãã®åºæ¬çãªå³ã ï¼
plot(x, y) # base qplot(x, y) # ggplot2
å ·ä½ä¾ï¼
> plot(diamonds$carat, diamonds$price) > qplot(carat, price, data = diamonds)
æãç·ã°ã©ã
æãç·ã°ã©ãã¯æç³»åãã¼ã¿ã®å¯è¦åã®éã«ãã使ãããï¼
plot(x, y, type = "l") # base qplot(x, y, geom = "line") # ggplot2
å ·ä½ä¾ï¼
> plot(economics$date, economics$unemploy, type = "l") > qplot(date, unemploy, data = economics, geom = "line")
æ£ã°ã©ã
ããªãã¿ã®æ£ã°ã©ãï¼
barplot(x) # base qplot(names(x), x, geom = "bar", stat = "identity") # ggplot2
å¤ã®é »åº¦ãæ£ã°ã©ãã§è¡¨ç¤ºããå ´åã¯ä»¥ä¸ã®éãï¼
barplot(table(x)) # base qplot(x, geom = "bar") # ggplot2
xã颿£å¤ã®å ´åï¼qplot颿°ã®geom = "bar"ã¯çç¥ã§ããï¼
å ·ä½ä¾ï¼
> table(diamonds$color) D E F G H I J 6775 9797 9542 11292 8304 5422 2808 > barplot(table(diamonds$color)) > qplot(color, data = diamonds)
ãã¹ãã°ã©ã
ãã¼ã¿ã®åº¦æ°åå¸ãå¯è¦åããå³ï¼
hist(x) # base qplot(x, geom = "histogram") # ggplot2
hist颿°ã¯binsã§ï¼qplot颿°ã¯binwidthã§ãã¹ãã°ã©ã ã®å¹ ã調ç¯ã§ããï¼
hist(x, bins = 100) qplot(x, geom = "histogram", binwidth = 1)
xãé£ç¶å¤ã®å ´åï¼qplot颿°ã®geom = "histogram"ã¯çç¥ã§ããï¼
å ·ä½ä¾ï¼
> hist(diamonds$price) > qplot(price, data = diamonds)
å¯åº¦ãããã
ãã¹ãã°ã©ã ã¨åãããã¼ã¿ã®åå¸ã示ãå³ï¼ãã¡ãã¯ã«ã¼ãã«å¯åº¦æ¨å®ã使ã£ã¦ããï¼
plot(density(x)) # base qplot(x, geom = "density") # ggplot2
å ·ä½ä¾ï¼
> plot(density(diamonds$price)) > qplot(price, data = diamonds, geom = "density")
ç®±ã²ãå³
è¤æ°ã®ãã¼ã¿ã®åå¸ãæ¯è¼ããã®ã«ä¾¿å©ãªå³ï¼
boxplot(y ~ x) # base qplot(x, y, geom = "boxplot") # ggplot2
å ·ä½ä¾ï¼
> boxplot(diamonds$carat ~ diamonds$cut) > qplot(cut, carat, data = diamonds, geom = "boxplot")
æ¡ä»¶ä»ããããã
æå®ããå¤éã®å¤ã®ç¯å²ãã¨ã«ãã¼ã¿ã®æ£å¸å³ãä½ããã¨ãã§ããï¼
coplot(y ~ x | z) # base qplot(x, y, facets = ~ z) # ggplot2
å ·ä½ä¾ï¼
> coplot(diamonds$price ~ diamonds$carat | diamonds$cut) > qplot(carat, price, data = diamonds, facets = ~ cut)
使°´æºä½å³é¢æ°ã¨ã¬ã¤ã¤ã¼
Rã®baseã©ã¤ãã©ãªã®ä½æ°´æºä½å³é¢æ°ã¯ãããã以ä¸ã®ggplot2ã®ã¬ã¤ã¤ã¼ã¨å¯¾å¿ãã¦ããï¼
base functions | ggplot2 layers |
---|---|
curve() | geom_curve() |
hline() | geom_hline() |
lines() | geom_line() |
points() | geom_point() |
polygon() | geom_polygon() |
rect() | geom_rect() |
rug() | geom_rug() |
segments() | geom_segment() |
text() | geom_text() |
vline() | geom_vline() |
abline(lm(y ~ x)) | geom_smooth(method = "lm") |
lines(density(x)) | geom_density() |
lines(loess(x, y)) | geom_smooth() |
Rã®baseã®ä½æ°´æºä½å³é¢æ°ã¯ï¼å ã«plot颿°ã§ã°ã©ããæç»ãã¦ããå®è¡ãããã¨ã§ã°ã©ãä¸ã«å³å½¢ã追å ããï¼
plot(x, y) lines(x, y)
qplot颿°ã®å ´åã¯ï¼ä»¥ä¸ã®ããã«qplot()ã®æ»ãå¤ã«å¯¾ãã¦ã¬ã¤ã¤ã¼ã追å ãããã¨ã§æç»ã§ããï¼
qplot(x, y) + geom_line()
以ä¸
qplot颿°ã使ãã¨ããã©ã«ãã®ã¾ã¾ã§ãbaseã©ã¤ãã©ãªãããããããªã°ã©ããæããï¼qplot颿°+ã¬ã¤ã¤ã¼ã¨ããå ·åã«ã¤ã³ã¿ã¼ãã§ã¤ã¹ã«ä¸è²«æ§ãããç¹ãéè¦ãªãã¤ã³ãã (barplotãhistã®ãããªä»ã®é¢æ°ãå¼ã³åºãå¿ è¦ãã»ã¨ãã©ãªã)ï¼
ã¾ã ggplot2ã触ã£ããã¨ããªãäººã¯æ¯é次åRã使ãæã«è©¦ãã¦ã¿ããï¼
åèè³æ
- ggplot. had.co.nz
- æ¬å®¶ï¼åãªã½ã¼ã¹ã¸ã®ãªã³ã¯ããªãã¡ã¬ã³ã¹ããï¼
- ggplot2 book
- ggplot2本ã®2ç« (qplot)ã¨Appeldixã®PDFããã¦ã³ãã¼ãã§ããï¼
- Visualising data with R - had - blip.tv
- ggplot2ä½è ã«ããè¬ç¾©ã®é²ç»ï¼