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

Color matching skills of ggplot Chart in R language Visualization

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "ggplot chart color matching skills in R language visualization". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Today I would like to share with you the color matching principles and basic skills of ggplot charts.

Chart color matching is a very esoteric topic, which is no longer unfathomable thanks to the color packages contributed by many developers of the R language platform.

Here, I will divide all the color matching scenes into two categories:

Discrete variable color matching and continuous variable color matching

The color matching mechanism of the ggplot function is relatively intelligent. When you assign colour or fill attributes to variable mapping, the function automatically distinguishes between variable attributes (discrete variables or continuous variables), and then gives the color matching style suitable for both cases.

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

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

You can compare the above two lines of code: I do not specify any actual color, just assign the colour color attribute to the discrete variable cut or the continuous variable depth, and the output chart has completed the default discrete color mapping and continuous color gradient mapping.

For the above two types of color scenes, if you want to modify them, you need to define your own color scale:

Discrete color scale:

Scale_colour_XXX:

The three most common situations that apply here

In the first case, use the color themes in the ggthemes package, some of which are very popular, such as the Economist themes scale_colour_economist (), scale_colour_wsj (), as well as excel themes, tableau themes, and stata themes.

In the second case, you can use the color cards in the RColorBrewer package. Here are all the discrete color cards allowed by the package's scale_colour_brewer () function.

The color scheme gives us three color matching scenarios about classification variables:

The same color scheme, that is, several color segments are uniformly taken out in the same color system to be filled with classification variables. the final effect looks very refreshing, and the top row of colors in the picture are all of this kind. (suitable for expressing ordered factor classification variables, such as segmented presentation of GDP, etc.)

Dichroism scheme, that is, the smooth transition of the two color systems, uniform values, the middle color is generally white or light. (suitable for expressing classified segments with positive and negative values, such as growth rate indicators, profit margins, etc.)

Multi-classification and different color scheme, take a group of colors with different color system relatively coordinated, this situation is more suitable to express the common situation where there is no order and size between the classification items.

The above color cards and color packs were developed by a well-known team specializing in map visualization color schemes, available at the following URL, online color matching and free download services. (the website may be blocked. If it cannot be opened, you need to use VPN)

Http://colorbrewer2.org/#

Of course, the third is their own DIY, R supports direct calls to recognizable color names and color codes (hexadecimal codes).

Scale_fill_manual (values=c ("# FB882C", "# 5B88A0"))

Or

Scale_fill_manual (values=c ("Linen", "Peru", "PeachPuff", "SandyBrown", "Chocolate"))

However, in the case of continuous variable color modification, there is not much room for discussion:

Scale_fill_gradient ()

Scale_fill_gradient2 ()

Scale_fill_gradient () allows you to assign a set of two-color continuous gradients, low= "white", high= "red"

Scale_fill_gradient2 () allows you to assign a set of tricolor continuous gradients, low= "blue", mid= "white", high= "red"

Next, I will demonstrate one case for each of the above situations:

Library ("ggthemes")

Library ("RColorBrewer")

Discrete:

# use the color theme in the ggthemes package (Wall Street Journal theme color template)

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

# use the color matching card in the RColorBrewer package

Ggplot (diamonds,aes (carat,price,colour=cut)) + geom_point () + scale_colour_brewer (palette= "Greens")

# use DIY to make your own color matching card

Ggplot (diamonds,aes (carat,price,colour=cut)) + geom_point () + scale_colour_manual (values=c ("# 84CEFC", "# ACDAE4", "# 5C9EA4", "# 04FEFC", "# 4CD2CC")

Continuous:

Two-color gradient:

Ggplot (diamonds,aes (carat,price,colour=depth)) + geom_point () + scale_colour_gradient (low= "white", high= "Blue")

Tricolor gradient:

Ggplot (diamonds,aes (carat,price,colour=depth)) + geom_point () + scale_colour_gradient2 (low= "# 8E0F2E", mid= "# BFBEBE", high= "# 0E4E75")

This is the end of the introduction of "ggplot Chart Color matching skills in R language Visualization". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report