R Tutorial: Making Better Plots
Автор: DataCamp
Загружено: 2020-03-04
Просмотров: 324
Want to learn more? Take the full course at https://learn.datacamp.com/courses/ca... at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
One of the places forcats really shines is in helping you make better graphs. Have you ever made a graph and everything is in the wrong order?
What's wrong with this plot? Well, how quickly could you answer "what's the third biggest problem people face?" It's very hard to read this graph right away. For example, to find the problem the largest percentage of people consider an issue, we have to find the point all the way on the right and then connect it back to the text on the y-axis. It's even more difficult to do this for points in the middle.
To fix this, we can use a forcats function, fct_reorder(). Many forcats functions start with fct, which stands for factor. From now on, I'll be saying factor instead of fct.
Factor reorder takes two arguments: the variable you want to reorder and the variable you want to reorder it by. By adding this to our ggplot2 function, we'll have a nicely ordered scatter plot and we can read the y-axis top to bottom.
In that case, we're looking at your unordered variable in relation to another variable. But sometimes we are looking at the variable by itself, such as in a bar chart. Then, you usually want to order by its frequency. Let’s take a look at how we can do this for a frequency bar chart for CurrentJobTitleSelect.
Just like in the previous graph, this one is hard to read. It's fairly easy to see Data Scientist is the most popular job title, and operations research practitioner and data miner the least, but what about the fourth most popular title?
To make the graph easier to read, we can use the forcats function fct_infreq(). This orders a factor by the frequency of each level.
Now the graph is much more easy to read; we can easily see that the fourth most popular title is a data analyst. To make it even better, we could switch the order, from most to least frequent.
To do this, we can use the function fct_rev(). This takes the current levels of the factor and reverses them.
Time to put this into practice.
#DataCamp #RTutorial #CategoricalDataintheTidyverse #MakingBetterPlots

Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: