日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

用户体验可视化指南pdf_R中增强可视化的初学者指南

發(fā)布時(shí)間:2023/11/29 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用户体验可视化指南pdf_R中增强可视化的初学者指南 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

用戶體驗(yàn)可視化指南pdf

Learning to build complete visualizations in R is like any other data science skill, it’s a journey. RStudio’s ggplot2 is a useful package for telling data’s story, so if you are newer to ggplot2 and would love to develop your visualizing skills, you’re in luck. I developed a pretty quick — and practical — guide to help beginners advance their understanding of ggplot2 and design a couple polished, business-insightful graphs. Because early success with visualizations can be very motivating!

學(xué)習(xí)在R中構(gòu)建完整的可視化效果就像其他任何數(shù)據(jù)科學(xué)技能一樣,是一段旅程。 RStudio的ggplot2是一個(gè)有用的軟件包,用于講述數(shù)據(jù)的故事,因此,如果您是ggplot2的新手,并且希望發(fā)展自己的可視化技能,那么您會(huì)很幸運(yùn)。 我開發(fā)了一個(gè)非常快速且實(shí)用的指南,以幫助初學(xué)者提高對(duì)ggplot2的理解,并設(shè)計(jì)一些精美的,具有業(yè)務(wù)洞察力的圖表。 因?yàn)榭梢暬脑缙诔晒?huì)非常有激勵(lì)作用!

This tutorial assumes you have completed at least one introduction to ggplot2, like this one. If you haven’t, I encourage you to first to get some basics down.

本教程假定您至少已完成ggplot2的介紹, 例如本教程。 如果您還沒(méi)有,我鼓勵(lì)您首先了解一些基礎(chǔ)知識(shí)。

By the end of this tutorial you will:

在本教程結(jié)束時(shí),您將:

  • Deepen your understanding for enhancing visualizations in ggplot2

    加深對(duì)ggplot2中可視化效果的理解
  • Become familiar with navigating the ggplot2 cheat sheet (useful tool)

    熟悉導(dǎo)航ggplot2備忘單(有用的工具)
  • Build two original, polished visuals shown below through a simple, step-by-step format

    通過(guò)簡(jiǎn)單的分步格式構(gòu)建兩個(gè)原始的,經(jīng)過(guò)拋光的視覺(jué)效果,如下所示
Visualization #2可視化#2

Before we begin, here are a couple tools that can support your learning. The first is the ‘R Studio Data Visualization with ggplot2 cheat sheet’ (referred to as ‘cheat sheet’ from now on). We will reference it throughout to help you navigate it for future use.

在我們開始之前,這里有一些工具可以支持您的學(xué)習(xí)。 第一個(gè)是“帶有g(shù)gplot2備忘單的R Studio數(shù)據(jù)可視化”(從現(xiàn)在開始稱為“備忘單”)。 我們將始終引用它,以幫助您導(dǎo)航以備將來(lái)使用。

The second is a ggplot2 Quick Guide I made to help me build ggplots on my own faster. It’s not comprehensive, but it may help you more quickly understand the big picture of ggplot2.

第二個(gè)是 ggplot2快速指南 ,它旨在幫助我更快地自行構(gòu)建ggplots。 它并不全面,但是可以幫助您更快地了解ggplot2的概況。

我們走吧! (Let’s go!)

For this tutorial, we will use the IBM HR Employee Attrition dataset, available here. This data offers (fictitious) business insight and requires no preprocessing. Sweet!

在本教程中,我們將使用IBM HR Employee Attrition數(shù)據(jù)集 , 可從此處獲得 。 該數(shù)據(jù)提供(虛擬的)業(yè)務(wù)洞察力,不需要進(jìn)行預(yù)處理。 甜!

Let’s install libraries and import the data.

讓我們安裝庫(kù)并導(dǎo)入數(shù)據(jù)。

# install libraries
library(ggplot2)
library(scales)
install.packages("ggthemes")
library(ggthemes)# import data
data <- read.csv(file.path('C:YourFilePath’, 'data.csv'), stringsAsFactors = TRUE)

Then check the data and structure.

然后檢查數(shù)據(jù)和結(jié)構(gòu)。

# view first 5 rows
head(attrition)# check structure
str(attrition)

Upon doing so, you will see that there are 1470 observations with 35 employee variables. Let’s start visual #1.

這樣做之后,您將看到有1470個(gè)觀察值和35個(gè)員工變量。 讓我們開始視覺(jué)#1。

視覺(jué)#1 (Visual #1)

HR wants to know how monthly income is related to employee attrition by job role.

人力資源部想知道按職位 劃分月收入員工流失之間的關(guān)系。

步驟1.數(shù)據(jù),美學(xué),幾何 (Step 1. Data, Aesthetics, Geoms)

For this problem, ‘JobRole’ is our X variable (discrete) and ‘MonthlyIncome’ is our Y variable (continuous). ‘Attrition’ (yes/no) is our z variable.

對(duì)于此問(wèn)題,“ JobRole”是我們的X變量(離散),“ MonthlyIncome”是我們的Y變量(連續(xù))。 “損耗”(是/否)是我們的z變量。

Check side 1 of your cheat sheet under ‘Two Variables: Discrete X, Continuous Y,’ and note the various graphs. We will use geom_bar(). On the cheat sheet, it’s listed as geom_bar(stat = ‘identity’). This would give us total monthly income of all employees. We instead want average, so we insert (stat = ‘summary’, fun = mean).

在“兩個(gè)變量:離散X,連續(xù)Y”下檢查備忘單的第一面,并記下各種圖形。 我們將使用geom_bar()。 在備忘單上,它被列為geom_bar(stat ='identity')。 這將給我們所有雇員的總月收入。 相反,我們想要平均值,所以我們插入(stat ='summary',fun = mean)。

# essential layers
ggplot(data, aes(x = JobRole, y = MonthlyIncome, fill=Attrition)) +
geom_bar(stat = 'summary', fun = mean) #Gives mean monthly income

We obviously can’t read the names, which leads us to step 2…

我們顯然無(wú)法讀取名稱,這導(dǎo)致我們進(jìn)入步驟2…

步驟2.座標(biāo)和位置調(diào)整 (Step 2. Coordinates and Position Adjustments)

When names are too long, it often helps to flip the x and y axis. To do so, we will add coord_flip() as a layer, as shown below. We will also unstack the bars to better compare Attrition, by adding position = ‘dodge’ within geom_bar() in the code. Refer to the ggplot2 cheat sheet side 2, ‘Coordinate Systems’ and ‘Position Adjustments’ to see where both are located.

名稱太長(zhǎng)時(shí),通常有助于翻轉(zhuǎn)x和y軸。 為此,我們將添加coord_flip()作為圖層,如下所示。 通過(guò)在代碼的geom_bar()中添加position ='dodge',我們還將對(duì)這些條進(jìn)行堆疊以更好地比較損耗。 請(qǐng)參考ggplot2備忘單第2面,“坐標(biāo)系”和“位置調(diào)整”,以了解兩者的位置。

# unstack bars and flipping axis
ggplot(data, aes(x = JobRole, y = MonthlyIncome, fill=Attrition)) +
geom_bar(stat = ‘summary’, fun = mean, position = ‘dodge’) +
coord_flip()

步驟3.從最高到最低重新排列條形 (Step 3. Reorder bars from highest to lowest)

Now, let’s reorder the bars from highest to lowest Monthly Income to help us better analyze by Job Role. Add the reorder code below within the aesthetics line.

現(xiàn)在,讓我們從月收入的最高到最低重新排序,以幫助我們更好地按工作角色進(jìn)行分析。 在美學(xué)行中的下方添加重新訂購(gòu)代碼。

# reordering job role
ggplot(data, aes(x = reorder(JobRole, MonthlyIncome), y = MonthlyIncome, fill = Attrition)) +
geom_bar(stat = 'summary', fun = mean, position = 'dodge') +
coord_flip()

步驟4.更改條形顏色和寬度 (Step 4. Change bar colors and width)

Let’s change the bar colors to “match the company brand.” This must be done manually, so find scale_fill_manual() on side 2 of the cheat sheet, under “Scales.” It lists colors in base R. You can try some, but they aren’t “company colors.” I obtained the color #s below from color-hex.com.

讓我們更改條形顏色以“匹配公司品牌”。 這必須手動(dòng)完成,因此請(qǐng)?jiān)趥渫鼏蔚诙?cè)的“比例”下找到scale_fill_manual()。 它在基準(zhǔn)R中列出了顏色。您可以嘗試一些,但它們不是“公司顏色”。 我從color-hex.com獲得以下顏色#。

Also, we will narrow the bar widths by adding ‘width=.8’ within geom_bar() to add visually-appealing space.

另外,我們將通過(guò)在geom_bar()中添加'width = .8'來(lái)縮小條形寬度,以增加視覺(jué)上吸引人的空間。

ggplot(data, aes(x = reorder(JobRole, MonthlyIncome), y = MonthlyIncome, fill = Attrition)) +
geom_bar(stat='summary', fun=mean, width=.8, position='dodge') +
coord_flip() +
scale_fill_manual(values = c('#96adbd', '#425e72'))

步驟5.標(biāo)題和軸標(biāo)簽 (Step 5. Title and Axis Labels)

Now let’s add Title and Labels. We don’t need an x label since the job titles explain themselves. See the code for how we handled. Also, check out “Labels” on side 2 of the cheat sheet.

現(xiàn)在讓我們添加標(biāo)題和標(biāo)簽。 我們不需要x標(biāo)簽,因?yàn)槁毼徽f(shuō)明了自己。 請(qǐng)參閱代碼以了解我們的處理方式。 另外,請(qǐng)檢查備忘單第二面的“標(biāo)簽”。

ggplot(data, aes(x = reorder(JobRole, MonthlyIncome), y = MonthlyIncome, fill = Attrition)) +
geom_bar(stat='summary', fun=mean, width=.8, position='dodge') +
coord_flip() +
scale_fill_manual(values = c('#96adbd', '#425e72')) +
xlab(' ') + #Removing x label
ylab('Monthly Income in USD') +
ggtitle('Employee Attrition by Job Role & Income')

步驟6.添加主題 (Step 6. Add Theme)

A theme will kick it up a notch. We will add a theme layer at the end of our code, as shown below. When you start typing ‘theme’ in R, it shows options. For this graph, I chose theme_clean()

一個(gè)主題將使它提升一個(gè)等級(jí)。 我們將在代碼末尾添加一個(gè)主題層,如下所示。 當(dāng)您開始在R中鍵入“主題”時(shí),它會(huì)顯示選項(xiàng)。 對(duì)于此圖,我選擇了theme_clean()

#Adding theme after title
ggtitle('Employee Attrition by Job Role & Income') +
theme_clean()

步驟7.降低圖形高度并使輪廓不可見(jiàn) (Step 7. Reduce graph height and make outlines invisible)

Just two easy tweaks. First, we will remove the graph and legend outlines. Second, the graph seems tall, so let’s reduce the height via aspect.ratio within theme(). Here is the full code for the final graph.

只需兩個(gè)簡(jiǎn)單的調(diào)整。 首先,我們將刪除圖形和圖例輪廓。 其次,圖形看起來(lái)很高,因此讓我們通過(guò)theme()中的Aspect.ratio降低高度。 這是最終圖形的完整代碼。

ggplot(data, aes(x = reorder(JobRole, MonthlyIncome), y = MonthlyIncome, fill = Attrition)) +
geom_bar(stat='summary', fun=mean, width=.8, position='dodge') +
coord_flip() +
scale_fill_manual(values = c('#96adbd', '#425e72')) +
xlab(' ') +
ylab('Monthly Income in USD') +
ggtitle('Employee Attrition by Job Role & Income') +
theme_clean() +
theme(aspect.ratio = .65,
plot.background = element_rect(color = 'white'),
legend.background = element_rect(color = 'white'))

Nice. We see that Research Directors who make more in monthly income are more likely to leave the company. The opposite is the case for other job roles.

真好 我們發(fā)現(xiàn),月收入更高的研究主管更有可能離開公司。 其他工作角色則相反。

You’ve accomplished a lot. Ready for another go? Visual 2 walk-through will be a piece of cake.

您已經(jīng)取得了很多成就。 準(zhǔn)備再去嗎? Visual 2演練將是小菜一碟。

視覺(jué)#2 (Visual #2)

For the second visual, we want to know if employee attrition has any relationship to monthly income, years since last promotion, and work-life balance. Another multivariate analysis.

對(duì)于第二個(gè)視覺(jué)圖像,我們想知道員工的流失是否與月收入自上次升職以來(lái) 的年限以及工作與生活的平衡有關(guān)。 另一個(gè)多元分析。

步驟1.數(shù)據(jù),美學(xué),幾何 (Step 1. Data, Aesthetics, Geoms)

For this problem, ‘MonthlyIncome’ is our X and ‘YearsSinceLastPromotion’ is our Y variable. Both are continuous, so check side 1 of your cheat sheet under ‘Two Variables: Continuous X, Continuous Y.’ For visualization context, we will use geom_smooth(), a regression line often added to scatter plots to reveal patterns. ‘Attrition’ will again be differentiated by color.

對(duì)于此問(wèn)題,“ MonthlyIncome”是我們的X,“ YearsSinceLastPromotion”是我們的Y變量。 兩者都是連續(xù)的,因此請(qǐng)檢查備忘單第1面的“兩個(gè)變量:連續(xù)X,連續(xù)Y”。 對(duì)于可視化上下文,我們將使用geom_smooth(),這是一條通常添加到散點(diǎn)圖中以揭示模式的回歸線。 “損耗”將再次通過(guò)顏色區(qū)分。

ggplot(data, aes(x=MonthlyIncome, y=YearsSinceLastPromotion, color=Attrition)) +
geom_smooth(se = FALSE) #se = False removes confidence shading

We can see that employees who leave are promoted less often. Let’s delve deeper and compare by work-life balance. For this 4th variable, we need to use ‘Faceting’ to view subplots by work-life balance level.

我們可以看到,離職的員工升職的頻率降低了。 讓我們深入研究并通過(guò)工作與生活之間的平衡進(jìn)行比較。 對(duì)于第四個(gè)變量,我們需要使用“ Faceting”以按工作與生活的平衡水平查看子圖。

步驟2.刻面將子圖添加到畫布 (Step 2. Faceting to add subplots to the canvas)

Check out ‘Faceting’ on side 2 of the cheat sheet. We will use facet_wrap() for a rectangular layout.

檢查備忘單第二面的“ Faceting”。 我們將facet_wrap()用于矩形布局。

ggplot(data, aes(x = MonthlyIncome, y = YearsSinceLastPromotion, color=Attrition)) +
geom_smooth(se = FALSE) +
facet_wrap(WorkLifeBalance~.)

The facet grids look good, but what do the numbers mean? The data description explains the codes for ‘WorkLifeBalance’: 1 = ‘Bad’, 2 = ‘Good’, 3 = ‘Better’, 4 = ‘Best’. Add them in step 3.

刻面網(wǎng)格看起來(lái)不錯(cuò),但是數(shù)字意味著什么? 數(shù)據(jù)說(shuō)明解釋了“ WorkLifeBalance”的代碼:1 =“差”,2 =“好”,3 =“更好”,4 =“最好”。 在步驟3中添加它們。

步驟3.將標(biāo)簽添加到構(gòu)面子圖 (Step 3. Add Labels to Facet Subplots)

To add subplot labels, we need to first define the names with a character vector, then use the ‘labeller’ function within facet_wrap.

要添加子圖標(biāo)簽,我們需要首先使用字符向量定義名稱,然后在facet_wrap中使用'labeller'函數(shù)。

# define WorkLifeBalance values
wlb.labs <- c('1' = 'Bad Balance', '2' = 'Good Balance', '3' = 'Better Balance', '4' = 'Best Balance')#Add values to facet_wrap()
ggplot(data, aes(x = MonthlyIncome, y = YearsSinceLastPromotion, color=Attrition)) +
geom_smooth(se = FALSE) +
facet_wrap(WorkLifeBalance~.,
labeller = labeller(WorkLifeBalance = wlb.labs))

步驟4.標(biāo)簽和標(biāo)題 (Step 4. Labels and Title)

Add your labels and title at the end of your code.

在代碼末尾添加標(biāo)簽和標(biāo)題。

facet_wrap(WorkLifeBalance~.,
labeller = labeller(WorkLifeBalance = wlb.labs)) +
xlab('Monthly Income') +
ylab('Years Since Last Promotion') +
ggtitle('Employee Attrition by Workplace Factors')

步驟5.在標(biāo)簽和刻度標(biāo)記之間添加空格 (Step 5. Add Space Between Labels and Tick Markers)

When I look at the graph, the x and y labels seem too close to the tick markers. A simple trick is to insert newline (\n) code within label names.

當(dāng)我查看圖表時(shí),x和y標(biāo)簽似乎太靠近刻度線標(biāo)記。 一個(gè)簡(jiǎn)單的技巧是在標(biāo)簽名稱中插入換行(\ n)代碼。

xlab('\nMonthly Income') + #Adds space above label
ylab('Years Since Last Promotion\n') #Adds space below label

步驟6.主題 (Step 6. Theme)

When you installed library(‘ggthemes’), it gave you more options. For a modern look, I went with theme_fivethirtyeight(). Simply add at the end.

當(dāng)您安裝庫(kù)('ggthemes')時(shí),它為您提供了更多選擇。 對(duì)于現(xiàn)代外觀,我選擇了theme_fivethirtyeight()。 只需在末尾添加即可。

ggtitle('Employee Attrition by Workplace Factors') +
theme_fivethirtyeight()

步驟7.覆蓋主題默認(rèn)值 (Step 7. Override a Theme Default)

What happened to our x and y labels? Well, the default for theme_fivethirtyeight() doesn’t have labels. But we can easily override that with a second theme() layer at the end of your code as shown below.

我們的x和y標(biāo)簽發(fā)生了什么? 好吧,theme_fivethirtyeight()的默認(rèn)值沒(méi)有標(biāo)簽。 但是我們可以在代碼末尾的第二個(gè)theme()層輕松覆蓋它,如下所示。

theme_fivethirtyeight() +
theme(axis.title = element_text())

Not bad. But…people may not be able to tell if ‘Better Balance’ and ‘Best Balance’ are for the top or bottom grids right away. Let’s also change our legend location in step 8.

不錯(cuò)。 但是……人們可能無(wú)法立即判斷出“最佳平衡”和“最佳平衡”是用于頂部還是底部網(wǎng)格。 我們還要在步驟8中更改圖例位置。

步驟8.在網(wǎng)格之間添加空間并更改圖例位置 (Step 8. Add Space Between Grids and Change Legend Location)

Adding space between top and bottom grids and changing the legend location both occur within the second theme() line. See side 2 of cheat sheet under ‘Legends.’

在頂部和底部網(wǎng)格之間添加空間并更改圖例位置都在第二個(gè)theme()行內(nèi)。 請(qǐng)參閱“傳奇”下備忘單的第二面。

theme_fivethirtyeight() +
theme(axis.title = element_text(),
legend.position = 'top',
legend.justification = 'left',
panel.spacing = unit(1.5, 'lines'))

步驟9。更改線條顏色 (Step 9. Change Line Color)

It would be awesome to change line colors to pack a visual punch. Standard R colors don’t quite meet our needs. We will change manually just like we did with Visual #1. I obtained the colors #s from color-hex.com, which has become a useful tool for us.

更改線條顏色以增加視覺(jué)沖擊力真是太棒了。 標(biāo)準(zhǔn)R顏色不能完全滿足我們的需求。 我們將像使用Visual#1一樣手動(dòng)進(jìn)行更改。 我從color-hex.com獲得了顏色#,它已成為我們的有用工具。

Here is the full code for the second visual.

這是第二個(gè)視覺(jué)效果的完整代碼。

ggplot(data, aes(x = MonthlyIncome, y = YearsSinceLastPromotion, color=Attrition)) +
geom_smooth(se = FALSE) +
facet_wrap(WorkLifeBalance~.,
labeller = labeller(WorkLifeBalance = wlb.labs)) +
xlab('\nMonthly Income') +
ylab('Years Since Last Promotion\n') +
theme_ggtitle('Employee Attrition by Workplace Factors') +
theme_fivethirtyeight() +
theme(axis.title = element_text(),
legend.position = 'top',
legend.justification = 'left',
panel.spacing = unit(1.5, 'lines')) +
scale_color_manual(values = c('#999999','#ffb500'))

Another job well done. We see that employees in roles lacking work-life balance seem to stay if promotions are more frequent. The difference in attrition is less noticeable in good or higher work-life balance levels.

另一項(xiàng)工作做得很好。 我們看到,如果升職更加頻繁,則缺乏工作與生活平衡的角色的員工似乎會(huì)留下來(lái)。 在良好或較高的工作與生活平衡水平下,損耗的差異不太明顯。

In this tutorial, we gained skills needed for ggplot2 visual enhancement, became more familiar with the R Studio ggplot2 cheat sheet, and built two nice visuals. I hope that the step-by-step explanations and cheat sheet referencing were helpful and enhanced your confidence using ggplot2.

在本教程中,我們獲得了ggplot2視覺(jué)增強(qiáng)所需的技能,對(duì)R Studio ggplot2備忘單更加熟悉,并構(gòu)建了兩個(gè)不錯(cuò)的視覺(jué)效果。 我希望逐步說(shuō)明和備忘單參考對(duì)您有所幫助,并使用ggplot2增強(qiáng)您的信心。

Many are helping me as I advance my data science and machine learning skills, so my goal is to help and support others in the same way.

隨著我提高數(shù)據(jù)科學(xué)和機(jī)器學(xué)習(xí)技能,許多人正在幫助我,所以我的目標(biāo)是以同樣的方式幫助和支持他人。

翻譯自: https://towardsdatascience.com/beginners-guide-to-enhancing-visualizations-in-r-9fa5a00927c9

用戶體驗(yàn)可視化指南pdf

總結(jié)

以上是生活随笔為你收集整理的用户体验可视化指南pdf_R中增强可视化的初学者指南的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。