49
Reference: http://zevross.com/blog/2014/08/04/beautiful-plotting-in-r-a-ggplot2-cheatsheet-3/ http://www.cookbook-r.com/ http://moderndata.plot.ly/trisurf-plots-in-r-using-plotly/ Prepared by Volkan OBAN Some R Examples [R table and Graphics] Advanced Data Visualization in R (Some Examples)

Some R Examples[R table and Graphics] -Advanced Data Visualization in R (Some Examples)

Embed Size (px)

Citation preview

Reference: http://zevross.com/blog/2014/08/04/beautiful-plotting-in-r-a-ggplot2-cheatsheet-3/

http://www.cookbook-r.com/

http://moderndata.plot.ly/trisurf-plots-in-r-using-plotly/

Prepared by Volkan OBAN

Some R Examples [R table and Graphics]

Advanced Data Visualization in R (Some Examples)

Example: > library(ggmap) > map1 = suppressMessages(get_map( + location = 'Istanbul', zoom = 14, #zoom-in level + maptype="satellite")) #map type > ggmap(map1) >

 

 

Example:

> library(ggmap)

> map = suppressMessages(get_map(location = 'Turkey', zoom = 4)) > ggmap(map)

Example:

trk = suppressMessages(geocode(c( + "Erzurum, Turkey", + "Istanbul,Turkey", + "Izmir,Turkey",

+ "Bursa,Turkey"), source="google")) > ggmap(map) + geom_point(aes(x=trk$lon, y = trk$lat), + lwd = 4, colour = "red") + ggtitle("Ziyaret ettiğim iller")

Example: Table

 

Code: 

library(hwriter) 

 

fdir <‐ "C:/Users/lenovo/Documents/Calısma/Yeni klasör" 

 

data <‐ textConnection("Name,A,B,C,D,E 

                       Volkan OBAN,1,2,5,4,3 

                       Alan Turing,8,12,24,38,7 

                       Virginia Woolf,15,8,7,28,32 

                       Bertnard Russel ,25,28,32,27,42 

                       Thomas Edison,268,321,9,43,21 

                       Albert Einstein,335,316,26,50,64 

                       Abdulhak Hamit Tarhan,10,7,5,3,8 

                       Maria Curie,337,309,25,27,60 

                       Ahmet Hasim,357,305,16,34,57 

                       Cahit Arf,348,302,24,39,56 

                       Omer Seyfettin,324,302,14,63,53 

                       Fizuli,296,301,12,14,40 

                       Gazali,338,299,18,21,62 

                       Asik Veysel,317,297,32,61,99 

                       Erzurumlu Emrah,353,289,11,60,76 

                       Sair Nefi,296,284,14,30,63 

                       Asik Mahsuni Serif,292,284,11,59,37 

                       Tarik Akan,297,283,5,35,26 

                       Cemal Sureyya,307,280,4,35,49 

                       Orhan Veli,358,279,17,29,82 

                       Ahmet Arif,299,271,12,37,71 

                       Sezai Karakoc,332,271,16,31,92 

                       Ozdemir Asaf,293,270,17,33,72 

                       Ahmet Kutsi Tecer,383,269,13,53,75 

                       Tevfik Fikret,276,264,8,40,37 

                       Cenap Sahabettin,280,261,7,26,38 

                       Ahmet Muhip Diranas,337,258,21,62,90 

                       Faruk Nafiz Camlibel,294,255,28,68,74 

                       Necip Fazil Kisakurek,336,250,24,39,84 

                       Nazim Hikmet, 349,246,30,73,113 

                       Can Yucel,361,224,19,35,99") 

 

 

data <‐ read.csv(data, h=T) 

 

row.colours <‐ rep(c("white", "#C0C0C0"), nrow(data)/2+1) 

 

hwrite(data[1:nrow(data), 1:6], 

       paste(fdir, "AlternatingRowsTable.html", sep="/"), 

       center=TRUE, 

       row.bgcolor=row.colours) 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Example:

 

 

 

Code: 

library(hwriter) 

 

fdir <‐ "C:/Users/lenovo/Documents/Calısma/Yeni klasör" 

 

data <‐ textConnection("Name,A,B,C,D,E 

                       Volkan OBAN,1,2,5,4,3 

                       Alan Turing,8,12,24,38,7 

                       Virginia Woolf,15,8,7,28,32 

                       Bertnard Russel ,25,28,32,27,42 

                       Thomas Edison,268,321,9,43,21 

                       Albert Einstein,335,316,26,50,64 

                       Abdulhak Hamit Tarhan,10,7,5,3,8 

                       Maria Curie,337,309,25,27,60 

                       Ahmet Hasim,357,305,16,34,57 

                       Cahit Arf,348,302,24,39,56 

                       Omer Seyfettin,324,302,14,63,53 

                       Fizuli,296,301,12,14,40 

                       Gazali,338,299,18,21,62 

                       Asik Veysel,317,297,32,61,99 

                       Erzurumlu Emrah,353,289,11,60,76 

                       Sair Nefi,296,284,14,30,63 

                       Asik Mahsuni Serif,292,284,11,59,37 

                       Tarik Akan,297,283,5,35,26 

                       Cemal Sureyya,307,280,4,35,49 

                       Orhan Veli,358,279,17,29,82 

                       Ahmet Arif,299,271,12,37,71 

                       Sezai Karakoc,332,271,16,31,92 

                       Ozdemir Asaf,293,270,17,33,72 

                       Ahmet Kutsi Tecer,383,269,13,53,75 

                       Tevfik Fikret,276,264,8,40,37 

                       Cenap Sahabettin,280,261,7,26,38 

                       Ahmet Muhip Diranas,337,258,21,62,90 

                       Faruk Nafiz Camlibel,294,255,28,68,74 

                       Necip Fazil Kisakurek,336,250,24,39,84 

                       Nazim Hikmet, 349,246,30,73,113 

                       Can Yucel,361,224,19,35,99") 

data <‐ read.csv(data, h=T) 

 

data$Name_cell_color <‐ "white" 

for(col in names(data[,c(‐1, ‐7)])) { 

  ntiles <‐ quantile(data[,c(col)], probs=c(.25, .75)) 

  color_col <‐ paste(col, "cell_color", sep="_") 

  columnData <‐ data[,c(col)] 

  data[,c(color_col)] <‐ ifelse(columnData<=ntiles[1], "red", 

                                ifelse(columnData>=ntiles[2], "green", 

                                       "white")) 

 

# transpose the color columns of the data frame and convert it to a list 

# goes from dataframe to matrix to dataframe to list 

# see http://stackoverflow.com/questions/3492379/data‐frame‐rows‐to‐a‐list 

row.colours <‐ as.list(as.data.frame(t(data[1:nrow(data),7:12]))) 

 

hwrite(data[1:nrow(data), 1:6], 

       paste(fdir, "QuartilesTable.html", sep="/"), 

       center=TRUE, 

       row.bgcolor=row.colours) 

 

 

 

 

 

Example:

>library(reshape2) > head(tips) total_bill tip sex smoker day time size 1 16.99 1.01 Female No Sun Dinner 2 2 10.34 1.66 Male No Sun Dinner 3 3 21.01 3.50 Male No Sun Dinner 3 4 23.68 3.31 Male No Sun Dinner 2 5 24.59 3.61 Female No Sun Dinner 4 6 25.29 4.71 Male No Sun Dinner 4 > # A histogram of bill sizes > hp <- ggplot(tips, aes(x=total_bill)) + geom_histogram(binwidth=2,colour="white") > > # Histogram of total_bill, divided by sex and smoker > hp + facet_grid(sex ~ smoker) > > # Same as above, with scales="free_y" > hp + facet_grid(sex ~ smoker, scales="free_y") > > # With panels that have the same scaling, but different range (and therefore different physical sizes) > hp + facet_grid(sex ~ smoker, scales="free", space="free")

 

 

Example: 

library(ggplot2) 

df1 <‐ data.frame(     sex = factor(c("Female","Female","Male","Male")),     time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")),     total_bill = c(13.53, 16.81, 16.24, 17.42) )  # A basic graph lp <‐ ggplot(data=df1, aes(x=time, y=total_bill, group=sex, shape=sex)) + geom_line() + geom_point() lp  # Change the legend lp + scale_shape_discrete(name  ="Payer",                           breaks=c("Female", "Male"),                           labels=c("Woman", "Man")) 

 

 

 

lp1 + scale_colour_discrete(name  ="Payer",                             breaks=c("Female", "Male"),                             labels=c("Woman", "Man")) +       scale_shape_discrete(name  ="Payer",                            breaks=c("Female", "Male"),                            labels=c("Woman", "Man")) 

 

Multiplot---  >library(ggplot2) >bp <‐ ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() 

> a1<‐bp + theme(legend.position=c(.5, .5)) >  > # Set the "anchoring point" of the legend (bottom‐left is 0,0; top‐right is 1,1) > # Put bottom‐left corner of legend box in bottom‐left corner of graph > a2<‐bp + theme(legend.justification=c(0,0), legend.position=c(0,0)) >  > # Put bottom‐right corner of legend box in bottom‐right corner of graph > a3<‐bp + theme(legend.justification=c(1,0), legend.position=c(1,0))   > multiplot <‐ function(..., plotlist=NULL, file, cols=1, layout=NULL) { +     require(grid) +      +     # Make a list from the ... arguments and plotlist +     plots <‐ c(list(...), plotlist) +      +     numPlots = length(plots) 

+      +     # If layout is NULL, then use 'cols' to determine layout +     if (is.null(layout)) { +         # Make the panel +         # ncol: Number of columns of plots +         # nrow: Number of rows needed, calculated from # of cols +         layout <‐ matrix(seq(1, cols * ceiling(numPlots/cols)), +                          ncol = cols, nrow = ceiling(numPlots/cols)) +     } +      +     if (numPlots==1) { +         print(plots[[1]]) +          +     } else { +         # Set up the page +         grid.newpage() +         pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout)))) +          +         # Make each plot, in the correct location +         for (i in 1:numPlots) { +             # Get the i,j matrix positions of the regions that contain this subplot +             matchidx <‐ as.data.frame(which(layout == i, arr.ind = TRUE)) +              +             print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, +                                             layout.pos.col = matchidx$col)) +         } +     } + } > multiplot(a1,a2,a3) 

 

 

Example: 

library(ggplot2) > nmmaps<-read.csv("chicago-nmmaps.csv", as.is=T) > nmmaps$date<-as.Date(nmmaps$date) > nmmaps<-nmmaps[nmmaps$date>as.Date("1996-12-31"),] > nmmaps$year<-substring(nmmaps$date,1,4) > head(nmmaps) city date death temp dewpoint pm10 o3 time season year 3654 chic 1997-01-01 137 36.0 37.50 13.052268 5.659256 3654 winter 1997 3655 chic 1997-01-02 123 45.0 47.25 41.948600 5.525417 3655 winter 1997 3656 chic 1997-01-03 127 40.0 38.00 27.041751 6.288548 3656 winter 1997 3657 chic 1997-01-04 146 51.5 45.50 25.072573 7.537758 3657 winter 1997

3658 chic 1997-01-05 102 27.0 11.25 15.343121 20.760798 3658 winter 1997 3659 chic 1997-01-06 127 17.0 5.75 9.364655 14.940874 3659 winter 1997 >g<-ggplot(nmmaps, aes(date, temp))+geom_point(color="firebrick") > g

>g<-ggplot(nmmaps, aes(date, temp))+geom_point(color="firebrick") > g + ylim(c(0,60))

 

 

> g<-ggplot(nmmaps, aes(date, temp, color=factor(season)))+geom_point() > g

 

 

g+theme(legend.title = element_text(colour="chocolate", size=16, face="bold"))+ + scale_color_discrete(name="This color is\ncalled chocolate!?")

 

 

 

 

> ggplot(nmmaps, aes(x=date, y=o3))+geom_line(color="grey")+geom_point(color="red")

 

 

> ggplot(nmmaps, aes(date,temp))+geom_point(color="chartreuse4")+ + facet_wrap(~year, ncol=2)

 

 

 

> ggplot(nmmaps, aes(date,temp))+geom_point(color="chartreuse4")+ + facet_wrap(~year, ncol=2, scales="free")

 

 

 

 

 

> library(grid) > pushViewport(viewport(layout = grid.layout(1, 2))) > print(myplot1, vp = viewport(layout.pos.row = 1, layout.pos.col = 1)) > print(myplot2, vp = viewport(layout.pos.row = 1, layout.pos.col = 2)) > > # alternative, a little easier > library(gridExtra) > grid.arrange(myplot1, myplot2, ncol=2)

 

 

 

 

> library(ggthemes) > ggplot(nmmaps, aes(date, temp, color=factor(season)))+ + geom_point()+ggtitle("This plot looks a lot different from the default")+ + theme_economist()+scale_colour_economist()

 

 

 

> ggplot(nmmaps, aes(date, temp, color=factor(season)))+ + geom_point() + + scale_color_manual(values=c("dodgerblue4", "darkolivegreen4", + "darkorchid3", "goldenrod1"))

 

 

 

 

> library(ggthemes) > ggplot(nmmaps, aes(date, temp, color=factor(season)))+ + geom_point() + + scale_colour_tableau()

 

 

 

 

 

> ggplot(nmmaps, aes(date, temp, color=o3))+geom_point()+ + scale_color_gradient(low="darkkhaki", high="darkgreen")

> mid<-mean(nmmaps$o3) > ggplot(nmmaps, aes(date, temp, color=o3))+geom_point()+ + scale_color_gradient2(midpoint=mid, + low="blue", mid="white", high="red" )

 

 

> library(grid) > > my_grob = grobTree(textGrob("This text stays in place!", x=0.1, y=0.95, hjust=0, + gp=gpar(col="blue", fontsize=12, fontface="italic"))) > > ggplot(nmmaps, aes(temp, o3))+geom_point(color="firebrick")+facet_wrap(~season, scales="free")+ + annotation_custom(my_grob)

 

 

 

> g<-ggplot(nmmaps, aes(x=season, y=o3)) > g+geom_jitter(alpha=0.5, aes(color=season),position = position_jitter(width = .2))

> g+geom_violin(alpha=0.5, color="gray")

> g+geom_violin(alpha=0.5, color="gray")+geom_jitter(alpha=0.5, aes(color=season), + position = position_jitter(width = 0.1))+coord_flip()

> nmmaps$o3run<-as.numeric(filter(nmmaps$o3, rep(1/30,30), sides=2)) > ggplot(nmmaps, aes(date, o3run))+geom_line(color="lightpink4", lwd=1)

> ggplot(nmmaps, aes(date, o3run))+geom_ribbon(aes(ymin=0, ymax=o3run), fill="lightpink3", color="lightpink3")+ + geom_line(color="lightpink4", lwd=1)

> ggplot(nmmaps, aes(date, temp))+ + geom_point(color="grey")+ + stat_smooth(method="gam", formula=y~s(x,k=10), col="darkolivegreen2", se=FALSE, size=1)+ + stat_smooth(method="gam", formula=y~s(x,k=30), col="red", se=FALSE, size=1)+ + stat_smooth(method="gam", formula=y~s(x,k=500), col="dodgerblue4", se=FALSE, size=1)

Examples: > library(plotly) > library(geometry) > > g <- expand.grid( + u = seq(0, 2 * pi, length.out = 24), + v = seq(-1, 1, length.out = 8) + ) > tp <- 1 + 0.5 * g$v * cos(g$u / 2) > m <- matrix( + c(tp * cos(g$u), tp * sin(g$u), 0.5 * g$v * sin(g$u / 2)), + ncol = 3, dimnames = list(NULL, c("x", "y", "z")) + ) > > # the key though is running delaunayn on g rather than m > d <- delaunayn(g)

> td <- t(d) > # but using m for plotting rather than the 2d g > > # define layout options > axs <- list( + backgroundcolor="rgb(230,230,230)", + gridcolor="rgb(255,255,255)", + showbackground=TRUE, + zerolinecolor="rgb(255,255,255" + ) > > # now figure out the colormap > # start by determining the mean of z for each row > # of the Delaunay vertices > zmean <- apply(d, MARGIN=1, function(row){mean(m[row,3])}) > > library(scales) > # result will be slighlty different > # since colour_ramp uses CIELAB instead of RGB > # could use colorRamp for exact replication > facecolor = colour_ramp( + brewer_pal(palette="RdBu")(9) + )(rescale(x=zmean)) > > > plot_ly( + x = m[, 1], y = m[, 2], z = m[, 3], + # JavaScript is 0 based index so subtract 1 + i = d[, 1]-1, j = d[, 2]-1, k = d[, 3]-1, + facecolor = facecolor, + type = "mesh3d" + ) %>% + layout( + title="Moebius band triangulation", + scene=list(xaxis=axs,yaxis=axs,zaxis=axs), + aspectratio=list(x=1,y=1,z=0.5) + )

 

 

Example:

 

n <- 12 h <- 1/(n-1) r = seq(h, 1, length.out=n) theta = seq(0, 2*pi, length.out=36) g <- expand.grid(r=r, theta=theta) x <- c(g$r * cos(g$theta),0) y <- c(g$r * sin(g$theta),0) z <- sin(x*y) m <- matrix( c(x,y,z),

ncol = 3, dimnames = list(NULL, c("x", "y", "z")) ) tri <- delaunayn(m[,1:2]) # now figure out the colormap zmean <- apply(tri,MARGIN=1,function(row){mean(m[row,3])}) library(scales) library(rje) facecolor = colour_ramp( cubeHelix(12) )(rescale(x=zmean)) plot_ly( x=x, y=y, z=z, i=tri[,1]-1, j=tri[,2]-1, k=tri[,3]-1, facecolor=facecolor, type="mesh3d" ) %>% layout( title="Triangulated surface", scene=list( xaxis=axs, yaxis=axs, zaxis=axs, camera=list( eye=list(x=1.75,y=-0.7,z=0.75) ) ), aspectratio=list(x=1,y=1,z=0.5) )

Example:

Code:

> library(geomorph) > plyFile <- 'http://people.sc.fsu.edu/~jburkardt/data/ply/chopper.ply' > dest <- basename(plyFile) > if (!file.exists(dest)) { + download.file(plyFile, dest) + } > > mesh <- read.ply(dest) > # see getS3method("shade3d", "mesh3d") for details on how to plot > > # plot point cloud > x <- mesh$vb["xpts",] > y <- mesh$vb["ypts",] > z <- mesh$vb["zpts",] > m <- matrix(c(x,y,z), ncol=3, dimnames=list(NULL,c("x","y","z"))) > > # now figure out the colormap > zmean <- apply(t(mesh$it),MARGIN=1,function(row){mean(m[row,3])}) > > library(scales) > facecolor = colour_ramp( + brewer_pal(palette="RdBu")(9) + )(rescale(x=zmean)) > > plot_ly( + x = x, y = y, z = z, + i = mesh$it[1,]-1, j = mesh$it[2,]-1, k = mesh$it[3,]-1, + facecolor = facecolor, + type = "mesh3d" + )

>

Subplot example:

> library(plotly) > p <- subplot( + plot_ly(economics, x = date, y = uempmed), + plot_ly(economics, x = date, y = unemploy), + margin = 0.05, + nrows=2 + ) %>% layout(showlegend = FALSE) > p

Example:

> data(iris) > iris$id <- as.integer(iris$Species) > p <- plot_ly(iris, x = Petal.Length, y = Petal.Width, group = Species, + xaxis = paste0("x", id), mode = "markers") > p <- plotly_build(subplot(p)) > p$filename <- "overwrite_subplot" > p

Example:

> library(plotly) > df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/1962_2006_walmart_store_openings.csv') > > # common map properties > g <- list(scope = 'usa', showland = T, landcolor = toRGB("gray90"), showcountries = F, subunitcolor = toRGB("white")) > > # year text labels > yrs <- unique(df$YEAR) > id <- seq_along(yrs) > df2 <- data.frame( + YEAR = yrs, + id = id + )

> > # id for anchoring traces on different plots > df$id <- as.integer(factor(df$YEAR)) > > p <- plot_ly(df, type = 'scattergeo', lon = LON, lat = LAT, group = YEAR, + geo = paste0("geo", id), + showlegend = F, marker = list(color = toRGB("blue"), opacity = 0.5)) %>% + add_trace(lon = -78, lat = 47, mode = 'text', group = YEAR, + geo = paste0("geo", id), + text = YEAR, data = df2) %>% + layout(title = 'New Walmart Stores per year 1962-2006<br> Source: <a href="http://www.econ.umn.edu/~holmes/data/WalMart/index.html">University of Minnesota</a>', + geo = g, + autosize = F, + width = 1000, + height = 900, + hovermode = F) > > p <- subplot(p, nrows = 9) > p

Example:

> library(plotly) > p <- plot_ly(plotly::mic, r = r, t = t, color = nms, mode = "lines") > layout(p, title = "Mic Patterns", orientation = -90)

Example: Waterfall Chart

> library(ggplot2) > library(reshape2) > > data <- textConnection("Step,Value,Change + Start,100 + Step 1,80,20 + Step 2,70,10 + Step 3,62,8 + Step 4,52,10 + Final result,52 + ") > data <- read.csv(data, h=T) > > data$id <- 1:nrow(data) > > naChange <- is.na(data$Change) > data[naChange,]$Change <- data[is.na(data$Change),]$Value > data[naChange,]$Value <- 0 > > data$start <- data$Value > data$end <- data$start+data$Change > > p <- ggplot(data, aes(Step, fill = "blue"))

> p + geom_rect(aes(x = Step, xmin = id - 0.45, xmax = id + 0.45, + ymin = start, ymax = end)) + + scale_fill_identity("Metric Name") + + labs(x="X Label", y="Y Label", title="Waterfall Chart Example")

Example: Basic Table.

library(xtable)

fdir <- "C:/Users/lenovo/Documents/Calisma/Yeni klasör"

data <- textConnection("Name,A,B,C,D,E

Volkan OBAN,0,2,4,6,8

Volkan OBAN,3,5,7,9,11

Volkan OBAN,11,13,15,17,19

Volkan OBAN,20,22,24,26,28

Volkan OBAN,1,2,3,4,5

VOLKAN OBAN,1,5,25,125,625

VOLKAN OBAN,0,-1,-2,-3,0

VOLKAN OBAN,2,4,6,8,10")

data <- read.csv(data, h=T)

table_as_html <- print(xtable(data), type="html", print.results = FALSE)

table_as_latex <- print(xtable(data), print.results = FALSE)

cat(table_as_html, file=paste(fdir, "BasicTable.html", sep="/"))

cat(table_as_latex, file=paste(fdir, "BasicTable.tex", sep="/"))