国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

[R] How to communicate with your data? - ggplot2

這篇具有很好參考價(jià)值的文章主要介紹了[R] How to communicate with your data? - ggplot2。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

We have gone through the basic part of how to clean and process before analyzing your data.

How to communicate with your data?

R語言具有生成各種圖形的多種可能性。

并非所有圖形功能對(duì)初學(xué)者來說都是必要的。 復(fù)雜的圖形需要長代碼。

我們將從簡(jiǎn)單的圖形元素開始,然后逐步定制復(fù)雜圖形。

Which package do we need: ggplot 2

>library (ggplot2)

What can we do?

For continuous variables:

Creating, editing coloring histogram

For categorical variables

Creating, editing coloring bar plot
我們需要哪個(gè)包:

ggplot2 >庫(ggplot2)

我們能做什么

對(duì)于連續(xù)變量: 創(chuàng)建,編輯著色直方圖

對(duì)于分類變量: 創(chuàng)建,編輯著色條形圖

# 導(dǎo)入 ggplot2 包
library(ggplot2)

# 創(chuàng)建一個(gè)數(shù)據(jù)框
data <- data.frame(
  x = c(1, 2, 3, 4, 5),
  y = c(2, 3, 4, 5, 6)
)

# 使用 ggplot 函數(shù)創(chuàng)建一個(gè)散點(diǎn)圖
ggplot(data, aes(x = x, y = y)) +
  geom_point()

Separate parts or layers

In ggplot2, a plot can be subdivided into separate parts or layers, each of which contributes to the final appearance of the plot. This layering system allows you to add different elements to the plot, such as data points, lines, text, and annotations, in a flexible and customizable way.

Here's a brief explanation of the key components of a ggplot2 plot:

  1. Data: The data you want to visualize, typically in the form of a data frame.

  2. Aesthetic Mapping (aes)?adj. 審美的,美學(xué)的;美的,藝術(shù)的: Aesthetic mappings define how variables in the data are mapped to visual properties of the plot, such as x and y positions, colors, shapes, and sizes.?

  3. Geoms (Geometric Objects): Geoms are the visual elements that represent the data in the plot, such as points, lines, bars, and polygons. Each geom function adds a new layer to the plot.

  4. Facets: Facets allow you to create multiple plots, each showing a different subset of the data. You can facet by one or more variables to create small multiples.

  5. Stats (Statistical Transformations): Stats are used to calculate summary statistics or perform transformations on the data before plotting. Each stat function can be thought of as a new dataset that is plotted using a geom.

  6. Scales: Scales control how the data values are mapped to the visual properties of the plot, such as axes, colors, and sizes. You can customize scales to change the appearance of the plot.

  7. Coordinate Systems: Coordinate systems determine how the plot is spatially arranged. The default is Cartesian coordinates, but ggplot2 also supports polar coordinates and other specialized coordinate systems.

By combining these components and adding them in layers, you can create complex and informative visualizations that effectively communicate insights from your data.

Using mtcars dataset to explore:

The mtcars dataset in R contains information about various features of 32 different automobiles from the early 1970s. Here are the meanings of the variables in the mtcars dataset:

  1. mpg: Miles per gallon (fuel efficiency).
  2. cyl: Number of cylinders.
  3. disp: Displacement (engine size) in cubic inches.
  4. hp: Gross horsepower.
  5. drat: Rear axle ratio.
  6. wt: Weight (in 1000 lbs).
  7. qsec: 1/4 mile time (in seconds).
  8. vs: Engine type, where 0 = V-shaped and 1 = straight.
  9. am: Transmission type, where 0 = automatic and 1 = manual.
  10. gear: Number of forward gears.
  11. carb: Number of carburetors.
#Load mtcars and ggplot2
data("mtcars")
str(mtcars)

library(ggplot2)
'data.frame':	32 obs. of  11 variables:
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
 $ disp: num  160 160 108 258 360 ...
 $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 $ qsec: num  16.5 17 18.6 19.4 17 ...
 $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
 $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
 $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
 $ carb: num  4 4 1 1 2 1 4 2 2 4 ...

It tell the performances of cars in the US.

ggplot(mtcars,aes(x=mpg))+geom_histogram()

[R] How to communicate with your data? - ggplot2,r語言,python,開發(fā)語言

ggplot(mtcars,aes(x=cyl))+geom_histogram()

[R] How to communicate with your data? - ggplot2,r語言,python,開發(fā)語言

It look poor.

ggplot(mtcars,aes(x=mpg))+geom_dotplot()

[R] How to communicate with your data? - ggplot2,r語言,python,開發(fā)語言

The resulting image is a dot plot where each dot represents a car from the mtcars dataset, and the position of the dot on the x-axis represents its miles per gallon value. The dot plot can give you an idea of the distribution of miles per gallon values in the dataset and can help identify any patterns or outliers.

ggplot(mtcars,aes(x=qsec))+geom_area(stat="bin")

[R] How to communicate with your data? - ggplot2,r語言,python,開發(fā)語言

The code attempts to create an area plot using the qsec variable from the mtcars dataset.

ggplot(mtcars,aes(x=disp))+geom_density()

#or

ggplot(mtcars,aes(x=disp))+geom_density(kernel ="gaussian")

[R] How to communicate with your data? - ggplot2,r語言,python,開發(fā)語言

The code creates a density plot using the disp (displacement) variable from the mtcars dataset. Here's a breakdown of the code:

  • ggplot(mtcars, aes(x = disp)): This sets up the basic plot using the mtcars dataset and specifies that the x-axis of the plot should represent the disp variable.

  • geom_density(): This adds a layer to the plot, specifying that the data should be displayed as a density plot.

Density plots are useful for visualizing the distribution of a continuous variable and can help identify patterns such as peaks, valleys, and skewness偏度 in the data.

In a density plot created using geom_density(), the y-axis represents the density of the data at each point along the x-axis. Density is a way of representing the distribution of data values. It is calculated using kernel density estimation, which estimates the probability density function of the underlying variable.

Graphing

[R] How to communicate with your data? - ggplot2,r語言,python,開發(fā)語言

poor for publication

1.binwidth

2. color

3. title and labels

4. Gaussian curve: from a normal distribution or not

Change four parameters in my bar design= change to be made on Geom

Binwidth=nbr Change the bar width

Fill ="name of the?colour" Change the colour with which the bar is filled

Colour="name of the colour” Change the outline of the?bar

Alpha=nbr? Change the transparency of the colour

ggplot(mtcars,aes(x=mpg))+geom_histogram(binwidth = 5)
ggplot(mtcars,aes(x=mpg))++geom_histogram(fill="blue",binwidth=5)
ggplot(mtcars,aes(x=mpg))+geom_histogram(fill="skyblue",alpha=0.7,binwidth=5,colour="grey")

[R] How to communicate with your data? - ggplot2,r語言,python,開發(fā)語言[R] How to communicate with your data? - ggplot2,r語言,python,開發(fā)語言

[R] How to communicate with your data? - ggplot2,r語言,python,開發(fā)語言

#Let's practice, hisogram of BMI in purple
#after importing the excel file with File->Import dataset->From excel
ggplot(SEE_students_data_2,aes(x=BMI))+geom_histogram(binwidth = 1, fill="purple",colour="black",alpha=0.5)

[R] How to communicate with your data? - ggplot2,r語言,python,開發(fā)語言

  • ggplot(SEE_students_data_2, aes(x = BMI)):使用SEE_students_data_2數(shù)據(jù)集,將BMI變量映射到x軸。

  • geom_histogram(binwidth = 1, fill = "purple", colour = "black", alpha = 0.5):添加直方圖層,其中binwidth = 1指定每個(gè)直方柱的寬度為1(即每個(gè)單位)。fill = "purple"設(shè)置直方圖的填充顏色為紫色,colour = "black"設(shè)置邊框顏色為黑色,alpha = 0.5設(shè)置透明度為0.5,使得直方圖具有一定的透明度。

  • ggplot(SEE_students_data_2, aes(x = BMI)): This sets up the basic plot using the SEE_students_data_2 dataset and maps the BMI variable to the x-axis.

  • geom_histogram(binwidth = 1, fill = "purple", colour = "black", alpha = 0.5): This adds a histogram layer to the plot. binwidth = 1 specifies the width of each histogram bin as 1 (i.e., each unit). fill = "purple" sets the fill color of the histogram bars to purple, colour = "black" sets the border color to black, and alpha = 0.5 sets the transparency to 0.5, giving the histogram bars some transparency.

Tips:

1. Since male and female depends on the variable Gender, the fill option should be specified in the aesthetics part

2. Geom_area require the option stat=bin when there is no variable plot on the Y axis

ggplot(SEE_students_data_2,aes(x=BMI, fill=Gender))+geom_density(colour="black",alpha=0.5)

[R] How to communicate with your data? - ggplot2,r語言,python,開發(fā)語言

  • ggplot(SEE_students_data_2, aes(x = BMI, fill = Gender)): Sets up the basic plot using the SEE_students_data_2 dataset. The aes() function maps the BMI variable to the x-axis and uses the Gender variable to fill the density curves by gender.

  • geom_density(colour = "black", alpha = 0.5): Adds a density plot layer to the plot. The colour = "black" argument sets the color of the density curve outlines to black, and the alpha = 0.5 argument sets the transparency of the density curves to 0.5, making them partially transparent.

?

ggplot(SEE_students_data_2,aes(x=BMI, fill=Gender)) + geom_area(stat="bin", colour="black",alpha=0.5,binwidth=1)

Geom_area require the option stat=bin when there is no variable to plot on the Y axis[R] How to communicate with your data? - ggplot2,r語言,python,開發(fā)語言

ggplot(SEE_students_data_2,aes(x=BMI, fill=Gender))+geom_density(colour="black",alpha=0.5)+labs(title="Body Mass index per Gender\nSEE Students", y="Frequency",x="Body Mass Index")

#add a title and axis title to the BMI ?geom_density graph[R] How to communicate with your data? - ggplot2,r語言,python,開發(fā)語言

Unvariate categorical data

#Graphing a factor variable using geom_bar()

ggplot(SEE_students_data_2,aes(x=Gender))+geom_bar()

[R] How to communicate with your data? - ggplot2,r語言,python,開發(fā)語言

?

#adding color to the bar using a set, a given color, manually defined colors
ggplot(SEE_students_data_2,aes(x=Gender, fill=Gender))+geom_bar(alpha=0.5)+scale_fill_brewer(palette="Set1")
ggplot(SEE_students_data_2,aes(x=Gender, fill=Gender))+geom_bar()+scale_fill_brewer(palette = "Blues")
ggplot(SEE_students_data_2,aes(x=Gender,fill=Gender))+geom_bar(alpha=0.75)+scale_fill_manual(values=c("pink","blue"))
  1. ggplot(SEE_students_data_2, aes(x = Gender, fill = Gender)) + geom_bar(alpha = 0.5) + scale_fill_brewer(palette = "Set1"): This code creates a bar plot where each bar is filled with a color from the "Set1" color palette調(diào)色板, which is part of the RColorBrewer釀造師 package. The alpha = 0.5 argument sets the transparency of the bars to 0.5, making them partially transparent.

  2. ggplot(SEE_students_data_2, aes(x = Gender, fill = Gender)) + geom_bar() + scale_fill_brewer(palette = "Blues"): This code creates a bar plot with bars filled with shades of blue from the "Blues" color palette. The bars are fully opaque by default.

  3. Manually defined color: ggplot(SEE_students_data_2, aes(x = Gender, fill = Gender)) + geom_bar(alpha = 0.75) + scale_fill_manual(values = c("pink", "blue")): This code creates a bar plot with bars filled with the colors "pink" and "blue", using the scale_fill_manual() function to manually specify the colors. The alpha = 0.75 argument sets the transparency of the bars to 0.75, making them partially transparent.

[R] How to communicate with your data? - ggplot2,r語言,python,開發(fā)語言[R] How to communicate with your data? - ggplot2,r語言,python,開發(fā)語言

[R] How to communicate with your data? - ggplot2,r語言,python,開發(fā)語言

Order the bar in the right order:

# Install and load the forcats package
install.packages("forcats")
library(forcats)

# Create the plot with the reordered factor levels
ggplot(CUHKSZ_employment_survey_1, aes(fct_infreq(Occupation, palette="Blues")) +
  geom_bar(fill = Occupation, alpha = 0.75) +
  scale_fill_brewer(palette = "Blues")
  • ggplot(CUHKSZ_employment_survey_1, aes(x = fct_infreq(Occupation), fill = Occupation)): This sets up the basic plot using the CUHKSZ_employment_survey_1 dataset. The x aesthetic uses the fct_infreq() function from the forcats package to reorder the Occupation variable based on frequency. The fill aesthetic fills the bars based on the Occupation variable.

  • geom_bar(alpha = 0.75): This adds a bar plot layer to the plot. The alpha parameter sets the transparency of the bars to 0.75, making them partially transparent.

  • scale_fill_brewer(palette = "Blues"): This sets the fill color of the bars using the "Blues" color palette from the RColorBrewer package.

  • the fill = Occupation aesthetic is used to fill the bars of the bar plot based on the levels of the Occupation variable. Each unique level of the Occupation variable will be represented by a different color in the plot, which can be helpful for distinguishing between different categories or groups in the data.

  • additional resources:?STHDA - Homehttp://www.sthda.com/english/文章來源地址http://www.zghlxwxcb.cn/news/detail-838362.html

到了這里,關(guān)于[R] How to communicate with your data? - ggplot2的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • R語言的ggplot2繪制分組折線圖?

    R語言的ggplot2繪制分組折線圖?

    R繪制分組折線圖.R 首先看數(shù)據(jù)情況:group有3組。Time有3組,數(shù)據(jù)意思是在3組3個(gè)時(shí)間點(diǎn)測(cè)量了某指標(biāo),現(xiàn)在要繪制組1、組2、組3某指標(biāo)y按時(shí)間的變化趨勢(shì) 數(shù)據(jù)情況: 看看最終的效果圖如下: 下面是本次使用的代碼 .libPaths () setwd ( \\\"C:/Users/12974/Desktop/百度經(jīng)驗(yàn)/03圖形繪制/03R繪

    2024年01月22日
    瀏覽(19)
  • R語言ggplot2 | R語言繪制物種組成面積圖(三)

    R語言ggplot2 | R語言繪制物種組成面積圖(三)

    ?? 利用R語言繪制物種組成圖。本文以堆疊面積圖的方式與大家分享。 面積圖又叫區(qū)域圖。它是在折線圖的基礎(chǔ)之上形成的, 它將折線圖中折線與自變量坐標(biāo)軸之間的區(qū)域使用顏色或者紋理填充,這樣一個(gè)填充區(qū)域我們叫面積。顏色的填充可以更好地突出趨勢(shì)信息(比如時(shí)

    2024年02月13日
    瀏覽(20)
  • R語言畫圖的-- ggplot2(實(shí)現(xiàn)圖例的精細(xì)修改)

    R語言畫圖的-- ggplot2(實(shí)現(xiàn)圖例的精細(xì)修改)

    ggplot2 是R中用來作圖的很強(qiáng)的包,但是其用法比較多且各種參數(shù)比較復(fù)雜,我自己使用的時(shí)候還經(jīng)常需要查閱一些關(guān)鍵參數(shù)等,因此想要寫一個(gè) r 中 ggplot2 的作圖文檔,方便自己查閱。 但是今天突然發(fā)現(xiàn)了一個(gè)網(wǎng)站,這個(gè)網(wǎng)站里面包含了 ggplot2 作圖的幾乎所有內(nèi)容。有各種圖

    2024年02月13日
    瀏覽(27)
  • R 語言 ggplot2 PCA 主成分分析(虛擬數(shù)據(jù)集)

    R 語言 ggplot2 PCA 主成分分析(虛擬數(shù)據(jù)集)

    以上代碼生成了100行基因,10列樣本的矩陣 前五列命名 wt 開頭+ 1-5 ,表示正?;?后五列命名 ko 開頭+ 1-5 ,表示缺少基因的樣本(knock-out) 給每行基因都統(tǒng)一命名 gene + 1-100 head() 函數(shù)默認(rèn)查看前6行 現(xiàn)在只是定義了矩陣的shape和name,還沒填充數(shù)值 這段代碼的作用是生成一個(gè)

    2024年02月11日
    瀏覽(16)
  • R語言實(shí)踐——ggplot2+ggrepel繪制散點(diǎn)+優(yōu)化注釋文本位置

    R語言實(shí)踐——ggplot2+ggrepel繪制散點(diǎn)+優(yōu)化注釋文本位置

    書接adjustText實(shí)踐——調(diào)整matplotlib散點(diǎn)圖標(biāo)簽,避免重復(fù) 上文中,matplotlib+adjustText對(duì)于我的實(shí)例來說并沒有起到很好的效果。所以,博主決定在R中利用gglot2+ggrepel繪制,期待效果。 博主不常使用R,在此過程中詳細(xì)記錄每一步驟,以作備忘。 2.1 快速繪制散點(diǎn)圖(plot) 2.2 ge

    2023年04月11日
    瀏覽(20)
  • R語言數(shù)據(jù)繪圖學(xué)習(xí)(0x01)-安裝ggplot2與嘗試

    R語言數(shù)據(jù)繪圖學(xué)習(xí)(0x01)-安裝ggplot2與嘗試

    一直聽說數(shù)據(jù)分析里R語言是比較‘正統(tǒng)’,況且久聞ggplot2這些R語言的數(shù)據(jù)分析庫大名,想到今后數(shù)據(jù)分析和整理的需要,這里開一個(gè)簡(jiǎn)單的系列學(xué)習(xí)一些R語言和ggplot2的繪圖基礎(chǔ)。本人學(xué)習(xí)的書籍是Winston Chang大佬的《R Graphics Cookbook》,且稍有一點(diǎn)Python里的Plotnine繪圖基礎(chǔ)。

    2024年02月04日
    瀏覽(16)
  • 跟著NatureMetabolism學(xué)作圖:R語言ggplot2轉(zhuǎn)錄組差異表達(dá)火山圖

    跟著NatureMetabolism學(xué)作圖:R語言ggplot2轉(zhuǎn)錄組差異表達(dá)火山圖

    論文 Independent phenotypic plasticity axes define distinct obesity sub-types https://www.nature.com/articles/s42255-022-00629-2#Sec15 s42255-022-00629-2.pdf 論文中沒有公開代碼,但是所有作圖數(shù)據(jù)都公開了,我們可以試著用論文中提供的數(shù)據(jù)模仿論文中的圖 今天的推文重復(fù)一下論文中的Fig3b 差異表達(dá)火山圖

    2024年02月08日
    瀏覽(18)
  • R語言中使用ggplot2繪制散點(diǎn)圖箱線圖,附加顯著性檢驗(yàn)

    R語言中使用ggplot2繪制散點(diǎn)圖箱線圖,附加顯著性檢驗(yàn)

    散點(diǎn)圖可以直觀反映數(shù)據(jù)的分布,箱線圖可以展示均值等關(guān)鍵統(tǒng)計(jì)量,二者結(jié)合能夠清晰呈現(xiàn)數(shù)據(jù)蘊(yùn)含的信息。 本篇筆記主要內(nèi)容:介紹R語言中繪制箱線圖和散點(diǎn)圖的方法,以及二者結(jié)合展示教程,添加差異比較顯著性分析,繪制如上結(jié)果圖。 在實(shí)際數(shù)據(jù)可視化過程中,輸

    2024年03月20日
    瀏覽(28)
  • how to protect your stomach

    To protect your stomach and maintain good digestive health, here are some tips: Eat a Balanced Diet: Consume a well-balanced diet that includes fruits, vegetables, whole grains, lean proteins, and healthy fats. Avoid excessive consumption of processed foods, sugary snacks, and fatty or fried foods, as they can irritate the stomach lining. Practice Portion Co

    2024年01月21日
    瀏覽(19)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包