r - Format geom_text label doesn't work when using aes_string -
r - Format geom_text label doesn't work when using aes_string -
i using dot function format text labels in plot created ggplot2
. works fine when using aes
, doesn't work expected when using aes_string
. there workaround create work aes_string
?
require(ggplot2) # define format function dot <- function(x, ...) { format(x, ..., big.mark = ".", scientific = false, trim = true) } # create dummy info df <- data.frame(cbind(levels(iris$species),c(10000000000,200000,30000))) df$x2 <- as.numeric(as.character(df$x2)) # works 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 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")))
rather quote "x2", must quote whole expression
ggplot(iris) + geom_bar(aes(species, sepal.width), stat = "identity") + geom_text(data=df, aes_string(x="x1", y =180, label = "dot(x2)"))
if wanted specify variable names via character vector, utilize paste()
build expression.
r ggplot2
Comments
Post a Comment