## ggplot displaying expression in x axis

ggplot expression subscript

ggplot2 superscript annotation

ggplot subscript in legend

ggtitle expression

ggtitle subscript

Below, I have R code that plots a grouped bar plot.

group_name = c('A_1x', 'A_1x', 'A_2x', 'A_2x', 'A_3x', 'A_3x', 'A_4x', 'A_4x') mydata2 <- data.frame(mygroup = group_name, mysubgroup = factor(c("Yes", "No"), levels = c("Yes", "No")), value = c(60,40,90,10,55,45,88,12)) ggplot(mydata2, aes(mygroup, value, fill = mysubgroup)) + geom_bar(position = "dodge", width = 0.5, stat = "identity")+ coord_flip()

Currently, the plot looks like below. However, I want to show expressions in the x axis as shown in the below picture.

I have tried this:

group_name = c(expression(A[1*x]),expression(A[1*x]), expression(A[2*x]),expression(A[2*x]), expression(A[3*x]),expression(A[3*x]), expression(A[4*x]),expression(A[4*x]))

But it gives the following error:

Error in as.data.frame.default(x[[i]], optional = TRUE) : cannot coerce class ""expression"" to a data.frame

How to fix it?

Here is a working example - I changed the `group_name`

to 4 elements instead of 8 and manually added them into the `ggplot`

expression. The issue was that the expression type can't be a column name for a `data.frame`

. This escapes that issue.

library(ggplot2) group_name = c('A_1x', 'A_1x', 'A_2x', 'A_2x', 'A_3x', 'A_3x', 'A_4x', 'A_4x') mydata2 <- data.frame(mygroup = group_name, mysubgroup = factor(c("Yes", "No"), levels = c("Yes", "No")), value = c(60,40,90,10,55,45,88,12)) group_name = c(expression(A[1*x]), expression(A[2*x]), expression(A[3*x]), expression(A[4*x])) ggplot(mydata2, aes(mygroup, value, fill = mysubgroup)) + geom_bar(position = "dodge", width = 0.5, stat = "identity")+ coord_flip() + scale_x_discrete(labels=group_name) # Adding the labels here

**R Basics,** We can modify both axes and legends. ggplot2 actually considers these allow the first one to just be a string and the second to be a mathematical expression. p <- ggplot(df, aes(x, y)) + geom_point(aes(colour = z)) p1 = p + xlab("X axis") + # can also be used to add a duplicate guide p + guides (x = guide_axis (n.dodge = 2), y.sec = guide_axis ()) Contents ggplot2 is a part of the tidyverse , an ecosystem of packages designed with common APIs and a shared philosophy.

`labels`

can be a function,

group_name = sprintf("A[%i*x]", rep(1:4,each=2)) # alternatively, use gsub with your original vector # group_name = c('A_1x', 'A_1x', 'A_2x', 'A_2x', 'A_3x', 'A_3x', 'A_4x', 'A_4x') # gsub("A_([0-9])x","A[\\1*x]", group_name) mydata2 <- data.frame(mygroup = group_name, mysubgroup = factor(c("Yes", "No"), levels = c("Yes", "No")), value = c(60,40,90,10,55,45,88,12)) ggplot(mydata2, aes(mygroup, value, fill = mysubgroup)) + geom_bar(position = "dodge", width = 0.5, stat = "identity")+ coord_flip() + scale_x_discrete(labels = function(l) parse(text=l))

**ggplot tricks not to forget about - Cyberhelp,** Within an xlab() or ylab() function, use expression(paste()) to use special characters. the layer theme(axis.text.x = element_text(angle = 90, hjust = 1)); Display axis tick labels as percentages Yesterday, I was trying to put some finishing touches on a figure I made in ggplot2 that visualizes some simulation results. The plot features several panels using facet_grid(), and uses colors to distinguish between different regression models that were fit to the simulated data. I wanted to label certain axes and panel names using the Greek letters I had used as parameter notation, and I

The suggestion by @baptiste to use a function is great, though his code maybe not complete to give the expected results. One can also directely use the function `parse`

in `scale_x_discrete(labels = parse(text=levels(mydata2$mygroup)))`

modified code to produce the plot:

group_name = sprintf("A[%i*x]", rep(1:4,each=2)) # alternatively, use gsub with your original vector # group_name = c('A_1x', 'A_1x', 'A_2x', 'A_2x', 'A_3x', 'A_3x', 'A_4x', 'A_4x') # gsub("A_([0-9])x","A[\\1*x]", group_name) mydata2 <- data.frame(mygroup = group_name, mysubgroup = factor(c("Yes", "No"), levels = c("Yes", "No")), value = c(60,40,90,10,55,45,88,12)) ggplot(mydata2, aes(mygroup, value, fill = mysubgroup)) + geom_bar(position = "dodge", width = 0.5, stat = "identity")+ coord_flip() + scale_x_discrete(labels = parse(text=levels(mydata2$mygroup)))

**Quick plot,** See facet_grid : display marginal facets? geom Character vector (or expression) giving plot title, x axis label, and y axis label respectively. asp. The y/x aspect ggplot (mpg, aes (displ, hwy)) + geom_point (aes (colour = class)) + scale_x_continuous ("A really awesome x axis label") + scale_y_continuous ("An amazingly great y axis label") The use of + to “add” scales to a plot is a little misleading. When you + a scale, you’re not actually adding it to the plot, but overriding the existing scale.

**Axis guide,** A character string or expression indicating a title of guide. The number of rows (for vertical axes) or columns (for horizontal axes) that should be used to render Discrete axes. In the examples below, we’ll use only the functions scale_x_discrete() and xlim() to customize x axis tick marks. The same type of examples can be applied to a discrete y axis using the functions scale_y_discrete() and ylim().

**Chapter 7 ggplot2,** The two most important cues in this plot are the point positions on the x-axis and y-axis, which represent population size and the total number of murders, If qplot is an integral part of ggplot2, then the ggplot command is a super component of the ggplot2 package. ggplot2 is a part of the tidyverse,. Formatting text and labels in ggplot or ggplot2 axis is easy. The R ggplot2 Density Plot is useful to visualize the distribution of variables with an underlying smoothness.

**Chapter 14 Graphing Part II,** While ggplot typically produces very reasonable choices for values o the axis scales and color that know about how colors are displayed and what sort of color blindness is possible. cowplot::plot_grid( ggplot(ACS, aes(x=Age, y=Income)) + geom_point() + R plotting has a notation scheme which it calls expressions . Customize a discrete axis. The functions scale_x_discrete() and scale_y_discrete() are used to customize discrete x and y axis, respectively. It is possible to use these functions to change the following x or y axis parameters : axis titles; axis limits (data range to display) choose where tick marks appear; manually label tick marks

##### Comments

- It seemed to me that one ought to be able to create a vector of expressions rather than having to type each one out explicitly. This is the best I could com up with, but perhaps there's a more elegant method:
`scale_x_discrete(labels=sapply(paste0("A[", 1:4, "*x]"), function(i) parse(text=i)))`