Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to solve the issue of the group title being cut off?

this is my first time to ask questions in stack overflow.
Recently I have done a bit of data visualization on the Amino Acids by making a multiple pie charts
First, this is my dataset

library(ggplot2)
df = data.frame(Species <-c('Chicken','Chicken','Chicken','Chicken','Chicken','Human','Human','Human','Human','Human','Crab-eating macaque','Crab-eating macaque','Crab-eating macaque','Crab-eating macaque','Crab-eating macaque','Mouse','Mouse','Mouse','Mouse','Mouse','Zebrafish','Zebrafish','Zebrafish','Zebrafish','Zebrafish'),
                Amino_acids <- c('E','R','G','P','Others','E','R','G','P','Others','E','R','G','P','Others','E','R','G','P','Others','E','R','G','P','Others'),
                value <- c(18,6,10,9,57,26,14,8,5,46,29,15,10,4,42,23,17,7,5,48,31,4,13,7,46))

df$Species <- factor(df$Species)
df$Amino_acids <- factor(df$Amino_acids) 

Then, using ggplot2 to make the data visualization

ggplot(data=df, aes(x=" ", y=value, group=Amino_acids, colour=Amino_acids, fill=Amino_acids)) +
  geom_bar(width = 1, stat = "identity", position= "fill") +
  coord_polar("y", start=0)+
  facet_grid(.~ Species) + facet_wrap(.~Species, strip.position="top")+theme_void()

and the result is this
Results](https://i.stack.imgur.com/8EheY.png)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The problem is I dont know why some of the group (species) titles are being cut off, for example, crab-eating macaque, that is very confusing to me. Also, I have tried hjust on the axis.title.x and y already and it seems no use at all.

Can anyone solve this issue for me? I would rly appreciate it if you can.

>Solution :

You’re looking to edit strip.title.x. You have several ways to go around this. I’d prefer editing the box size for the text:

ggplot(data=df, aes(x=" ", y=value, group=Amino_acids, colour=Amino_acids, fill=Amino_acids)) +
  geom_bar(width = 1, stat = "identity", position= "fill") +
  coord_polar("y", start=0)+
  facet_grid(.~ Species) + 
  facet_wrap(.~Species) +
  theme_void() +
  theme(strip.text.x = element_text(margin = margin(1,0,1,0), "cm"))

However, you could also adjust the vertical position:

ggplot(data=df, aes(x=" ", y=value, group=Amino_acids, colour=Amino_acids, fill=Amino_acids)) +
  geom_bar(width = 1, stat = "identity", position= "fill") +
  coord_polar("y", start=0)+
  facet_grid(.~ Species) + 
  facet_wrap(.~Species) +
  theme_void() +
  theme(strip.text.x = element_text(vjust = 1))

Both should result in this:
enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading