programing

ggplot gem_text 글꼴 크기 컨트롤

elecom 2023. 7. 7. 18:33
반응형

ggplot gem_text 글꼴 크기 컨트롤

저는 제 막대 그래프의 레이블에 대해 글꼴을 10으로 변경하려고 했습니다.ggplot2다음과 같은 일을 함으로써:

ggplot(data=file,aes(x=V1,y=V3,fill=V2)) +
    geom_bar(stat="identity",position="dodge",colour="white") + 
    geom_text(aes(label=V2),position=position_dodge(width=0.9),
                                                 hjust=1.5,colour="white") +
    theme_bw()+theme(element_text(size=10))

ggsave(filename="barplot.pdf",width=4,height=4)

그러나 결과 이미지는 막대 그래프 레이블에 대해 매우 큰 글꼴 크기를 가집니다.

그리고 나서 수정할 생각을 했습니다.geom_text()이것으로:

geom_text(size=10,aes(label=V2),position=position_dodge(width=0.9),
                                                   hjust=1.5,colour="white")

레이블 글꼴이 더 큽니다...

안에서 사이즈를 변경할 수 있습니다.geom_text3과 같은 것으로, 이제는 축 레이블과 유사한 글꼴 10처럼 보입니다.

무슨 일이죠?한다theme(text=element_text(size=10))라벨에는 적용되지 않습니까?

그리고 왜 10인치의 크기가geom_text()에 있어서와 다릅니다.theme(text=element_text())?

다음은 텍스트/라벨 크기를 변경하기 위한 몇 가지 옵션입니다.

library(ggplot2)

# Example data using mtcars

a <- aggregate(mpg ~ vs + am , mtcars, function(i) round(mean(i)))

p <- ggplot(mtcars, aes(factor(vs), y=mpg, fill=factor(am))) + 
            geom_bar(stat="identity",position="dodge") + 
            geom_text(data = a, aes(label = mpg), 
                            position = position_dodge(width=0.9),  size=20)

size에서geom_text의 크기를 변경합니다.geom_text라벨

p <- p + theme(axis.text = element_text(size = 15)) # changes axis labels

p <- p + theme(axis.title = element_text(size = 25)) # change axis titles

p <- p + theme(text = element_text(size = 10)) # this will change all text size 
                                                             # (except geom_text)


를 위해 gem_text()에서 10의 크기가 테마(text=text_text())의 크기와 다른 이유는 무엇입니까?

네, 그들은 다릅니다.간단한 수동 확인을 해보니 ~(14/5)의 비율인 것 같습니다.geom_text에의 크기.theme크기

따라서 균일한 크기에 대한 끔찍한 해결책은 이 비율로 확장하는 것입니다.

geom.text.size = 7
theme.size = (14/5) * geom.text.size

ggplot(mtcars, aes(factor(vs), y=mpg, fill=factor(am))) + 
  geom_bar(stat="identity",position="dodge") + 
  geom_text(data = a, aes(label = mpg), 
            position = position_dodge(width=0.9),  size=geom.text.size) + 
  theme(axis.text = element_text(size = theme.size, colour="black")) 

물론 이것은 이유를 설명하지 못합니다. 그리고 피타입니다(그리고 저는 이것을 하는 더 합리적인 방법이 있다고 생각합니다).

ggplot2의 사용자 지정 FAQ에서 관련 항목을 확인하십시오. https://ggplot2.tidyverse.org/articles/faq-customising.html#what-is-the-default-size-of-geom_text-and-how-can-i-change-the-font-size-of-geom_text

의 기본 크기를 수정할 수 있습니다.geom_text()배치하여update_geom_defaults("text", list(size = X)여기서 X는 스크립트 시작 부분에서 새 크기를 선택한 것입니다.

언급URL : https://stackoverflow.com/questions/25061822/ggplot-geom-text-font-size-control

반응형