tft每日頭條

 > 生活

 > ggplot2改變坐标軸屬性

ggplot2改變坐标軸屬性

生活 更新时间:2024-08-04 02:08:41
背景介紹

ggplot中有時候需要做一些分面圖,那麼對于這些分面的字體、顔色、背景是怎麼修改的呢?今天我們來看一下!

軟件介紹

[軟件名稱]:R(4.1.2)

[軟件名稱]:RStudio(1.4.1106)

教程介紹

1.加載需要的包,我一般喜歡直接加載tidyverse

library(tidyverse)

2.首先我們使用R語言内置的數據集繪制一個圖

ggplot(iris,aes(Sepal.Length,Sepal.Width)) geom_point(aes(size=Petal.Length,color=Petal.Width)) facet_grid(.~Species)

ggplot2改變坐标軸屬性(ggplot2中facetgrid的分面字體顔色和背景的修改)1

3.然後使用windowsFonts提取系統字體Times New Roman,使用scale_color_gradient修改顔色範圍

windowsFonts(A=windowsFont("Times New Roman")) ggplot(iris,aes(Sepal.Length,Sepal.Width)) geom_point(aes(size=Petal.Length,color=Petal.Width)) facet_grid(.~Species) scale_color_gradient(low = "blue",high = "red") theme(text=element_text("A",size=15))

ggplot2改變坐标軸屬性(ggplot2中facetgrid的分面字體顔色和背景的修改)2

4.使用strip.background可以對分面的背景進行修改,使用strip.text可以對分面的字體的顔色,大小進行修改

windowsFonts(A=windowsFont("Times New Roman")) ggplot(iris,aes(Sepal.Length,Sepal.Width)) geom_point(aes(size=Petal.Length,color=Petal.Width)) facet_grid(.~Species) scale_color_gradient(low = "blue",high = "red") theme(text=element_text("A",size=15)) theme(strip.background = element_rect(fill=c("#FF6A6A"))) theme(strip.text = element_text(size = 15,colour = "blue"))

ggplot2改變坐标軸屬性(ggplot2中facetgrid的分面字體顔色和背景的修改)3

5.如果要修改分面的名稱呢?怎麼弄:使用levels函數提取出裡面的元素,然後賦值給其新的名稱即可,如下所示

levels(iris$Species)[levels(iris$Species)=="setosa"] <- "分面1" levels(iris$Species)[levels(iris$Species)=="versicolor"] <- "分面2" levels(iris$Species)[levels(iris$Species)=="virginica"] <- "分面3"

6.代碼重新運行,分面名稱即可改變

windowsFonts(A=windowsFont("Times New Roman")) ggplot(iris,aes(Sepal.Length,Sepal.Width)) geom_point(aes(size=Petal.Length,color=Petal.Width)) facet_grid(.~Species) scale_color_gradient(low = "blue",high = "red") theme(text=element_text("A",size=15)) theme(strip.background = element_rect(fill=c("#FF6A6A"))) theme(strip.text = element_text(size = 15,colour = "blue"))

ggplot2改變坐标軸屬性(ggplot2中facetgrid的分面字體顔色和背景的修改)4

7.好了,今天就學習一下這個技巧!

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关生活资讯推荐

热门生活资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved