In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to adjust the order of factors in R language in the process of data visualization. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Case drills
The dataset used in this example is the dataset that comes with the tidyverse package. You can use? gss_cat to view the relevant variables. I will not repeat them here.
It is a frequent operation to change the order of factors in the process of data visualization. For example, if we want to see how the average number of hours spent watching TV per day of different religions is different, we can use the following code:
Relig_summary group_by (relig)% >% summarise (age = mean (age, na.rm = TRUE), tvhours = mean (tvhours, na.rm = TRUE), n = n () ggplot (relig_summary, aes (tvhours, relig)) + geom_point ()
The output from running the code is as follows:
The dot diagram above is actually very ugly. We may think that we can change the order of religions so that the religion with the smallest tvhours is at the bottom of the y-axis and the one with the largest tvhours is at the top.
What to do, you need to use the fct_reorder () method, which takes two parameters:
The first is the factor in which you want to change the order, in this case: religions
Second, the reference to change the order, in this case: tvhours
The code is as follows:
Ggplot (relig_summary, aes (tvhours, fct_reorder (relig, tvhours)) + geom_point ()
As you can see, the picture becomes clearer after changing the order of the religions.
Look at another example:
Rincome_summary% group_by (rincome)% >% summarise (age = mean (age, na.rm = TRUE), tvhours = mean (tvhours, na.rm = TRUE), n = n () ggplot (rincome_summary, aes (age, fct_reorder (rincome, age)) + geom_point ()
The above code can draw the relationship between different rincome and age sorted by age:
However, the problem is that our income (y-axis) is very messy after being sorted by age, so this method is not good. Considering that income is already in order, the good way to deal with it is to keep the original order of income. So we wrote the following code:
Rincome_summary group_by (rincome)% >% summarise (age = mean (age, na.rm = TRUE), tvhours = mean (tvhours, na.rm = TRUE), n = n () ggplot (rincome_summary, aes (age, rincome)) + geom_point ()
Take a look at our chart this time, although other income levels ranks well, we don't want "Not applicable" to be in the first place. At this point we can use fct_relevel (), which also has two parameters:
The factors that need to be sorted, in this case: rincome
The levels that needs to be placed at the front, in this case: Not applicable
The code is as follows:
Ggplot (rincome_summary, aes (age, fct_relevel (rincome, "Not applicable")) + geom_point ()
In this way, our graphics will be more satisfactory.
Look at another example: color control of line graphs:
By_age% filter (! is.na (age))% >% count (age, marital)% >% group_by (age)% >% mutate (prop = n / sum (n)) ggplot (by_age, aes (age, prop, colour = marital)) + geom_line (na.rm = TRUE) ggplot (by_age, aes (age, prop, colour = fct_reorder2 (marital, age, prop)) + geom_line () + labs (colour = "marital")
The above code shows the proportion of changes in marital status at different ages:
Through fct_reorder2, we have realized that the order of the values of y in the legend and the maximum x variable is the same, which can be clearer.
Finally, let's look at an example of adjusting the order of factors in a bar chart.
The following code can change the x-axis label in positive and reverse order:
Gss_cat% >%
Mutate (marital = marital% > fct_infreq ())% >%
Ggplot (aes (marital)) +
Geom_bar ()
Gss_cat% >%
Mutate (marital = marital% >% fct_infreq () >% fct_rev ())% >%
Ggplot (aes (marital)) +
Geom_bar ()
You can try it on your own computer, the key lies in fct_rev ().
Today, we introduce the change of the order of factors in visualization through 3 examples. Thank you for your patience. The main purpose of publishing these things is to urge yourself. I hope everyone will pay attention to the comments and point out the shortcomings and make progress together.
On the R language how to adjust the order of factors in the process of data visualization is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.