Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Introduction and Beautification skills of scatter Diagram in R language Visualization

Shulou Source: shulou.com Published: 2022-06-02 06:20:55 09月30日 Update

This article mainly explains "the introduction and beautification skills of scatter chart in R language visualization". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the introduction and beautification skills of scatter chart in R language visualization".

Today I will share with you the scatter chart and its beautification skills!

Scatter plot in R language requires two numerical variables as X-axis and Y-axis mapping objects, and classifies variables by color, size and shape.

Since the scatter chart works better when there is a large amount of data, here we use diamonds, the dataset included in the ggplot package, to demonstrate the case.

Ggplot (diamonds,aes (carat,price)) + geom_point ()

The basic syntax for making a scatter chart in R is as above, dataset, X axis, Y axis, and finally the scatter chart layer.

The above scatter plot is the simplest default graph without any modification. By adding classification variables to the aes mapping to distinguish by size, color and shape, a series of scatter plots can be made.

Scatter plot (shape classification)

Ggplot (diamonds,aes (carat,price)) + geom_point (aes (shape=cut))

Scatter plot (color classification)

Ggplot (diamonds,aes (carat,price)) + geom_point (aes (colour=cut))

Scatter plot (size classification)

Ggplot (diamonds,aes (carat,price)) + geom_point (aes (size=cut))

You can also add more than two classification index parameters to a function statement at the same time.

Ggplot (diamonds,aes (carat,price)) + geom_point (aes (colour=cut,size=table))

Because there are many records in this data set, the data points coincide with each other, and many areas are difficult to identify. Two processing methods are provided in R language:

Use the alpha parameter (transparency):

Ggplot (diamonds,aes (carat,price)) + geom_point (alpha = 1max 10)

Use a pixel-level scatter chart:

Ggplot (diamonds,aes (carat,price)) + geom_point (shape= ".")

Because the scatter chart can be used to show the size of an index on the scatter point by adding size parameters, there is basically no need for a separate bubble chart function.

None of the above scatter plots has considered the problem of beautification; here, you can beautify it by adding themes and color templates:

Ggplot (diamonds,aes (carat,price)) + geom_point (aes (colour=cut)) + ggtitle ("Scatter Plot") + theme_economist (base_size=14) + scale_colour_economist () + guides (colour=guide_legend (title=NULL))

Ggplot (diamonds,aes (carat,price)) + geom_point (aes (colour=cut,size=table)) + ggtitle ("Scatter Plot") + theme_wsj () + scale_colour_wsj () + guides (size=guide_legend (title=NULL), colour=guide_legend (title=NULL))

Ggplot (diamonds,aes (carat,price)) + geom_point (aes (colour=cut,size=table)) + ggtitle ("Scatter Plot") + theme_economist (base_size=14) + scale_colour_economist () + guides (size=guide_legend (title=NULL), colour=guide_legend (title=NULL))

Of course, if you think the dataset is too dense and want to see the specific distribution in a certain area, just add xlim () and ylim () parameters:

Ggplot (diamonds,aes (carat,price)) + geom_point (aes (colour=cut,size=table)) + ggtitle ("Scatter Plot") + theme_wsj () + scale_colour_wsj () + guides (size=guide_legend (title=NULL), colour=guide_legend (title=NULL)) + xlim

Ggplot (diamonds,aes (carat,price)) + geom_point (aes (colour=cut,size=table)) + ggtitle ("Scatter Plot") + theme_economist (base_size=14) + scale_colour_economist () + guides (size=guide_legend (title=NULL), colour=guide_legend (title=NULL)) + xlim (1) + ylim (5000)

Finally, if you want to fit the trend curve of the dataset, you can also do so by adding the geom_smooth () parameter:

Ggplot (diamonds,aes (carat,price)) + geom_point () + geom_smooth (method='lm') + theme_classic ()

Scatter chart function combined with statistical transformation can derive an infinite number of chart types, specific chart type details, you can refer to Hadley's original ggplot.

At this point, I believe you have a deeper understanding of the "introduction and beautification skills of scatter diagrams in R language visualization". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Data classification language parameters techniques size color visualization functions variables shapes two content regions can be passed at the same time charts situations indicators types Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB macOS Linux Docker Microsoft