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

Friday, April 22, 2022

[FIXED] How to properly put lables on a faceted pie chart in ggplot?

 April 22, 2022     dplyr, ggplot2, pie-chart, r     No comments   

Issue

So i have data like this:

tag  team   count    pct
E       A      12   1.00
E       B       5    .50
E       C       1    .20
I       B       5    .50
I       C       4    .80

I want to make a faceted pie chart with this data but I cannot get the text labels to appear in appropriate positions on the plot....what am I doing wrong here...


ggplot(data, aes(x = factor(1), y = pct, fill = tag)) +
  geom_bar(stat = "identity", width = 1) +
  coord_polar("y", start=0) +
  theme_void()+ # remove background, grid, numeric labels
  facet_wrap(~team)+
  geom_text(aes(label = sprintf("%1.2f%%", 100*pct)))

Solution

You are on the right track!

Minor adjustments:

ggplot(df, aes(x = factor(1), y = pct, fill = tag)) +
    geom_bar(stat = "identity", width = 1) +
    coord_polar("y", start=0) +
    theme_void()+ # remove background, grid, numeric labels
    facet_wrap(~team)+
    geom_text(aes(label = sprintf("%1.2f%%", 100*pct)),position = position_stack(vjust = 0.5)) + 
    coord_polar(theta = 'y')

Need to include position= position_stack(vjust = 0.5) and coord_polar(theta = 'y')

enter image description here



Answered By - akash87
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • 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