Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically parse discrete x-axis labels

Tags:

parsing

r

ggplot2

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.")
like image 547
EngBIRD Avatar asked Jul 31 '26 08:07

EngBIRD


1 Answers

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.")
like image 187
EngBIRD Avatar answered Aug 02 '26 00:08

EngBIRD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!