0

ggplot2: group x axis discrete values into subgroups

I found this figure on the link above is very useful to create a grouping barchart. However, my question is that how could I change the angle of the letter a,b,c,... etc in the x axis? It worked okay when plotting it, but since I used another code to layout the Arabic letter, the sub-x axis did not rotate

My data is as follows:

rr <- df %>% count(college,department)
View(rr)

enter image description here

And I did the following code, it worked for the grouping value, and the sub-x axis labels did rotate. However, Arabic letters did not appear correctly.

zz<-ggplot(rr, aes(college, n, fill=department, label = department)) + 
  geom_bar(position="dodge", stat="identity") + 
  geom_text(position = position_dodge(width = 1), aes(x=college, y=0) ) +
  theme(axis.title.y = element_text(size = rel(2), angle = 90), legend.position = "none") + ylim(0,140)+
  theme(axis.text.x = element_text(angle = 60,size = rel(3), color="black"))

enter image description here

Now, I am using the following code in order to allow Arabic letters appear correctly, it did not rotate the sub-x axis when I override the previous code with the following:

gg<-ggplotly(zz) %>% layout(titlefont=list(size=10), yaxis = list(side="right",  gridcolor = toRGB("gray90"),
                                                                  gridwidth = 2, ticks="", title="عدد المحاضرات لكل قسم", titlefont=list(size=20)), 
                            xaxis = list(ticks="", tickfont=list(size=14), title="الأقسام الأكاديمية", titlefont=list(size=30)),
                            margin = list(l = 50, r=30, b = 50, t = 80))

It appeared with vertical sub-x axis as follows:

enter image description here

But I am trying to rotate them in the second code, any answers would be really appreciated, thank you.

1 Answers1

2

Without a reproducible example of your dataset, it is hard to be sure but based on the post you are referring, the letter are plot by the geom_text function, so you can pass angle = 60 into geom_text:

zz<-ggplot(rr, aes(college, n, fill=department, label = department)) + 
  geom_bar(position="dodge", stat="identity") + 
  geom_text(position = position_dodge(width = 1), aes(x=college, y=0), angle = 60 ) +
  theme(axis.title.y = element_text(size = rel(2), angle = 90), legend.position = "none") + ylim(0,140)+
  theme(axis.text.x = element_text(angle = 60,size = rel(3), color="black"))

Here, an example based on the post you are referring (ggplot2: group x axis discrete values into subgroups):

dat <- data.frame(value=runif(26)*10,
                    grouping=c(rep("Group 1",10),
                               rep("Group 2",10),
                               rep("Group 3",6)),
                    letters=LETTERS[1:26])

library(ggplot2)

ggplot(dat, aes(grouping, value, fill=letters, label = letters)) + 
  geom_bar(position="dodge", stat="identity") + 
  geom_text(position = position_dodge(width = 1), aes(x=grouping, y=0), angle = 90)

enter image description here

dc37
  • 15,840
  • 4
  • 15
  • 32
  • Great! thank yo dc37, but when I try to plotting in different way as mentioned in my editing above, it did not rotate – user1064936 Mar 27 '20 at 23:08
  • You need to provide a reproducible example of `zz` to be sure of what you are talking about. Right now, I can't see what is going wrong with your data. See this link for providing a reproducible example: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – dc37 Mar 27 '20 at 23:14