본문 바로가기

Data Analytics

R 기본 예제 돌려보기 20131212

require(stats)

plot(cars)

lines(lowess(cars))


plot(sin, -pi, 2*pi) # see ?plot.function


## Discrete Distribution Plot:

plot(table(rpois(100, 5)), type = "h", col = "red", lwd = 10,

     main = "rpois(100, lambda = 5)")


## Simple quantiles/ECDF, see ecdf() {library(stats)} for a better one:

plot(x <- sort(rnorm(47)), type = "s", main = "plot(x, type = \"s\")")

points(x, cex = .5, col = "dark red")



> install.packages("ggplot2")

> library(ggplot2)

Find out what's changed in ggplot2 with

news(Version == "0.9.3.1", package = "ggplot2")


> p <- qplot(wt, mpg, colour = hp, data = mtcars)

> p

> mtcars


Examples

### Adding objects to a ggplot object

p <- qplot(wt, mpg, colour = hp, data = mtcars)

p + coord_cartesian(ylim = c(0, 40))

p + scale_colour_continuous(breaks = c(100, 300))

p + guides(colour = "colourbar")

# Use a different data frame

m <- mtcars[1:10, ]

p %+% m

### Adding objects to a theme object

# Compare these results of adding theme objects to other theme objects

add_el <- theme_grey() + theme(text = element_text(family = "Times"))

rep_el <- theme_grey() %+replace% theme(text = element_text(family = "Times"))

add_el$text

rep_el$text


df <- data.frame( foo = 1:7 )  // 1 ~ 7 을 foo 로 넣겠다

> df <- data.frame(foo = 1:3, bar = -1:-9)

> df

  foo bar

1   1  -1

2   2  -2

3   3  -3

4   1  -4

5   2  -5

6   3  -6

7   1  -7

8   2  -8

9   3  -9


mean> x <- c(0:10, 50)

mean> xm <- mean(x)

mean> c(xm, mean(x, trim = 0.10))


필요한 문서를 찾자

> ??pattern

library 가 load 되지 않은 상황에서 찾으려고 한다면

> help.search("mean")



> pi

[1] 3.141593

> sqrt(2)

[1] 1.414214

> sqrt(9)

[1] 3

> print(matrix(c(1,2,3,4), 2,2))

     [,1] [,2]

[1,]    1    3

[2,]    2    4

> print(matrix(c(1,2,3,4), 2,4))

     [,1] [,2] [,3] [,4]

[1,]    1    3    1    3

[2,]    2    4    2    4

> print(matrix(c(1,2,3,4), 4,2))

     [,1] [,2]

[1,]    1    1

[2,]    2    2

[3,]    3    3

[4,]    4    4

> print(list("j", "a", "c", "o", "b"))

[[1]]

[1] "j"


[[2]]

[1] "a"


[[3]]

[1] "c"


[[4]]

[1] "o"


[[5]]

[1] "b"



> ls()

 [1] "add_el"             "ch"                

 [3] "channel"            "cs"                

 [5] "df"                 "draw.plotmath.cell"

 [7] "draw.title.cell"    "get.c"             

 [9] "get.r"              "i"                 

[11] "m"                  "make.table"        

[13] "nc"                 "nr"                

[15] "oldpar"             "p"                 

[17] "rep_el"             "x"                 

[19] "xm"        

> ls.str()

add_el : List of 38

 $ line                :List of 4

 $ rect                :List of 4

 $ text                :List of 8

 $ axis.text           :List of 8

 $ strip.text          :List of 8

 $ axis.line           : list()

 $ axis.text.x         :List of 8

 $ axis.text.y         :List of 8

 $ axis.ticks          :List of 4

 $ axis.title.x        :List of 8

 $ axis.title.y        :List of 8

 $ axis.ticks.length   :Class 'unit'  atomic [1:1] 0.15

 $ axis.ticks.margin   :Class 'unit'  atomic [1:1] 0.1

 $ legend.background   :List of 4

 $ legend.margin       :Class 'unit'  atomic [1:1] 0.2

 $ legend.key          :List of 4

 $ legend.key.size     :Class 'unit'  atomic [1:1] 1.2

 $ legend.key.height   : NULL

 $ legend.key.width    : NULL

 $ legend.text         :List of 8

 $ legend.text.align   : NULL

 $ legend.title        :List of 8

 $ legend.title.align  : NULL

 $ legend.position     : chr "right"

 $ legend.direction    : NULL

 $ legend.justification: chr "center"

 $ legend.box          : NULL

 $ panel.background    :List of 4

 $ panel.border        : list()

 $ panel.grid.major    :List of 4

 $ panel.grid.minor    :List of 4

 $ panel.margin        :Class 'unit'  atomic [1:1] 0.25

 $ strip.background    :List of 4

 $ strip.text.x        :List of 8

 $ strip.text.y        :List of 8

 $ plot.background     :List of 4

 $ plot.title          :List of 8

 $ plot.margin         :Class 'unit'  atomic [1:4] 1 1 0.5 0.5

ch : Class 'RODBC'  atomic [1:1] 1

channel :  int -1

cs : 'data.frame': 10 obs. of  16 variables:

 $ MOVIE_NAME    : Factor w/ 10 levels "26년","다크 나이트 라이즈",..: 1 7 6 5 8 2 4 10 3 9

 $ B_TIMING      : Factor w/ 1 level "before": 1 1 1 1 1 1 1 1 1 1

 $ B_NUM_COMM    : int  15747 12909 3275 3136 1690 1675 1458 1415 1406 1344

 $ A_TIMING      : Factor w/ 1 level "after": 1 1 1 1 1 1 1 1 1 1

 $ A_NUM_COMM    : int  51268 35763 13817 2012 3990 22878 1513 13044 8768 5187

 $ AVG_COMM      : num  6.32 7.15 8.91 7.06 6.4 ...

 $ DIRECTOR      : Factor w/ 10 levels "김용한","김지훈",..: 9 7 5 8 4 10 3 2 1 6

 $ PRODUCTION    : Factor w/ 7 levels "(주)노마드필름",..: 7 4 NA 2 1 NA 5 6 3 NA

 $ DISTRIBUTOR   : Factor w/ 9 levels "(주)나이너스엔터테인먼트",..: 4 7 6 5 5 9 1 8 3 2

 $ RELEASE_DATE  : Factor w/ 10 levels "2012-07-19","2012-11-15",..: 4 10 7 8 2 1 9 5 3 6

 $ NATIONALITY   : Factor w/ 2 levels "미국","한국": 2 2 1 2 2 1 2 2 2 1

 $ AUDIENCE_ALL  : int  2940475 6953843 9001352 334323 210278 6396528 47105 2014589 972962 449077

 $ AUDIENCE_SEOUL: int  811194 1691033 2595298 81804 62744 2057529 17164 473037 219933 173381

 $ SCREEN_NO_ALL : int  604 1341 1381 385 300 1210 172 617 444 382

 $ GENRE         : Factor w/ 9 levels "SF,판타지","드라마",..: 5 8 7 3 9 6 2 2 4 1

 $ VIEWING_CLASS : Factor w/ 4 levels "12세이상관람가",..: 2 2 1 3 2 2 1 1 2 4

df : 'data.frame': 9 obs. of  2 variables:

 $ foo: int  1 2 3 1 2 3 1 2 3

 $ bar: int  -1 -2 -3 -4 -5 -6 -7 -8 -9

draw.plotmath.cell : function (expr, i, nr, string = NULL)  

draw.title.cell : function (title, i, nr)  

get.c : function (i, nr)  

get.r : function (i, nr)  

i :  num 40

m : 'data.frame': 10 obs. of  11 variables:

 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2

 $ cyl : num  6 6 4 6 8 6 8 4 4 6

 $ disp: num  160 160 108 258 360 ...

 $ hp  : num  110 110 93 110 175 105 245 62 95 123

 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92

 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...

 $ qsec: num  16.5 17 18.6 19.4 17 ...

 $ vs  : num  0 0 1 1 0 1 0 1 1 1

 $ am  : num  1 1 1 0 0 0 0 0 0 0

 $ gear: num  4 4 4 3 3 3 3 4 4 4

 $ carb: num  4 4 1 1 2 1 4 2 2 4

make.table : function (nr, nc)  

nc :  num 2

nr :  num 20

oldpar : List of 2

 $ mar: num [1:4] 5.1 4.1 4.1 2.1

 $ pty: chr "m"

p : List of 9

 $ data       :'data.frame': 32 obs. of  11 variables:

 $ layers     :List of 1

 $ scales     :Reference class 'Scales' [package "ggplot2"] with 1 fields

  ..and 21 methods, of which 9 are possibly relevant

 $ mapping    :List of 3

 $ theme      : list()

 $ coordinates:List of 1

 $ facet      :List of 1

 $ plot_env   :<environment: R_GlobalEnv> 

 $ labels     :List of 3

rep_el : List of 38

 $ line                :List of 4

 $ rect                :List of 4

 $ text                :List of 8

 $ axis.text           :List of 8

 $ strip.text          :List of 8

 $ axis.line           : list()

 $ axis.text.x         :List of 8

 $ axis.text.y         :List of 8

 $ axis.ticks          :List of 4

 $ axis.title.x        :List of 8

 $ axis.title.y        :List of 8

 $ axis.ticks.length   :Class 'unit'  atomic [1:1] 0.15

 $ axis.ticks.margin   :Class 'unit'  atomic [1:1] 0.1

 $ legend.background   :List of 4

 $ legend.margin       :Class 'unit'  atomic [1:1] 0.2

 $ legend.key          :List of 4

 $ legend.key.size     :Class 'unit'  atomic [1:1] 1.2

 $ legend.key.height   : NULL

 $ legend.key.width    : NULL

 $ legend.text         :List of 8

 $ legend.text.align   : NULL

 $ legend.title        :List of 8

 $ legend.title.align  : NULL

 $ legend.position     : chr "right"

 $ legend.direction    : NULL

 $ legend.justification: chr "center"

 $ legend.box          : NULL

 $ panel.background    :List of 4

 $ panel.border        : list()

 $ panel.grid.major    :List of 4

 $ panel.grid.minor    :List of 4

 $ panel.margin        :Class 'unit'  atomic [1:1] 0.25

 $ strip.background    :List of 4

 $ strip.text.x        :List of 8

 $ strip.text.y        :List of 8

 $ plot.background     :List of 4

 $ plot.title          :List of 8

 $ plot.margin         :Class 'unit'  atomic [1:4] 1 1 0.5 0.5

x :  num [1:12] 0 1 2 3 4 5 6 7 8 9 ...

xm :  num 46.2