R ggplot2 가변너비 막대그래프

1 개요[ | ]

R ggplot2 가변너비 막대그래프

2 너비 지정 ★[ | ]

df <- data.frame(class=c("A","B","C"), score=c(60, 30, 90), width=c(.1, .3, .5))
df
library(ggplot2)
ggplot(df, aes(x=class, y=score, width=width)) + 
  geom_bar(aes(fill=class), stat="identity")

3 너비 지정 + 위치 계산[ | ]

df <- data.frame(class=c("A","B","C"), score=c(60, 30, 90), width=c(20, 30, 40))
df
df$xmax <- cumsum(df$width)
df$xmin <- df$xmax - df$width
df$labelx <- (df$xmax + df$xmin)/2
df
library(ggplot2)
g <- ggplot(df, aes(ymin=0))
g2 <- g + geom_rect(aes(xmin=xmin, xmax=xmax, ymax=score, fill=class))
g2
g3 <- g2 + geom_text(aes(x=labelx, y=score/2, label=class)) + labs(x="width",y="score")
g3

4 같이 보기[ | ]

5 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}