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)
49
Embed
Some R Examples[R table and Graphics] -Advanced Data Visualization in R (Some Examples)
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
>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")
> 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)
> 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()
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),