欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

R語(yǔ)言ggplot2圖例修改超詳細(xì)介紹

 更新時(shí)間:2022年11月03日 15:58:14   作者:醫(yī)學(xué)和生信筆記  
ggplot2是R語(yǔ)言最流行的畫(huà)圖包,基于圖層化語(yǔ)法的思想設(shè)計(jì)和創(chuàng)建美觀優(yōu)雅的圖形,下面這篇文章主要給大家介紹了關(guān)于R語(yǔ)言ggplot2圖例修改的相關(guān)資料,需要的朋友可以參考下

前言

大家經(jīng)常對(duì)ggplot2中的圖例不滿意,想要各種修改,今天就介紹下圖例的各種修改細(xì)節(jié),基本上常用的操作都涉及到了!

library(ggplot2)
library(gcookbook)

移除圖例

提供3種方法可以在ggplot2中移除圖例。

# 基本圖形
pg_plot <- ggplot(PlantGrowth, aes(x = group, y = weight, fill = group)) +
  geom_boxplot()

pg_plot

plot of chunk unnamed-chunk-2

# 首先可以使用guides()函數(shù)移除圖例
pg_plot +
  guides(fill = "none")

plot of chunk unnamed-chunk-3

然后可以在scale_**函數(shù)中移除,這里是fill,你要根據(jù)自己的情況換成shape、color等。

pg_plot+scale_fill_discrete(guide = "none")

plot of chunk unnamed-chunk-4

第3種方法是在theme中移除。

pg_plot+theme(legend.position = "none")

plot of chunk unnamed-chunk-5

改變圖例位置

也是在theme中更改,

pg_plot <- ggplot(PlantGrowth, aes(x = group, y = weight, fill = group)) +
  geom_boxplot() +
  scale_fill_brewer(palette = "Pastel2")

pg_plot +
  theme(legend.position = "top") # 放在頂部

plot of chunk unnamed-chunk-6

legend.position的參數(shù)可以是left、right、top、bottom,還可以是坐標(biāo)。

pg_plot+theme(legend.position = c(0.8,0.3))

plot of chunk unnamed-chunk-7

ggplot2中,左下角的坐標(biāo)是c(0,0),右上角的坐標(biāo)是c(1,1),你可以自己設(shè)置想要放置的位置。需要注意的是,你設(shè)置的這個(gè)坐標(biāo)是圖例中心點(diǎn)的坐標(biāo),可以通過(guò)legend.justification設(shè)置圖例的哪個(gè)位置放在你的坐標(biāo)上。

# 圖例右下角,放在畫(huà)布右下角
pg_plot +
  theme(legend.position = c(1, 0), legend.justification = c(1, 0))

plot of chunk unnamed-chunk-8

p <- ggplot(heightweight, aes(x=ageYear, y=heightIn, shape=sex, colour=sex))+geom_point()
p

plot of chunk unnamed-chunk-9

兩個(gè)圖例更改為水平排列:

p + scale_shape_discrete(label = c("female","male")) +
  theme(legend.direction = "horizontal")

plot of chunk unnamed-chunk-10

修改圖例的邊框和背景

pg_plot+
  theme(legend.position = c(0.85,0.2))+
  theme(legend.background = element_rect(fill = "orange",color = "black"))

plot of chunk unnamed-chunk-11

中間還有一部分是白色,需要另外一個(gè)參數(shù)修改:

pg_plot+
  theme(legend.position = c(0.85,0.2))+
  theme(legend.background = element_rect(fill = "orange",color = "black"),
        legend.key = element_rect(fill = "orange")
        )

plot of chunk unnamed-chunk-12

改變圖例順序

pg_plot <- ggplot(PlantGrowth, aes(x = group, y = weight, fill = group)) +
  geom_boxplot()

pg_plot

plot of chunk unnamed-chunk-13

# limits
pg_plot +
  scale_fill_discrete(limits = c("trt1", "trt2", "ctrl"))

plot of chunk unnamed-chunk-14

也可以在畫(huà)圖之前,通過(guò)因子化的方法把數(shù)據(jù)先排好序再畫(huà)圖哦。

反轉(zhuǎn)圖例順序

2種方法。

pg_plot+scale_fill_discrete(guide = guide_legend(reverse = T))

plot of chunk unnamed-chunk-15

pg_plot+guides(fill = guide_legend(reverse = T))

plot of chunk unnamed-chunk-16

修改圖例標(biāo)題

2種方法。

pg_plot <- ggplot(PlantGrowth, aes(x = group, y = weight, fill = group)) +
  geom_boxplot()

pg_plot

plot of chunk unnamed-chunk-17

labs里面修改

pg_plot + labs(fill = "Condition")

plot of chunk unnamed-chunk-18

scale_*函數(shù)里面修改。

pg_plot+scale_fill_discrete(name = "hahah")

plot of chunk unnamed-chunk-19

還有一種比較復(fù)雜的情況,如果多個(gè)一個(gè)變量映射給多個(gè)圖形參數(shù),或者有多個(gè)圖例,怎么修改呢?像下面這個(gè)例子,sex和shape、color都有關(guān)。

p <- ggplot(heightweight, aes(x=ageYear, y=heightIn, shape=sex, colour=sex))+geom_point()
p

plot of chunk unnamed-chunk-20

可以在scale_*函數(shù)中修改:

p + scale_shape_discrete(name = "shape")+
  scale_color_discrete(name = "colooorrr")

plot of chunk unnamed-chunk-21

也可以在legend中修改:

p + labs(shape = "shapppeee",color = "colooorrr")

plot of chunk unnamed-chunk-22

還可以在guides函數(shù)中修改:

p + guides(shape = guide_legend(title = "this is\nshape"),
           color = guide_legend(title = "cooolor")
           )

plot of chunk unnamed-chunk-23

修改圖例標(biāo)題外觀

pg_plot <- ggplot(PlantGrowth, aes(x = group, y = weight, fill = group)) +
  geom_boxplot()
pg_plot

plot of chunk unnamed-chunk-24

可以在theme中修改:

pg_plot + theme(
  legend.title = element_text(
    face = "italic",
    family = "Times",
    colour = "red",
    size = 18
  )
)

plot of chunk unnamed-chunk-26

也可以在guides中修改:

pg_plot +
  guides(fill = guide_legend(title.theme = element_text(
    face = "italic",
    family = "Times",
    colour = "red",
    size = 14))
  )

plot of chunk unnamed-chunk-27

移除圖例標(biāo)題

pg_plot

plot of chunk unnamed-chunk-29

可以在theme中修改,也可以在scale_xxx函數(shù)中修改,也可以在guides函數(shù)中修改。

pg_plot+theme(legend.title = element_blank())

plot of chunk unnamed-chunk-30

pg_plot+scale_fill_discrete(name = NULL)

plot of chunk unnamed-chunk-30

pg_plot+scale_fill_hue(guide = guide_legend(title = NULL))

plot of chunk unnamed-chunk-30

pg_plot+guides(fill = guide_legend(title = NULL))

plot of chunk unnamed-chunk-30

修改圖例標(biāo)簽

pg_plot

plot of chunk unnamed-chunk-31

pg_plot+scale_fill_discrete(labels = c("label1","label2","label3"))

plot of chunk unnamed-chunk-32

修改圖例標(biāo)簽外觀

在theme中修改:

pg_plot +
  theme(legend.text = element_text(
    colour = "red",
    face = "italic",
    size = 22)
  )

plot of chunk unnamed-chunk-33

總結(jié)

到此這篇關(guān)于R語(yǔ)言ggplot2圖例修改的文章就介紹到這了,更多相關(guān)R語(yǔ)言ggplot2圖例修改內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論