R language Learning Note ︱echarts and R visual Package--area Map

Source: Internet
Author: User


Author's message: Thanks to the CDA DSC training camp Weekend finish class, often teachers, Zengko teacher added a small lesson, said Echart and r combination of function package recharts some basic usage. By comparing Yihui Xie's documentation with GitHub, the Zengko teacher greatly simplifies some of the code and is very readable.


About the origin of this package, Baidu marriage d3.js=echarts,echarts+r=recharts Package (Yang Zhou and Taiyun Wei), Yihui Xie teacher modify can pass JS parameters, to achieve more functions, but, Yihui Xie Teacher's improved version of the package has not been sent out, so the God has done a function, first give you try (like Yihui Xie Teacher).

This function is called Echartr, the essence is to the echarts parameter is encapsulatedas a list, and most functions are the same as the Echart function.

To use this function requires: Load recharts package +echartr function from GitHub + some global settings

Download, install and download the ECHARTR function , see the Yihui Xie Teacher's GitHub homepage, paste the URL: https://github.com/madlogos/recharts

Dynamic graphs can be seen: http://madlogos.github.io/recharts/files/echartr.html#force-

——————————————————————————————


first, the basic grammar

Echart (data= data, x=~x axis variable, y=~y axis variable, type= ' scatter ', palette= palette color, title= main title, subtitle= subtitle, xaxis=x axis option, yaxis=y axis option, markline= marker Line, markpoint= Mark Point, etc.)
The grammar uses the source lectures, specific about scatter chart, bubble chart, pie chart, rose chart What to see Teacher's homepage, mainly on Excel is not very well implemented map.

There are three types of maps: area callout, point callout, line callout (most complex data structure), area standard, point label and current standard are different from regional standard.

The most troublesome thing about drawing a map is data collation, and if you want to fill in the dots/lines you have to refactor some data.

——————————————————————————————


second, regional labeling


The main input data type + function + Join point/Line three parts.


1. Area callout input data structure




Several provinces, 2012,2013,2014 three-year GDP value (data source view Xie Teacher homepage), the area labeled data structure is most easy to understand.

The provinces and the data of the year, the need to become a factor type, Xie teachers to use the method is:


As.factor (Enc2native (Dtgdp$prov))


This enc2native is very interesting to use to solve the problem of "Chinese transcoding is always persistent in Windows", reading or setting the encoding of character vectors.


2. Functions


ECHARTR (DTGDP, x = ~prov, y = ~GDP, series= ~year,         type=c (' map ', ' China ', ' area '), palette= ' Gdocs ', title=        "GDPs of China provinces, 2012-2014 (Million USD) ",        subtitle= ' (Source:wikipedia) ',        subtitle_url=" https:// Raw.githubusercontent.com/madlogos/shared_doc/master/shared_documents/chinagdp.txt ",        dataRangePalette=c (' Red ', ' orange ', ' yellow ', ' green ', ' Limegreen '),        datarange=c (' High ', "low"), Pos=list (toolbox=3), MARKPOINT=TOP3)


Function Interpretation: series represents the category label (this is the year);

Type is set to Chinese map, world map, Region callout (area), Dot Callout (point);

Palette Palette, I think the function is the color of the point on the map , Many palette styles can be used including ' aetnagreen ', ' aetnablue ', ' aetnaviolet ', ' Aetnaorange ' , ' aetnateal ', ' Aetnacranberry ';

Datarangepalette: If you open data roaming, you can specify the roaming color board (with palette function), otherwise the Echarts default value, I think the color function is the most important, and not only limited to "red", "yellow", You can also color RGB in many colors, such as #ff6a6a (light red), the details can be found in the RGB color lookup table.

DataRange represents the lower left corner, High/low;

Toolbox=3 represents the lower right column, small pencil, save the picture of the small button, 3 is the lower right corner, 1 is the upper right corner.



3, add a function Markpint


For example, add a point of Zhejiang province GDP, C (Zhejiang, zhejiang, 10000,x,y,t) = (vector name, point set name, Zhejiang GDP value, zhejiang x coordinate, zhejiang y coordinate, whether shiny)

At this time Xie teacher case to join Guangdong, Jiangsu, Shandong Province, three provinces point, so first constitute a data.frame name is called TOP3 data frame, notice series and Prov is character type.




In the function, Echartr (..., markpoint=top3) calls Markpoint, or is the same as adding a bit of data to the messy, call soon.





4. World map


Paste the data structure you need and the functions that are different from the previous Chinese provinces.




World map Examples of data are simple, at a glance. The following function is also very different from the main map of China: type=c ("Map", "World", "area"). The following is the compression code, please see Teacher Xie homepage.


ECHARTR (WORLDGDP, x = ~country, y = ~GDP, type=c (' Map ', ' world ', ' area ')


——————————————————————————————


third, point labeling


Different from the area, is to add the coordinates of the point (x, y), the data structure and the difference between the function




If you want to add a bit of markpoint, the data structure becomes as shown in the following figure, note that name,city is a character type and effect represents a cool effect.




Function of the difference, mainly in the Typle=c ("map", "China", "point"), the following code is omitted version, just to see the different, detailed back to see the teacher's homepage.


ECHARTR (CHINAPM25, x=~city, Y=~PM25, Xcoord=~xcoord, Ycoord=~ycoord,        type=c (' map ', ' China ', ' point '), markpoint= TOP5)



——————————————————————————————


Four, line callout


Data structures such as, more from (start), to (end).

The line callout map is special, and the dataset includes Y (quantity), X (Origin place name), X1 (end place name), series (data series), Xcoord (start latitude), Ycoord (beginning longitude), xcoord1 (end point latitude), Ycoord1 (endpoint longitude). Additional annotations, the Makeline format is expanded




The data y in the figure is a null value, indicating that the line has only one flight by default.

The DOT data format is:




Add line format:




function is no big difference.


ECHARTR (Flight, X=~from, X1=~to, Y=~y, Series=~from, Xcoord=~xcoord.x, ycoord=~ycoord.x,        Xcoord1=~xcoord.y, Ycoord1=~ycoord.y, Type=c (' Map ', ' China ', ' line '),        pos=list (toolbox=3), title= "Southern Airlines main flight Route", Markpoint=tier1)

extension, you need to implement the following steps to achieve the following:

Set the theme dark color, set new point data makepoint, line data makeline

I play in my own time, there is a problem,

Once the data is set, the high-low coordinate scale in the lower left corner is maintained at a very high level, so there is only one color in the map.






R language Learning Note ︱echarts and R visual Package--area Map

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.