简体   繁体   中英

Dynamically parse discrete x-axis labels

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 (ie 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.")

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.")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM