国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - R語言 - R語言 使用ggplot2繪制好看的分組散點圖

R語言 使用ggplot2繪制好看的分組散點圖

2022-01-04 15:48yepeng2007fei R語言

這篇文章主要介紹了R語言 使用ggplot2繪制好看的分組散點圖操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

我們以iris數據集為例,該數據集包括花萼的長度和寬度,花瓣的長度和寬度,以及物種,如下圖:

R語言 使用ggplot2繪制好看的分組散點圖

本文我們要繪制不同物種下花萼的長度和寬度的分布情況,以及二者之間的相關性關系。

1. 首先載入ggplot2包,

library(ggplot2)

2. 然后進行ggplot(data = NULL, mapping = aes(), ..., environment = parent.frame())繪制,在繪制中第一個參數是數據,第二個參數是數據映射,是繪制的全局變量,其中包含的參數有x,y,color,size,alpha,shape等。

例如:ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)),然后通過快捷散點繪制

+geom_point(size = 2.0, shape = 16),顏色代表不同的物種,如下圖:

R語言 使用ggplot2繪制好看的分組散點圖

3. 上面顯示的是最原始的散點繪制,通過顏色區分不同的物種,那么如何進行效果的提升呢?

首先是可以進行分面,使得不同物種的對比效果更為顯著,這里使用+facet_wrap( ~ Species),效果如下:

R語言 使用ggplot2繪制好看的分組散點圖

4. 通過分面后對比效果好了不少,如果想看下不同物種下花萼長度與寬度的關系呢?可以使用+geom_smooth(method = "loess"),效果圖如下:

R語言 使用ggplot2繪制好看的分組散點圖

5. 通過上面的操作效果好了很多,但是還是感覺不夠高大上,那我們可以使用library(ggthemes)這個包進行精修一下,通過修改theme,使用+theme_solarized(),效果如下:

R語言 使用ggplot2繪制好看的分組散點圖

還有更多的theme選擇,例如+theme_wsj(),效果如下:

R語言 使用ggplot2繪制好看的分組散點圖

這樣我們的圖是不是高大上了很多呢,所以其實數據可視化也沒有多難。最后給下源碼:

library(ggthemes)
library(ggplot2)

ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point(size = 2.0, shape = 16) +
facet_wrap( ~ Species) +
geom_smooth(method = "loess")+

theme_wsj()

補充:R語言 畫圖神器ggplot2包

 

ggplot2

R語言里畫圖最好用的包啦。感覺圖都挺清晰的,就懶得加文字了(或者以后回來補吧>.)前面幾個圖挺基礎的,后面也許會有沒見過的ggplot用法哦。

Install Package

install.packages("ggplot2")
library(ggplot2)

 

Scatter Plot

為了方便展示,用gapminder的數據

if(!require(gapminder)) install.packages("gapminder")
library(gapminder)
gapminder

數據大概是這樣的

R語言 使用ggplot2繪制好看的分組散點圖

假設我們現在想要知道2007年lifeExp和人均GDP之間的關系。

先篩選數據

library(dplyr)
gapminder_2007 <- gapminder %>%
filter(year == 2007)

畫lifeExp和gdpPercap關系的散點圖,x為gdpPercap,y為lifeExp。

ggplot(gapminder_2007,aes(x = gdpPercap, y = lifeExp))+geom_point()

R語言 使用ggplot2繪制好看的分組散點圖

看的出來lifeExp與gdpPercap存在近似lifeExp=log(gdpPercap)的關系,對x軸的數值進行log值處理。另外,為了呈現更多信息,用顏色標記國家所在的洲,并用點的大小表示人口數量。

ggplot(gapminder_2007,aes(x = gdpPercap, y = lifeExp, color = continent, size = pop))+
geom_point()+scale_x_log10()+theme_minimal()+
labs(x = "GDP per capita",
y = "Life expectancy",
title = "Life expectancy increases as GDP per capita increases",
caption = "Data source: gapminder")

R語言 使用ggplot2繪制好看的分組散點圖

另外一種呈現方式如下:

加入了回歸線和坐標軸的histogram。

plot <- ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp)) + 
geom_point()+geom_smooth(method="lm")+scale_x_log10()+
labs(x = "GDP per capita",
y = "Life expectancy",
title = "Life expectancy increases as GDP per capita increases",
caption = "Data source: gapminder")
ggMarginal(plot, type = "histogram", fill="transparent")
#ggMarginal(plot, type = "boxplot", fill="transparent")

R語言 使用ggplot2繪制好看的分組散點圖

Histogram

gapminder_gdp2007 <- gapminder %>%
filter(year == 2007, continent == "Americas") %>%
mutate(country = fct_reorder(country,gdpPercap,last))
ggplot(gapminder_gdp2007, aes(x=country, y = gdpPercap))+
geom_col(fill="skyblue", color="black")+
labs(x = "Country",
y = "GDP per capita",
title = "GDP per capita in North America and South America, 2007",
caption = "Data source: gapminder")+
coord_flip()+theme_minimal()

R語言 使用ggplot2繪制好看的分組散點圖

Line Plot

gapminder_pop <- gapminder %>%
filter(country %in% c("United States","China"))
ggplot(gapminder_pop,aes(x = year, y = pop, color = country))+
geom_line(lwd = 0.8)+theme_light()+
labs(x = "Year",
y = "Population",
title = "Population in China and United States, 1953-2007",
caption = "Data source: gapminder")

R語言 使用ggplot2繪制好看的分組散點圖

Facet Plot

gapminder_gdp <- gapminder %>%
group_by(year, continent) %>%
summarize(avg_gdp = mean(gdpPercap))
ggplot(gapminder_gdp,aes(x = year, y = avg_gdp, color = continent))+
geom_line(lwd = 0.8)+theme_light()+facet_wrap(~continent)+
labs(x = "Year",
y = "Average GDP per capita",
title = "Average GDP per capita change in different continent",
caption = "Data source: gapminder")+
scale_x_continuous(breaks=c(1955,1970,1985,2000))

R語言 使用ggplot2繪制好看的分組散點圖

Path Plot

gapminder_lifeexp <- gapminder %>%
filter(year %in% c(1957,2007), continent == "Europe") %>%
arrange(year) %>%
mutate(country = fct_reorder(country,lifeExp,last))
ggplot(gapminder_lifeexp) +geom_path(aes(x = lifeExp, y = country),
arrow = arrow(length = unit(1.5, "mm"), type = "closed")) +
geom_text(
aes(x = lifeExp,
y = country,
label = round(lifeExp, 1),
hjust = ifelse(year == 2007,-0.2,1.2)),
size =3,
family = "Bookman",
color = "gray25")+
scale_x_continuous(limits=c(45, 85))+
labs(
x = "Life expectancy",
y = "Country",
title = "People live longer in 2007 compared to 1957",
subtitle = "Life expectancy in European countries",
caption = "Data source: gapminder"
)

R語言 使用ggplot2繪制好看的分組散點圖

Density Plot

gapminder_1992 <- gapminder %>%
filter(year == 1992)
ggplot(gapminder_1992, aes(lifeExp))+theme_classic()+
geom_density(aes(fill=factor(continent)), alpha=0.8) + 
labs(
x="Life expectancy",
title="Life expectancy group by continent, 1992", 
caption="Data source: gapminder",
fill="Continent")

R語言 使用ggplot2繪制好看的分組散點圖

Slope Chart

gapminder_lifeexp2 <- gapminder %>%
filter(year %in% c(1977,1987,1997,2007),
country %in% c("Canada", "United States","Mexico","Haiti","El Salvador",
"Guatemala","Jamaica")) %>%
mutate(lifeExp = round(lifeExp))
ylabs <- subset(gapminder_lifeexp2, year==head(year,1))$country
yvals <- subset(gapminder_lifeexp2, year==head(year,1))$lifeExp
ggplot(gapminder_lifeexp2, aes(x=as.factor(year),y=lifeExp)) +
geom_line(aes(group=country),colour="grey80") +
geom_point(colour="white",size=8) +
geom_text(aes(label=lifeExp), size=3, color = "black") +
scale_y_continuous(name="", breaks=yvals, labels=ylabs)+
theme_classic()+
labs(title="Life Expectancy of some North America countries change from 1977 to 2007") + 
theme(axis.title=element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(hjust=0.5))

R語言 使用ggplot2繪制好看的分組散點圖

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。如有錯誤或未考慮完全的地方,望不吝賜教。

原文鏈接:https://blog.csdn.net/yepeng2007fei/article/details/80696713

延伸 · 閱讀

精彩推薦
  • R語言R語言常量知識點總結

    R語言常量知識點總結

    在本篇文章里小編給大家整理了一篇關于R語言常量知識點總結內容,有興趣的朋友們可以學習分享下。...

    R語言教程網12102021-12-29
  • R語言R語言中的vector(向量),array(數組)使用總結

    R語言中的vector(向量),array(數組)使用總結

    這篇文章主要介紹了R語言中的vector(向量),array(數組)使用總結,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要...

    A葉子葉來5772021-11-14
  • R語言如何用R語言繪制散點圖

    如何用R語言繪制散點圖

    這篇文章主要介紹了如何用R語言繪制散點圖,幫助大家更好的理解和學習使用R語言,感興趣的朋友可以了解下...

    菜鳥教程13002021-12-23
  • R語言R語言gsub替換字符工具的具體使用

    R語言gsub替換字符工具的具體使用

    這篇文章主要介紹了R語言gsub替換字符工具的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友...

    lztttao10372021-12-24
  • R語言R語言中qplot()函數的用法說明

    R語言中qplot()函數的用法說明

    這篇文章主要介紹了R語言中qplot()函數的用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧...

    Jack_丁明12752022-01-05
  • R語言R語言實現支持向量機SVM應用案例

    R語言實現支持向量機SVM應用案例

    本文主要介紹了R語言實現支持向量機SVM應用案例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    一天_pika5222022-01-18
  • R語言基于R/RStudio中安裝包“無法與服務器建立連接”的解決方案

    基于R/RStudio中安裝包“無法與服務器建立連接”的解決方案

    這篇文章主要介紹了基于R/RStudio中安裝包“無法與服務器建立連接”的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧...

    truffle52815052022-01-05
  • R語言R語言讀取xls與xlsx格式文件過程

    R語言讀取xls與xlsx格式文件過程

    這篇文章主要為大家介紹了使用R語言讀取xls與xlsx格式文件的過程步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪...

    Kanny廣小隸11982022-01-20
主站蜘蛛池模板: 成人国产精品视频 | av免费网站 | 亚洲视频在线观看 | 日韩中文一区 | 国产精品免费视频观看 | 黄色av免费在线播放 | 久久久99999 亚洲一区 | 久草在线视频免费播放 | 国产精品成人一区二区三区夜夜夜 | 亚洲国产婷婷香蕉久久久久久99 | 丝袜+亚洲+另类+欧美+变态 | 色天天综合久久久久综合片 | 久久久亚洲国产美女国产盗摄 | 免费一级片在线观看 | 精品久久久久久久久久久久 | 日韩一区二区三区在线 | 亚洲淫视频 | 欧州一区二区三区 | 91麻豆精品国产91久久久资源速度 | 欧美精产国品一二三区 | 久久久五月天 | 一级做a爰片久久毛片免费陪 | 国产毛片视频 | 精品无码久久久久国产 | 九九热视频精品在线观看 | 特黄特黄的视频 | 免费午夜电影 | 久久国产精品久久久久久 | 中文字幕在线观看视频地址二 | 最新av在线 | 热久久国产 | 欧美国产视频 | 日韩片一区 | 色婷婷综合久久久中文字幕 | 自拍视频网 | 先锋av在线资源 | 久久久久久久久久久久99 | 永久免费av| 不卡免费视频 | 性色av一区二区三区红粉影视 | 精品av|