Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to draw Statistical Graph by ggplot2 in R language

2025-03-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you the R language ggplot2 how to draw statistical graphics, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

What is ggplot2?

In a word: it is an R package used to draw statistical graphs (not just statistical graphs)!

Why is it called ggplot? Of course, gg does not mean Good Game, but Grammar of Graphics. Literal translation is the grammar of drawing. This is a subject of knowledge, the knowledge of drawing. Its distinctive grammar will be introduced later.

The author is Hadley Wickham. Let's take a look at the great god and touch some Reiki to lay a good foundation for the follow-up ggplot learning.

Speaking of this author, it is very impressive. He is the chief scientist of RStudio, and he actually developed the ggplot package during his Ph.D., and later thought that it was not good enough, so he pushed through the old and brought forth the new. with a loud bang in the sky, ggplot2 made its debut. Because this package is so easy to use, gradually, many programming languages such as Python have their corresponding libraries.

Of course, if ggplot2,Wickham alone cannot be the chief scientist of RStudio, he is also the author of dplyr,devtools,readxl,rvest and other packages, and is an omnipotent god.

At the same time, outside of work, he is good at baking and making cocktails and has his own family recipe website. It's really boring!

Update: the Great God also won the 2019 Nobel Prize in Statistics-COPSS Award!

All right, let's get to the point. Let's move on to ggplot2.

What kind of picture can ggplot2 draw?

The front has been praising its good, we have to see what kind of picture it can create! (due to the limitations of the blog, svg's high-definition uncoded vector graphics cannot be used, so only slightly blurred jpg images can be used.)

The first two graphs are standard statistics, we need to draw the chart, but the box chart, histogram, histogram, scatter chart, fitting regression and correlation coefficients, all on one chart, the amount of information is very rich. Put together the strengths of a hundred schools of thought into one's own words! But it doesn't matter. Ggplot2 can make statistical charts that surprise you even more. For example, the following regression tree:

There is also the following magma-like genealogy: how to pass on fire from ancestors to you.

And the last change in the crime rate.

There are thousands of good-looking pictures, and only ggplot2 is the interesting drawing package. Here is only a very small part of the situation, explain what it can do. The pictures on the screen, if they are not generated by themselves, will always be other people's pictures, not their own, so let's talk about how to assemble them into an enchanting and coquettish picture that belongs to you just like building blocks and assembling machines.

Assemble machine

There are three steps to ggplot2 drawing:

According to * * Design drawings * *

Use one by one * "parts" * *

Do it yourself * * "assemble" * *

We will introduce them one by one later, these three steps. But in fact, as long as there is a concept, using ggplot2 is like PS, a layer overlay, and finally constitute our target drawing.

Its advantages are obvious:

Users can control graphics at a more abstract level, making creative drawing easier

Adopt the design method of layer to make it more flexible

Beautiful graphics while avoiding tedious details

The common statistical transformation is integrated into the drawing.

Design drawing

To understand ggplot2, we must first understand the following concepts:

Data (Data) and mapping (Mapping): mapping data to images

Geometric objects (Geometric): represents the actual elements seen in the diagram, such as points, lines, polygons, etc.

Statistical transformation (Statistics): some kind of aggregation of data, such as histograms, or the interpretation of two-dimensional relationships by linear models

Scale: maps the values of data to graphics space, such as color, size, and shape to represent different values.

Coordinate system (Coordinate): how the data is mapped to the plane of the drawing, providing the axes and grid lines needed for drawing

Facet (Facet): decompose the data into subsets for joint presentation

Layer (Layer): overlay the desired drawing operation layer by layer, and finally get the desired drawing

These concepts can help you deepen your understanding of them, but still don't understand them? Never mind! At the beginning, we only need to master the construction of the following parts!

The parts of a machine

At the very beginning, you need to install R and it is recommended to install R Studio, both of which are free and need to be installed, and then you can open them directly from R Studio.

Then we need to install the ggplot2 package first and use the following statement in R or R Studio to complete the installation.

Install.packages ('ggplot2')

Then we can learn the usage of each of these parts.

1. Part-scatter diagram

If we want to see the relationship between two variables, the simplest and most convenient way is to draw a scatter chart, then how can we use ggplot2 to draw such a graph?

Library (ggplot2) p

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report