Tuesday, July 16, 2013

R Markdown Quick Reference

Content Weekly Trend
========================================================

### Read-in content raw data

```{r echo=FALSE, results='hide',message=FALSE}
memory.limit()
memory.size(max = TRUE)
rm(list=ls(all=T))

require(data.table)
require(xtable)
require(stringr)
require(ggplot2)

setwd("C:\\Users\\lingh\\Desktop\\Ad effectness\\Y! Index\\Yindex\\Ling\\content awareness");

data <- fread("content_week.csv", head=F, sep=',');
setnames(data, c("brandid", "pv", "wk"));
regexp <- "([[:digit:]]+)"
data$wk1 <- as.integer(str_extract(data$wk, regexp))
str(data)
```

```{r echo=TRUE, results="asis"}
data <- data[,wk:=NULL]
print(xtable(head(data)),type='html')
```

### Plot page views by brand

```{r echo=FALSE, results='hide',message=FALSE}
data1<-data[,sum(pv), by=brandid][order(brandid)]
setnames(data1, c("brandid", "pv"))
brand_meta <- fread("brand_meta.txt", head=F, sep='\t')
setnames(brand_meta, c("brandid", "brand_type"))
setkey(data1, "brandid")
setkey(brand_meta, "brandid")
plot1<-merge(data1,brand_meta)
plot1<-plot1[order(pv)]
plot1$group <- 1:length(plot1$pv)

plot2 <- plot1
plot2 <- transform(plot2, brand_type = factor(brand_type))
plot2 <- transform(plot2, brand_type = reorder(brand_type, rank(group)))
```

```{r echo=TRUE, results="asis"}
print(xtable(plot2),type='html')
```

```{r echo=FALSE, results='hide',message=FALSE, fig.width=20, fig.height=8}
ggplot(plot2, aes(x=brand_type, y=pv)) +
  geom_bar(stat = "identity", fill = "blue") + coord_flip() +
  geom_text(aes(x=brand_type, y=pv, label = paste(round(pv/10^6),"M") , hjust=-.2, face = "bold", size=20)) +
  ggtitle("US Page View Counts") +
  ylab("Page View Counts") + xlab("Brand") +
  theme(plot.title = element_text(face = "bold", size = 20)) +
  theme(axis.text.x = element_text(face = "bold", size = 16)) +
  theme(axis.text.y = element_text(face = "bold", size = 16)) +
  theme(axis.title.x = element_text(face = "bold", size = 16)) +
  theme(axis.title.y = element_text(face = "bold", size = 16, angle = 90)) +
  theme(legend.position = "none")
```


### Plot weekly page views
```{r echo=FALSE, results='hide',message=FALSE}
data2<-data[,sum(pv), by=wk1][order(wk1)]
setnames(data2, c("wk", "pv"))
week_meta <- fread("week_meta.txt", head=F, sep='\t')
setnames(week_meta, c("wk", "week"))
setkey(data2, "wk")
setkey(week_meta, "wk")
plot3<-merge(data2,week_meta)
plot3<-plot3[order(wk)]

plot4 <- plot3
plot4 <- transform(plot4, week = factor(week))
plot4 <- transform(plot4, week = reorder(week, rank(wk)))
```


```{r echo=TRUE, results="asis"}
print(xtable(plot4),type='html')
```

```{r  echo=FALSE, results='hide',message=FALSE, fig.width=20, fig.height=8}
ggplot(plot4, aes(x=week, y=pv)) +
  geom_bar(stat = "identity", fill = "blue")+
  geom_abline(intercept = mean(plot3$pv), color = 'red', size = 1, lty = 3) +
  ggtitle("Daily Page view Counts") +
  ylab("Page Views") + xlab("Week") +
  theme(plot.title = element_text(face = "bold", size = 20)) +
  theme(axis.text.x = element_text(face = "bold", size = 16, angle=-90)) +
  theme(axis.text.y = element_text(face = "bold", size = 16)) +
  theme(axis.title.x = element_text(face = "bold", size = 16)) +
  theme(axis.title.y = element_text(face = "bold", size = 16, angle = 90)) +
  theme(legend.position = "top") +
  theme(legend.key = element_rect(colour = NA)) +
  theme(legend.title = element_blank()) +
  theme(legend.position = "none")
```

Gamma
========================================================

This is an R Markdown document. Markdown is a simple formatting syntax for authoring web pages (click the **MD** toolbar button for help on Markdown).

When you click the **Knit HTML** button a web page will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:


Markdown Basics
-------------------------


Emphasis
-------------------------

*italic*

**bold**

_italic_ 

__bold__


Lists
-------------------------

### Unordered List

* Item 1
* Item 2
  * Item 2a
  * Item 2b

### Ordered List

1. Item 1
2. Item 2
3. Item 3
   * Item 3a
   * Item 3b


Manual Line Breaks
-------------------------

Roses are red,    
Violets are blue. 
To you, 
I always true. 


Links
-------------------------

http://example.com

[example](http://example.com)


Images
-------------------------

![image](http://example.com/logo.png)

![Capture](C:\\Users\\*****\\Desktop\\capture.png)


Blockquotes
-------------------------

A friend once said:

> It's always better to give  
> than to receive.


R Code Blocks
-------------------------

```{r}
summary(cars)
summary(cars$dist)
summary(cars$speed)
```

You can also embed plots, for example:

```{r fig.width=7, fig.height=6}
plot(cars)
```


Inline R Code
-------------------------

There were `r nrow(cars)` cars studied



Plain Code Blocks
-------------------------

```
# This text is displayed verbatim / preformatted
```


Inline Code
-------------------------

We defined the `add` function to
compute the sum of two numbers.


LaTeX Equations
-------------------------


### Inline Equation

$equation$
$\lambda$

$latex equation$
$latex l_a$

\( equation \)
\(\beta+\gamma+\sum_i\)

### Display Equation

$$ equation $$
$$ \lambda $$

$$latex equation $$
$$latex l_a $$

\[ equation \]
\[\beta+\gamma+\sum_i\]


Horizontal Rule / Page Break
-------------------------

Three or more asterisks or dashes:

******

------


Tables
-------------------------

First Header  | Second Header
------------- | -------------
Content Cell  | Content Cell
Content Cell  | Content Cell


Reference Style Links and Images
-------------------------

### Links
A [linked phrase][id].

At the bottom of the document:

[id]: http://example.com/ "Title"

### Images
![alt text][id]

At the bottom of the document:

[id]: C:\\Users\\*****\\Desktop\\capture.png "Title"


Miscellaneous
-------------------------

superscript^2

~~strikethrough~~


Typographic Entities
-------------------------

ASCII characters are transformed into typographic HTML entities: 
Straight quotes ( " and ' ) into “curly” quotes 
Backtick quotes (``like this'') into “curly” quotes 
Dashes (“--” and “---”) into en- and em-dash entities 
Three consecutive dots (“...”) into an ellipsis entity 
Fractions 1/4, 1/2, and 3/4 into ¼, ½, and ¾. 
Symbols (c), (tm), and (r) into ©, ™, and ® 

No comments:

Post a Comment