Some R Examples[R table and Graphics]
Advanced Data Visualization in R (Some Examples)
References:
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/
I hope that it would ne useful for UseRs.
Umarım; R programı ile ilgilenen herkes için yararlı olur.
Volkan OBAN
11. 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 theref
ore different physical sizes)
> hp + facet_grid(sex ~ smoker, scales="free", space="free")
25.
> library(ggthemes)
> ggplot(nmmaps, aes(date, temp, color=factor(season)))+
+ geom_point()+ggtitle("This plot looks a lot different from the defaul
t")+
+ theme_economist()+scale_colour_economist()
37. 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)
38. > 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)
+ )
39.
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),