溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在R語(yǔ)言中使用ggplot2繪制分組散點(diǎn)圖

發(fā)布時(shí)間:2021-04-01 16:09:18 來(lái)源:億速云 閱讀:4839 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)怎么在R語(yǔ)言中使用ggplot2繪制分組散點(diǎn)圖,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

1. 首先載入ggplot2包,

library(ggplot2)

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

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

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

怎么在R語(yǔ)言中使用ggplot2繪制分組散點(diǎn)圖

3. 上面顯示的是最原始的散點(diǎn)繪制,通過(guò)顏色區(qū)分不同的物種,那么如何進(jìn)行效果的提升呢?

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

怎么在R語(yǔ)言中使用ggplot2繪制分組散點(diǎn)圖

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

怎么在R語(yǔ)言中使用ggplot2繪制分組散點(diǎn)圖

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

怎么在R語(yǔ)言中使用ggplot2繪制分組散點(diǎn)圖

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

怎么在R語(yǔ)言中使用ggplot2繪制分組散點(diǎn)圖

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

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()

補(bǔ)充:R語(yǔ)言 畫(huà)圖神器ggplot2包

ggplot2

R語(yǔ)言里畫(huà)圖最好用的包啦。感覺(jué)圖都挺清晰的,就懶得加文字了(或者以后回來(lái)補(bǔ)吧>.)前面幾個(gè)圖挺基礎(chǔ)的,后面也許會(huì)有沒(méi)見(jiàn)過(guò)的ggplot用法哦。

Install Package

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

Scatter Plot

為了方便展示,用gapminder的數(shù)據(jù)

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

數(shù)據(jù)大概是這樣的

怎么在R語(yǔ)言中使用ggplot2繪制分組散點(diǎn)圖

假設(shè)我們現(xiàn)在想要知道2007年lifeExp和人均GDP之間的關(guān)系。

先篩選數(shù)據(jù)

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

畫(huà)lifeExp和gdpPercap關(guān)系的散點(diǎn)圖,x為gdpPercap,y為lifeExp。

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

怎么在R語(yǔ)言中使用ggplot2繪制分組散點(diǎn)圖

看的出來(lái)lifeExp與gdpPercap存在近似lifeExp=log(gdpPercap)的關(guān)系,對(duì)x軸的數(shù)值進(jìn)行l(wèi)og值處理。另外,為了呈現(xiàn)更多信息,用顏色標(biāo)記國(guó)家所在的洲,并用點(diǎn)的大小表示人口數(shù)量。

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語(yǔ)言中使用ggplot2繪制分組散點(diǎn)圖

另外一種呈現(xiàn)方式如下:

加入了回歸線和坐標(biāo)軸的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語(yǔ)言中使用ggplot2繪制分組散點(diǎn)圖

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語(yǔ)言中使用ggplot2繪制分組散點(diǎn)圖

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語(yǔ)言中使用ggplot2繪制分組散點(diǎn)圖

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語(yǔ)言中使用ggplot2繪制分組散點(diǎn)圖

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語(yǔ)言中使用ggplot2繪制分組散點(diǎn)圖

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語(yǔ)言中使用ggplot2繪制分組散點(diǎn)圖

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語(yǔ)言中使用ggplot2繪制分組散點(diǎn)圖

關(guān)于怎么在R語(yǔ)言中使用ggplot2繪制分組散點(diǎn)圖就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI