ตัดป้ายกำกับแกนยาวผ่าน labeller=label_wrap ใน ggplot2

ฉันต้องการล้อมป้ายกำกับของฉันใน ggplot2 โดยอัตโนมัติ เช่น แทรกตัวแบ่งบรรทัดของป้ายกำกับแบบยาว ที่นี่ มีการเขียนวิธีการเขียนฟังก์ชัน (1) สำหรับมัน แต่น่าเสียดายที่ฉันไม่ทำ รู้ว่าจะใส่ labeller=label_wrap ไว้ที่ไหนในโค้ดของฉัน (2)

(1) ฟังก์ชั่นโดยแฮดลีย์

label_wrap <- function(variable, value) {
  lapply(strwrap(as.character(value), width=25, simplify=FALSE), 
         paste, collapse="\n")
}

(2) ตัวอย่างรหัส

df = data.frame(x = c("label", "long label", "very, very long label"), 
                y = c(10, 15, 20))

ggplot(df, aes(x, y)) + geom_bar(stat="identity")

ฮิสโตแกรมที่มีป้ายกำกับยาวไม่ถูกห่อ

ฉันอยากจะรวมป้ายกำกับที่ยาวกว่านี้ไว้ที่นี่


person Til Hund    schedule 19.02.2014    source แหล่งที่มา
comment
ทำไมคุณไม่ใช้ฟังก์ชัน strwrap กับป้ายกำกับของคุณนอก ggplot2   -  person baptiste    schedule 19.02.2014
comment
ฉันยังใหม่กับ R ฉันจะใช้ฟังก์ชัน strwrap ภายนอกได้อย่างไร   -  person Til Hund    schedule 19.02.2014
comment
โปรดใส่ตัวอย่างที่สามารถทำซ้ำได้   -  person Max Ghenis    schedule 12.08.2016


คำตอบ (4)


คุณไม่จำเป็นต้องใช้ฟังก์ชัน label_wrap ให้ใช้ฟังก์ชัน str_wrap จากแพ็คเกจ stringr แทน

คุณไม่ได้ระบุเฟรมข้อมูล df ดังนั้นฉันจึงสร้างเฟรมข้อมูลแบบธรรมดาซึ่งมีป้ายกำกับของคุณ จากนั้นใช้ฟังก์ชัน str_wrap กับป้ายกำกับ

library(ggplot2)
library(stringr)

df = data.frame(x = c("label", "long label", "very, very long label"), 
                y = c(10, 15, 20))
df

df$newx = str_wrap(df$x, width = 10)
df

ตอนนี้เพื่อใช้ป้ายกำกับกับแผนภูมิ ggplot: แผนภูมิแรกใช้ป้ายกำกับดั้งเดิม แผนภูมิที่สองใช้ป้ายกำกับที่แก้ไข และสำหรับแผนภูมิที่สาม ป้ายกำกับจะได้รับการแก้ไขในการเรียก ggplot

ggplot(df, aes(x, y)) + 
  xlab("") + ylab("Number of Participants") +
  geom_bar(stat = "identity") 

ggplot(df, aes(newx, y)) + 
  xlab("") + ylab("Number of Participants") +
  geom_bar(stat = "identity")

ggplot(df, aes(x, y)) + 
  xlab("") + ylab("Number of Participants") +
  geom_bar(stat = "identity") +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 10))

ป้อนคำอธิบายรูปภาพที่นี่

person Sandy Muspratt    schedule 19.02.2014
comment
วิธีนี้ดูเหมือนจะใกล้เคียงกับปัญหาของฉันมาก แต่จะเกิดอะไรขึ้นถ้ากราฟทุก ๆ สามกราฟมีป้ายกำกับต่างกัน ฉันต้องกำหนดฟังก์ชันที่แตกต่างกันสามฟังก์ชัน(x) เพื่อที่จะใช้ str_wrap หรือไม่ - person Til Hund; 20.02.2014
comment
ฉันขอโทษ ฉันไม่เข้าใจคำถามของคุณ การเรียกครั้งที่สองไปยัง ggplot คือหนทางไป นั่นคือ ใช้ str_wrap นอกการเรียกไปยัง ggplot หากคุณมีตัวแปรอื่นที่มีป้ายกำกับยาว ให้ใช้ str_wrap กับตัวแปรเหล่านั้นด้วย - person Sandy Muspratt; 20.02.2014
comment
ฉันคิดว่าเราเกือบจะถึงจุดนั้นแล้ว แต่ฉันไม่ค่อยเข้าใจวิธีนำ str_wrap ไปใช้กับตัวแปรทั้งหมดของฉัน ดังนั้นกราฟ (ประมาณ 25 ตัว) ซึ่งมีป้ายกำกับที่แตกต่างกันทั้งหมด (ดูโค้ดตัวอย่างด้านบนอีกครั้ง) - person Til Hund; 21.02.2014
comment
นี่คือ @SandyMuspratt ที่ยอดเยี่ยม หมายเหตุ: กำหนด dataframe ของคุณด้วย variable และ value แทนที่จะเป็น x และ y และจะเห็นได้ชัดว่าไวยากรณ์คือ scale_x_discrete(labels = function(x) str_wrap(x, width = 10)) โดยใช้ x และไม่ใช่ variable เนื่องจากฉันถือว่าไม่ถูกต้องในครั้งแรก - person PatrickT; 14.12.2014
comment
นอกจากนี้ คุณอาจต้องการเพิ่ม + theme(axis.text.x = element_text(hjust=0)) เพื่อจัดแนวป้ายกำกับไปทางซ้าย (ในตัวอย่างของคุณ เส้นทั้ง 3 เส้นจะดูอยู่ตรงกลาง) - person PatrickT; 14.12.2014
comment
ปัญหาใหญ่คือ str_wrap เปลี่ยนปัจจัยเป็นอักขระ ดังนั้นลำดับระดับของปัจจัยจึงหายไป - person Dan Chaltiel; 20.11.2017

แพ็คเกจ "scales" มีฟังก์ชันที่คล้ายกับของ Claude's และ Leonardo's: wrap_format

library(scales)
ggplot(df, aes(x, y)) + geom_bar(stat = "identity") + 
    labs(x = "", y = "Number of Participants") + 
    scale_x_discrete(labels = wrap_format(10))
person Nicolás Velásquez    schedule 03.08.2017
comment
ข้อดีของวิธีนี้เมื่อเปรียบเทียบกับโซลูชันของ Sandy ก็คือการรักษาลำดับของตัวประกอบไว้ - person AndrewMinCH; 15.10.2019

นี่เป็นอีกวิธีหนึ่งที่ไม่มีการอ้างอิงถึงไลบรารี stringr:

ggplot(df, aes(x, y)) + 
  xlab("") + ylab("Number of Participants") +
  geom_bar(stat = "identity") +
  scale_x_discrete(labels = function(x) lapply(strwrap(x, width = 10, simplify = FALSE), paste, collapse="\n"))

โทรที่ไหน:

lapply(strwrap(x, width = 10, simplify = FALSE), paste, collapse="\n")

ทำหน้าที่แยกป้ายกำกับแบบไดนามิก ผลลัพธ์จะเหมือนกับใน คำตอบ แรก

person Claude    schedule 05.08.2016

(หวังว่า) ปรับปรุงคำตอบของ @Claude:

get_wraper <- function(width) {
    function(x) {
        lapply(strwrap(x, width = width, simplify = FALSE), paste, collapse="\n")
    }
}

ggplot(df, aes(x, y)) + geom_bar(stat = "identity") + 
    labs(x = "", y = "Number of Participants") + 
    scale_x_discrete(labels = get_wraper(10))
person Leonardo Fontenelle    schedule 04.10.2016
comment
@Leonardo_Fontenelle มีฟอนต์เดียวก็พอแล้ว function(x,width) - person timat; 24.11.2016