I am using the dot function to format text labels in a plot created with ggplot2
. This works fine when using aes
, but doesn't work like expected when using aes_string
. Is there a workaround to make it work with aes_string
?
require(ggplot2)
# Define the format function
dot <- function(x, ...) {
format(x, ..., big.mark = ".", scientific = FALSE, trim = TRUE)
}
# Create dummy data
df <- data.frame(cbind(levels(iris$Species),c(10000000000,200000,30000)))
df$X2 <- as.numeric(as.character(df$X2))
# Works with aes
ggplot(iris) +
geom_bar(aes(Species,Sepal.Width),stat="identity") +
geom_text(data=df,aes(x=factor(X1),y=180,label=dot(X2)))
# Doesn't work with aes_string
ggplot(iris) +
geom_bar(aes(Species,Sepal.Width),stat="identity") +
geom_text(data=df,aes_string(x="X1",y=180,label=dot("X2")))