PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, November 28, 2022

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

 November 28, 2022     ggplot2, pie-chart, r     No comments   

Issue

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)

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



Answered By - RRDK
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing