Adapted from this answered question: Customize axis labels this this line in the MWE below can parse x-axis labels/values manually specified:
scale_x_discrete(labels=parse(text=c("The~First~Value","A~Second~Value","Finally~Third~Value")))
but any attempt to refer to dynamically replace c()
with xo
(i.e. the ordered list containing the imported expression) fails...
How can I format or adapt this column to work? I am puzzled as the parse command is working fine on the contents of c()
...
In the following simulated data frame, for simplicity, the only character in this example I have included is ~
(to parse and yield space). Full context would contain superscripts, subscripts, symbols, and greek characters imported from an externally managed table.
MWE:
library(ggplot2)
print("Program started")
z <- c("1","2","3")
x <- c("The~First~Value","A~Second~Value","Finally~Third~Value")
s <- c("No","No","No","Yes","Yes","Yes")
y <- c(1,2,3,2,3,4)
df <- as.data.frame(cbind(x=c(x,x),s=s,y=y,z=c(z,z)))
##########################################################################
xo <- as.data.frame(cbind(z,x))
xo <- xo[,"x"]
df[,"x"] <- factor(df[,"x"], levels=xo,ordered=TRUE)
##########################################################################
#xo <- levels(droplevels(xo))
gg <- ggplot(data = df, aes_string(x="x", y="y", weight="y", ymin=paste0("y"), ymax=paste0("y"), fill="s"));
dodge_str <- position_dodge(width = NULL, height = NULL);
gg <- gg + geom_bar(position=dodge_str, stat="identity", size=.3, colour = "black",width=.5)
#gg <- gg + scale_x_discrete(labels=parse(text=c("The~First~Value","A~Second~Value","Finally~Third~Value")))
gg <- gg + scale_x_discrete(labels=parse(text=c(xo)))
print(gg)
print("Program complete - a graph should be visible.")