ggplot displaying expression in x axis

妖精的绣舞 提交于 2020-01-11 02:34:44

问题


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?


回答1:


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 



回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/37758050/ggplot-displaying-expression-in-x-axis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!