ggplot2--Pie Chart

Source: Internet
Author: User
Tags ggplot

Directory:
    • Original pattern
    • How to remove the clutter from the center of a pie chart
    • How to remove a label next to a pie chart
    • How to get rid of the upper left corner of the extra horizontal line
    • How to remove the caption from the legend and place the legend above
    • How to add a percentage to a legend's label
    • How to make a small piece of pie chart appear clockwise from large to small
    • How to remove the numbers on the white box
    • How to add a percentage to a graph
    • How to create a pie ring

(For more information, see: R, Ggplot2, shiny summary)


Original pattern:
library(ggplot2)dtdata.frame(A = c(2, 7, 4, 10, 1), B = c(‘B‘,‘A‘,‘C‘,‘D‘,‘E‘))p""AB)) +   "identity") +   "y")   ## 把柱状图折叠成饼图(极坐标)p


How to remove the clutter from the center of the pie chart:
Library(GGPLOT2)DT= data. Frame(a = c(2, 7, 4, ten, 1), B = C('b', 'A ', 'C', 'D', 'E'))P= Ggplot (DT, AES (x ="", y =A, fill =B) + Geom_bar (stat ="Identity", width =1) + # # Width >=1When the center of the dot disappears coord_polar (theta ="Y")P


How to remove a label next to a pie chart:
Library(GGPLOT2)DT= data. Frame(A = C(2, 7, 4, ten, 1), B = C('b', 'A', 'C', 'D', 'E'))P= Ggplot (DT, AES (x ="", y =A, fill =B) + Geom_bar (stat ="Identity", width =1) + coord_polar (theta ="Y") + Labs (x ="", y ="", title =""# # to set the label to emptyP


how to get rid of the upper left corner of the extra horizontal line:
Library(GGPLOT2)DT= data. Frame(A = C(2, 7, 4, ten, 1), B = C('b', 'A', 'C', 'D', 'E'))P= Ggplot (DT, AES (x ="", y =A, fill =B) + Geom_bar (stat ="Identity", width =1) + coord_polar (theta ="Y") + Labs (x ="", y ="", title =""+ Theme (Axis.ticks = Element_blank ()) # # # to get rid of the "moustache" in the upper left-hand cornerP


How to remove the caption from the legend and place the legend above:
Library(GGPLOT2)DT= data. Frame(A = C(2, 7, 4, ten, 1), B = C('b', 'A', 'C', 'D', 'E'))P= Ggplot (DT, AES (x ="", y =A, fill =B) + Geom_bar (stat ="Identity", width =1) + coord_polar (theta ="Y") + Labs (x ="", y ="", title ="") + Theme (Axis.ticks = Element_blank ()) + theme (Legend.title = Element_blank (), legend.position ="Top"# # To set the legend title to empty and put the earthwork aboveP


How to add a percentage to a legend's label:
Library (ggplot2) dt = Data.frame (A = C (2,7,4,Ten,1), B = C (' B ',' A ',' C ',' D ',' E ')) MyLabel = as. Vector (DT$B)# # turns into vectors, otherwise the legend's labels may not match the actual orderMyLabel = Paste (MyLabel,"(", round (DT$A/SUM (dt$A) * -,2),"%) ", Sep ="")# # Use round () to preserve two decimal places for resultsp = ggplot (DT, AES (x ="", y = A, fill = B)) + geom_bar (stat ="Identity", width =1) + coord_polar (theta ="Y") + Labs (x ="", y ="", title ="") + Theme (Axis.ticks = Element_blank ()) + theme (Legend.title = Element_blank (), legend.position ="Top") + scale_fill_discrete (breaks = DT$B, labels = myLabel)# # Replace the original legend label with the current MyLabelP


How to make a small chunk of a pie chart appear clockwise from large to small:
Library (ggplot2) dt = Data.frame (A = C (2,7,4,Ten,1), B = C (' B ',' A ',' C ',' D ',' E ')) dt = Dt[order (dt$A, decreasing =TRUE),]# # Use order () to sort data frame data from large to small by column A dataMyLabel = as. Vector (DT$B) MyLabel = Paste (MyLabel,"(", round (DT$A/SUM (dt$A) * -,2),"%) ", Sep ="") p = ggplot (DT, AES (x ="", y = A, fill = B)) + geom_bar (stat ="Identity", width =1) + coord_polar (theta ="Y") + Labs (x ="", y ="", title ="") + Theme (Axis.ticks = Element_blank ()) + theme (Legend.title = Element_blank (), legend.position ="Top") + scale_fill_discrete (breaks = DT$B, labels = MyLabel) p


How to remove the numbers on the white box:
Library (ggplot2) dt = Data.frame (A = C (2,7,4,Ten,1), B = C (' B ',' A ',' C ',' D ',' E ')) dt = Dt[order (dt$A, decreasing =TRUE),]# # Use order () to sort data frame data from large to small by column A dataMyLabel = as. Vector (DT$B) MyLabel = Paste (MyLabel,"(", round (DT$A/SUM (dt$A) * -,2),"%) ", Sep ="") p = ggplot (DT, AES (x ="", y = A, fill = B)) + geom_bar (stat ="Identity", width =1) + coord_polar (theta ="Y") + Labs (x ="", y ="", title ="") + Theme (Axis.ticks = Element_blank ()) + theme (Legend.title = Element_blank (), legend.position ="Top") + scale_fill_discrete (breaks = DT$B, labels = myLabel) + Theme (Axis.text.x = Element_blank ())# # White box is the original histogram of the x-axis, the x-axis of the scale of the text can be removedP


How to add a percentage to the graph:
Library (ggplot2) dt = Data.frame (A = C (2,7,4,Ten,1), B = C (' B ',' A ',' C ',' D ',' E ')) dt = Dt[order (dt$a, decreasing =TRUE),]mylabel = as. Vector (dt$b) MyLabel = Paste (MyLabel,"(",round(Dt$a/sum(dt$a) * -,2),"%)", Sep ="") p = ggplot (DT, AES (x ="", y = A, fill = B)) + geom_bar (stat ="Identity", width =1) + coord_polar (theta ="Y") + Labs (x ="", y ="", title ="") + Theme (axis.Ticks= Element_blank ()) + theme (Legend.title = Element_blank (), legend.position ="Top") + scale_fill_discrete (breaks = dt$b, labels = myLabel) + Theme (axis.text. x = Element_blank ()) + Geom_text (Aes (y = A/2+ C (0, Cumsum (A) [-length(A)]), x =sum(A)/ -, label = MyLabel), size =5)# # Add a percentage to the graph: X adjust the distance from the label to the center of the circle, y adjust the left and right position of the labelP


How to create a pie ring:
Library (ggplot2) dt = Data.frame (A = C (2,7,4,Ten,1), B = C (' B ',' A ',' C ',' D ',' E ')) dt = Dt[order (dt$a, decreasing =TRUE),]mylabel = as. Vector (dt$b) MyLabel = Paste (MyLabel,"(",round(Dt$a/sum(dt$a) * -,2),"%)", Sep ="") p = ggplot (DT, AES (x ="", y = A, fill = B)) + geom_bar (stat ="Identity", width =0.3) +# # when width < 1 o'clock pie chart will turn into pie ringCoord_polar (theta ="Y") + THEME_BW () + Labs (x ="", y ="", title ="") + Theme (axis.Ticks= Element_blank ()) + Theme (Legend.position ="None") + Theme (axis.text. x = Element_blank ()) + Geom_text (Aes (y = A/2+ C (0, Cumsum (A) [-length(A)]), x =sum(A)/ -, label = MyLabel), size =5) p

Copyright NOTICE: Reprint Please indicate the source, thank you!

ggplot2--Pie Chart

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.