Interactive data visualization with R language

Source: Internet
Author: User
Tags plotly ggplot

Last week at the China R Language Conference in Beijing, I shared with you how to visualize the interactive data of R language. The field students are interested in this piece of content, so today put some common interactive visual R package out to share with you.

Rcharts Package

Speaking of the interaction package of the R language, the first thing to think about is the Rcharts package. The package generates a D3-based web interface directly in R.

Installation of the Rcharts package

Require (Devtools) install_github (' Rcharts ', ' RAMNATHV ')

The Rcharts function, like the lattice function, specifies the data source and drawing method through formula, data, and specifies the chart type by type.

Here's an example of how it works. We take the iris data set as an example by first re-assigning the column names through the name function (removing the dots between the words), and then using the Rplot function to draw the scatter plot (type= "point") and use the color to group (color= "species").

Library (rcharts) names (IRIS) = gsub ("\ \", "", Names (Iris)) P1 <-Rplot (sepallength ~ Sepalwidth | Species, data = iris, color = ' species ', type = ' point ') P1

Rcharts supports multiple JavaScript chart libraries, each with its own strengths. Each chart library has several customization options, most of which are supported by Rcharts.

NVD3 is a d3.js project designed to build reusable diagrams and components-it provides the same powerful functionality but is easier to use. It allows us to work with complex datasets to create more advanced visualizations. The Nplot function is provided in the Rcharts package.

The following is an example of the basic principles of the Nplot drawing in the eyes and hair color data (haireyecolor). We group by the color of the eyes (group= "eye"), draw a histogram for the number of hair colors, and set the type to histogram combination (type= "Multibarchart"), which enables grouping and stacking effects.

Library (rcharts) Hair_eye_male <-subset (As.data.frame (haireyecolor), Sex = = "Male") hair_eye_male[,1] <-paste0 ("Hair", hair_eye_male[,1]) hair_eye_male[,2] <-paste0 ("Eye", hair_eye_male[,2]) N1 <-Nplot (Freq to hair, group = "Eye", data = Hair_eye_male,type = "Multibarchart") N1

You can choose which categories you want to see or hide (by default, all categories) through the upper-right corner of the graph, or you can choose whether the columns are grouped or stacked by the upper-left corner (grouped by default). If you select stacked, an overlay histogram is drawn.

Highcharts is a pure JavaScript library that makes charts, supporting most of the chart types: line graphs, graphs, area plots, area graphs, histogram, pie charts, scatter plots, etc. The Hplot function is provided in the Rcharts package.

The basic principle of hplot drawing is illustrated by taking the student survey data set survery in the mass package as an example. We draw a bubble chart of the student's height and pulse beats per minute, using age variables as variables to adjust the bubble size.

Library (rcharts) a <-hplot (Pulse ~ Height, data = Mass::survey, type = "bubble", title = "Zoom Demo", subtitle = "Bubble chart" , size = "age", group = "Exer") a$colors (' Rgba (223,, 5) ', ' Rgba (119, 191, 179,. 5) ', ' Rgba (5) ') 113 (Zoomtype = "xy") a$exporting (Enabled = T) A

Rcharts package can draw more beautiful interactive diagrams, http://ramnathv.github.io/rCharts/and Https://github.com/ramnathv/rCharts/tree/master/demo have more examples for everyone to learn.

Recharts Package

After studying the Rcharts package, perhaps some readers will ask, do we have the Chinese developed the package to achieve similar effects? Here we recommend an equally powerful Recharts package.

This package originates from Baidu developed the domestic top level of open source D3-js Visual Project Echarts (Github Repo). Yang Zhou and Taiyun Wei based on the tool developed Recharts package, modified by Yihui Xie, can pass the JS parameters htmlwidgets, greatly simplifying the development difficulty. However, this package development is still not complete. In order to get started, a function echartr (downloaded to local and later loaded via the source command) is made based on the package for making the underlying Echart interaction diagram. Requires R version >=3.2.0.

The installation method is as follows:

Library (Devtools) install_github (' Yihui/recharts ')

After installation, you need to download the ECHARTR.R script locally on Https://github.com/madlogos/recharts/blob/master/R/echartR.R.

If you want to plot a scatter plot of the iris data set, you can do the following code:

Source ("~ECHARTR.R") names (Iris) = gsub ("\ \", "", Names (Iris)) echartr (data=iris,x=~sepallength,y=~petalwidth,series = ~ Species,type = ' scatter ')

To draw a bar chart:

Hair_eye_male <-subset (As.data.frame (haireyecolor), Sex = = "Male") hair_eye_male[,1] <-paste0 ("Hair", hair_eye_  male[,1]) hair_eye_male[,2] <-paste0 ("Eye", hair_eye_male[,2]) echartr (data = hair_eye_male, x = hair, y = ~freq, Series = ~eye,type = ' Bar ', palette= ' FiveThirtyEight ', Xlab = ' Hair ', Ylab = ' Freq ')


Rose Chart:

Dtcars <-mtcarsdtcars$car <-row.names (dtcars) dtcars$transmission <-as.factor (dtcars$am) levels (dtcars$ Transmission) <-C ("Automatic", "Manual") Dtcars$cylinder <-as.factor (dtcars$cyl) Dtcars$carburetor <- As.factor (Dtcars$carb) echartr (dtcars, x = ~cylinder, y = ~car, type= ' Rose ', palette= ' colorblind ', title= ' number of Cylind ERs ', subtitle = ' (source:mtcars) ')

Radar

Player <-Data.frame (Name=c (Rep ("Philipp Lahm", 8), Rep ("Dani Alves", 8)), Para=rep (C ("passing%", "Key passing", "Comp Crosses "," crossing% "," Successful dribbles "," dispossessed "," dribbled past "," fouls "), 2), Value=c (89.67, 1.51, 0.97, 24.32, 0.83, 0.86, 1.15, 0.47,86.62, 2.11, 0.99, 20.78, 1.58, 1.64, 0.9, 1.71)) Echartr (player, x= ~para, y= ~value, series = ~name, type= ' Radarfill ', symbollist= ' None ', palette=c (' Firebrick1 ', ' DodgerBlue '), title= ' Lahm vs Alves ', subtitle= ' ( by @mixedknuts) ')

plotly Package

The next thing we want to introduce is another powerful plotly package. It is a browser-based, interactive chart library that is built on the plotly.js of the open source JavaScript Chart library.

There are two ways to install:

Install.packages ("plotly")

Or

Devtools::install_github ("ropensci/plotly")

The plotly package uses the function Plot_ly function to draw the interaction graph.

If you plot a scatter plot relative to the iris data set, you need to set the mode parameter to "markers".

Library (plotly) P <-plot_ly (iris, x = petal.length, y = Petal.width,color = species, Colors = "Set1", mode = "markers") p

If you want to draw an interactive box line diagram, you need to set the type parameter to box.

Library (plotly) Plot_ly (midwest, x = percollege, color = state, type = "box")

If you are already familiar with the Ggplot2 drawing system, you can also use the ggplotly function to achieve interactive results for Ggplot2-drawn object p. For example, we want to interact with the density graph plotted by Ggplot and execute the following code.

Library (plotly) p <-ggplot (Data=lattice::singer,aes (X=height,fill=voice.part)) +geom_density () +facet_grid ( voice.part~.) (GG <-ggplotly (p))

Other

There are also lots of fun and useful interaction packs. For example, a dygraphs package designed to draw an interactive timing diagram can be installed by install.packages ("Dygraphs").

Library (dygraphs) lungdeaths <-cbind (mdeaths, fdeaths) dygraph (lungdeaths)%>%dyseries ("mdeaths", label = "Male" )%>%dyseries ("fdeaths", label = "Female")%>%dyoptions (stackedgraph = TRUE)%>%dyrangeselector (height = 20)

The DT package implements R data objects that can be filtered, paged, sorted, and many other functions in an HTML page. Installed via Install.packages ("DT").

In Iris Data set iris, for example, execute the following code:

Library (DT) DataTable (Iris)

The NetworkD3 package implements the D3 JavaScript Network Diagram, which is installed through Install.packages ("networkD3").

Here is an example of drawing a force-directed network diagram.

# Mislinks data (misnodes) # draw forcenetwork (Links = mislinks, Nodes = Misnodes,source = "Source", target = "target") , value = "value", NodeID = "name", group = "group", opacity = 0.8)

We can draw an interactive treemap diagram through the D3treer package, using

Devtools::install_github ("Timelyportfolio/d3treer")

Complete the D3treer package installation.

Library (TREEMAP) library (d3treer) data ("GNI2014") TM <-TreeMap (gni2014,index=c ("Continent", "ISO3"), vsize= " Population ", vcolor=" GNI ", type=" value ") d3tree (Tm,rootname =" World ")

Today is mainly about several r common interaction packages. In the environment of R, the advantage of Dynamic interactive graph is that it can be integrated with KNITR, shiny and so on, and can quickly establish a set of visual prototype system. I hope to share this part with you later.

Interactive data visualization with R language (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.