3

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.")
Community
  • 1
  • 1
EngBIRD
  • 1,915
  • 3
  • 18
  • 22
  • 1
    Why not use an ordered factor for x? Factor ordering is then based on c and factor labeling based on x. – CMichael Mar 15 '15 at 09:04

1 Answers1

5

class(xo) investigations made me realize that i was trying to work with an object of type factor which I don't think is not well handled as an argument text in parse.

Rather than try and remove the levels and factors it was just as easy and more stable it seems to convert to a character list (which I had assumed it was all along).

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 <- as.character(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=x))
gg <- gg + scale_x_discrete(labels=parse(text=xo))

print(gg)

print("Program complete - a graph should be visible.")
EngBIRD
  • 1,915
  • 3
  • 18
  • 22