Wednesday, 14 August 2013

Formatting aesthetic strings

Formatting aesthetic strings

I would like to format the decimals of a numeric variable mapped as an
aesthetic string, is this possible? Note: I would like to keep this as a
numeric variable and not convert to character as I would like to align the
x-axis scale with another plot.
plotDataTable <- function(df, .x, .y, .txt, minDec=1){
p <- ggplot(df, aes_string(x = .x, y = .y)) +
geom_text(aes_string(label=format(.txt, nsmall=minDec,
justify="right")), size = 3, hjust=1) +
theme_bw() +
labs(x = NULL, y = NULL) +
scale_y_continuous(limits=c(-0.01,0.01), breaks=0, labels= "Freq") +
theme(panel.grid.major = element_blank(),
panel.border = element_blank(),
axis.text.x = element_blank(),
axis.ticks = element_blank(),
plot.margin = unit(c(0, 1, 0, 0.5), "lines")) #top, right,
bottom, left
return(p)
}
dat <- data.frame(x=0:4, y = 0, freq=c(80.3, 10, 5, 3, 2))
data_table <- plotDataTable(dat, "x", "y", "freq")
Thanks.

No comments:

Post a Comment