r - ggplot displaying expression in x axis -
below, have r code plots 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, plot looks below. however, want show expressions in x axis shown in 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 gives following error:
error in as.data.frame.default(x[[i]], optional = true) : cannot coerce class ""expression"" data.frame
how fix it?
here working example - changed group_name
4 elements instead of 8 , manually added them ggplot
expression. issue expression type can't column name data.frame
. escapes 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 labels here
Comments
Post a Comment