Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use ggplot2 of R language to realize two-dimensional density map

Shulou Source: shulou.com Published: 2022-05-31 14:02:58 10月04日 Update

In this article, the editor introduces in detail "how to use the ggplot2 of R language to achieve two-dimensional density map", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use the ggplot2 of R language to achieve two-dimensional density map" can help you solve your doubts.

Facet (Facet)

Facet allows us to group the data according to a given condition, and then draw pictures separately.

# in the section on statistical transformation, it is mentioned that if the regression line is divided into groups according to the cutters, it is obvious that the graph will be very messy. With the facet function, we can draw separately.

Ggplot (small, aes (x=carat, y=price)) + geom_point (aes (colour=cut)) + scale_y_log10 () + facet_wrap (~ cut) + stat_smooth ()

Theme (Theme)

After drawing through ggplot, we may also need to customize the graph, such as title, xlab, ylab, such as high frequency need to use, needless to say, ggplot2 provides ggtitle (), xlab () and ylab () to achieve.

But this is far from enough, we need to change the font, font size, axis, background and other elements, which need to be done through the theme () function.

Ggplot2 provides some written themes, such as theme_grey () as the default theme, I often use theme_bw () theme with a white background, and theme_classic () theme, which is similar to R's basic drawing function.

In addition, the ggthemes package provides a number of topics to use, including:

Theme_economist theme_economist_white

Theme_wsj theme_excel

Theme_few theme_foundation

Theme_igray theme_solarized

Theme_stata theme_tufte

Require (ggthemes)

P + theme_wsj ()

Ggplot2_fig20

As for how to change these elements, I think my previous blog post with awkward words can be used as an example:

Fdata.frame (Xerox Magi Yaoyi)

Two-dimensional density map

In this document, for the convenience of drawing, we use a subset of the diamonds data set. If we use the full set, the amount of data is too large, and the scattered points will be pasted. This situation can be represented by two-dimensional density force.

Ggplot (diamonds, aes (carat, price)) + stat_density2d (aes (fill =.. level..), geom= "polygon") + scale_fill_continuous (high='darkred',low='darkgreen')

Ggplot2.fig22

Ggplot2 actual combat

There is a post in the fruit shell knowledge that introduces a wicked curve, which attracts countless otaku to draw pictures with a variety of tools, and even 3D dynamic drawings have come out. Here we use ggplot2 to draw. Please click here for the 3D version.

Fdata.frame (xmemc (x1memx2meme x3), y=rep (yphare3), type=rep (LETTERS [1:3], each=length (y)

Theta data.frame (x=radius*sin (theta), y=radius*cos (theta))

Ggplot (dd, aes (x, y)) + geom_path () + theme_null () + xlab (") + ylab (")

Read here, this article "how to use ggplot2 in R language to achieve two-dimensional density map" article has been introduced, want to master the knowledge of this article still need to practice and use in order to understand, if you want to know more related articles, welcome to follow the industry information channel.

Tags: Themes density two-dimensional language data articles elements content functions fonts backgrounds grouping proper obscene evil no needless to say examples complete collections functions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft NVidia Xiaomi OPPO Reno macOS