Creating interactive charts and maps with R language

Original link: http://tecdat.cn/?p=8032

 

You can create online interactive charts and maps directly from the R / RStudio.

 

Configuration

Start RStudio, create a new RScript, then set the working directory to download the data folder. 

The ggplot2 become interactive chart chart Plotly

Creating interactive dotted line in FIG.

The following code will be installed and loaded package (the program will automatically load ggplot2), load readr and dplyr , and then load the food stamp data we used before.

# install and load plotly, load readr and dplyr
install.packages("plotly")
library(plotly)
library(readr)
library(dplyr)

 Converting it into Plotly FIG.

# load data
food_stamps <- read_csv("food_stamps.csv")

# dot-and-line chart
food_stamps_chart <- ggplot(food_stamps, aes(x = year, y = participants)) +
  xlab("Year") +
  ylab("Participants (millions)") +
  theme_minimal(base_size = 14, base_family = "Georgia")
  geom_point() +
  geom_line()

plot(food_stamps_chart)

As mentioned earlier, this will ggplot2 chart saved in your environment. The following code is converted to "drawing" chart that should be displayed in Viewerthe lower right corner Tags:

When you hover your mouse on the chart at the time of default, some controls are displayed in the upper right corner. 

 

To reformat tooltip, we need to modify ggplot2 and plotly Code

The final layoutfunction code in the code as a tooltip style setting, with a white background and change the font family, the rest of it with the chart consistent. The press code into the page as shown below for editing:

<div class="container">

    <iframe width="100%" height="450" frameborder="0" scrolling="no" src="food_stamps_interactive.html"></iframe>

</div> <!-- /.container -->

The result should be:

Scatter disease and making interactive version of democracy

This code creates a scattergram having a substantially linear trend line, without the point mapping income_groupcolors:

The result should be:

The following code uses qualitative ColorBrewer palette to create a version of this chart for these points to by the World Bank income group for coloring.

This is a static version:

 

This is interactive:

请注意,交互式ggplot版本未继承scale_color_brewer对静态ggplot图表中的图例中的项目进行排序的代码。

但是我们可以通过首先运行以下代码来解决此问题:

这将转换income_group为分类变量或factor,然后按其类别或级别的顺序对数据框进行排序。

现在,像以前一样运行图表代码将可以固定交互式图表中图例中项目的顺序。

制作交互式版本的加州幼儿园免疫接种热图

这是 另一个示例。

结果应该是:

练习制作其他交互式图表

在课堂上,在时间允许的情况下,我们将使用plotly 创建这些ggplot2图表的交互式版本:

 

 

 

使用Leaflet制作地震风险图和地震图

 制作交互式在线地图的最广泛使用的JavaScript库。 

# install and load leaflet and rdgal
install.packages("leaflet")
install.packages("rgdal")
library(leaflet)
library(rgdal)

 首先,让我们看看如何制作以伯克利为中心的基本Leaflet地图:

该地图应显示在Viewer

leaflet函数创建一个 地图。

 

 该addProviderTiles函数使用Leaflet Providers插件将各种图块添加到地图。 

现在seismic使用rgdal中readOGR函数加载从shapefile 开始的地震地图所需的数据。

提到的两个分别seismic指向文件夹和其中的shapefile。

现在你应该在你的环境中调用的对象seismic是一个SpatialPolygonsDataFrame

我们还可以直接从美国地质调查局地震API中加载地震数据,如有关查找和下载数据的课程说明中所述:

使用该网址,我们加载了自1960年初以来6级及以上的地震,地震发生在美国大陆地理中心的6,000公里半径内。

让我们看一下seismic数据摘要:

# view summary of seismic_risk data
summary(seismic)

 

定义破坏性地震的年度风险的数据是可变的ValueRange。但是,此合并变量的类别顺序不正确。要更正此问题,我们应将变量从文本转换为factor,或levels按正确的顺序进行分类。

现在,类别应按正确的顺序排列:

请注意,要运行dplyr代码以mutate

接下来,我们将地震风险数据加载到 地图中:

现在,您应该leaflet在环境中看到一个类型的对象。

现在,我们只需两行代码就可以创建具有两层的地图:

结果应该是:

该函数colorFactor将命名的ColorBrewer调色板分配给类别变量。

该函数addPolygons将多边形添加到地图:不stroke = FALSE给它们轮廓;fillOpacity = 0.7使它们稍微透明;color = ~pal(ValueRange))使用调色板根据ValueRange数据中的值为多边形着色。

 

如果您有任何疑问,请在下面发表评论。 

 

 

  

大数据部落 -中国专业的第三方数据服务提供商,提供定制化的一站式数据挖掘和统计分析咨询服务

统计分析和数据挖掘咨询服务:y0.cn/teradat(咨询服务请联系官网客服

Click here to send me a messageQQ:3025393450

 

​QQ交流群:186388004 

【服务场景】  

科研项目; 公司项目外包;线上线下一对一培训;数据爬虫采集;学术研究;报告撰写;市场调查。

[Tribe] big data to provide customized one-stop data mining and statistical analysis consultancy

Welcome to elective our R language data analysis will be mining will know the course!

  

Big Data tribe  - Chinese professional third-party data service providers to provide customized one-stop data mining and statistical analysis consultancy services

Statistical analysis and data mining consulting services: y0.cn/teradat (Consulting Services, please contact the official website customer service )

Click here to send me a messageQQ:3025393450

 

QQ exchange group: 186 388 004 

[Service] Scene  

Research; the company outsourcing; online and offline one training; data reptile collection; academic research; report writing; market research.

[Tribe] big data to provide customized one-stop data mining and statistical analysis consultancy

Welcome to elective our R language data analysis will be mining will know the course!

Guess you like

Origin www.cnblogs.com/tecdat/p/11741492.html