第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有辦法改變ggplot2中圖例項之間的間距?

有沒有辦法改變ggplot2中圖例項之間的間距?

慕仙森 2019-12-16 10:59:59
有沒有辦法改變ggplot2中圖例項之間的間距?有沒有辦法改變ggplot2中圖例項之間的間距?我目前有l(wèi)egend.position ="top" 自動生成水平圖例。但是,這些項目的間距非常接近,我想知道如何將它們分開得更遠。
查看完整描述

4 回答

?
有只小跳蛙

TA貢獻1824條經(jīng)驗 獲得超8個贊

ggplot2 v3.0.0于2018年7月發(fā)布的版本具有修改的工作選項legend.spacing.x,legend.spacing.y并且legend.text。

示例:增加圖例鍵之間的水平間距


library(ggplot2)


ggplot(mtcars, aes(factor(cyl), fill = factor(cyl))) + 

  geom_bar() +

  coord_flip() +

  scale_fill_brewer("Cyl", palette = "Dark2") +

  theme_minimal(base_size = 14) +

  theme(legend.position = 'top', 

        legend.spacing.x = unit(1.0, 'cm'))



注意:如果只想在圖例文本的右側(cè)擴展間距,請使用 stringr::str_pad()


示例:將圖例鍵標(biāo)簽移至底部并增加垂直間距


ggplot(mtcars, aes(factor(cyl), fill = factor(cyl))) + 

  geom_bar() +

  coord_flip() +

  scale_fill_brewer("Cyl", palette = "Dark2") +

  theme_minimal(base_size = 14) +

  theme(legend.position = 'top', 

        legend.spacing.x = unit(1.0, 'cm'),

        legend.text = element_text(margin = margin(t = 10))) +

  guides(fill = guide_legend(title = "Cyl",

                             label.position = "bottom",

                             title.position = "left", title.vjust = 1)) 



示例:用于scale_fill_xxx&guide_colorbar


ggplot(mtcars, aes(mpg, wt)) +

  geom_point(aes(fill = hp), pch = I(21), size = 5)+

  scale_fill_viridis_c(guide = FALSE) +

  theme_classic(base_size = 14) +

  theme(legend.position = 'top', 

        legend.spacing.x = unit(0.5, 'cm'),

        legend.text = element_text(margin = margin(t = 10))) +

  guides(fill = guide_colorbar(title = "HP",

                               label.position = "bottom",

                               title.position = "left", title.vjust = 1,

                               # draw border around the legend

                               frame.colour = "black",

                               barwidth = 15,

                               barheight = 1.5)) 



對于垂直圖例,設(shè)置legend.key.size只會增加圖例鍵的大小,而不會增加它們之間的垂直間隔


ggplot(mtcars) +

  aes(x = cyl, fill = factor(cyl)) +

  geom_bar() +

  scale_fill_brewer("Cyl", palette = "Dark2") +

  theme_minimal(base_size = 14) +

  theme(legend.key.size = unit(1, "cm"))



為了增加圖例鍵之間的距離,需要修改legend-draw.r功能。有關(guān)更多信息,請參見此問題


# function to increase vertical spacing between legend keys

# @clauswilke

draw_key_polygon3 <- function(data, params, size) {

  lwd <- min(data$size, min(size) / 4)


  grid::rectGrob(

    width = grid::unit(0.6, "npc"),

    height = grid::unit(0.6, "npc"),

    gp = grid::gpar(

      col = data$colour,

      fill = alpha(data$fill, data$alpha),

      lty = data$linetype,

      lwd = lwd * .pt,

      linejoin = "mitre"

    ))

}


# register new key drawing function, 

# the effect is global & persistent throughout the R session

GeomBar$draw_key = draw_key_polygon3


ggplot(mtcars) +

  aes(x = cyl, fill = factor(cyl)) +

  geom_bar() +

  scale_fill_brewer("Cyl", palette = "Dark2") +

  theme_minimal(base_size = 14) +

  theme(legend.key = element_rect(color = NA, fill = NA),

        legend.key.size = unit(1.5, "cm")) +

  theme(legend.title.align = 0.5)


查看完整回答
反對 回復(fù) 2019-12-16
?
天涯盡頭無女友

TA貢獻1831條經(jīng)驗 獲得超9個贊

我認(rèn)為最好的選擇是guide_legend在內(nèi)使用guides:


p + guides(fill=guide_legend(

                 keywidth=0.1,

                 keyheight=0.1,

                 default.unit="inch")

      )

注意使用default.unit,無需加載grid程序包。


查看完整回答
反對 回復(fù) 2019-12-16
?
精慕HU

TA貢獻1845條經(jīng)驗 獲得超8個贊

我用于在水平圖例中添加空間的簡單解決方案,只需在標(biāo)簽中添加空間(請參見下面的摘錄):


  scale_fill_manual(values=c("red","blue","white"),

                    labels=c("Label of category 1          ",

                             "Label of category 2          ",

                             "Label of category 3"))


查看完整回答
反對 回復(fù) 2019-12-16
?
慕斯709654

TA貢獻1840條經(jīng)驗 獲得超5個贊

要在圖例中的條目之間增加間距,請調(diào)整主題元素的邊距l(xiāng)egend.text。


要在每個圖例標(biāo)簽的右側(cè)添加30pt的空間(對于水平圖例可能有用):


p + theme(legend.text = element_text(

    margin = margin(r = 30, unit = "pt")))

在每個圖例標(biāo)簽的左側(cè)添加30pt的空間(對于垂直圖例可能有用):


p + theme(legend.text = element_text(

    margin = margin(l = 30, unit = "pt")))

對于一個ggplot2對象p。關(guān)鍵字是legend.text和margin。


[有關(guān)編輯的注意事項:首次發(fā)布此答案時,存在一個錯誤。該錯誤現(xiàn)已修復(fù)]


查看完整回答
反對 回復(fù) 2019-12-16
  • 4 回答
  • 0 關(guān)注
  • 7626 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號