简体   繁体   中英

beautiful Pie Charts with R

Let's say I have this simple data:

 mydata <- data.frame(group=c("A", "B", "0", "AB"), FR=c(20, 32, 32, 16))

If I want to create a pie chart from this dataframe I can do:

 with(mydata,pie(FR, labels=paste0(as.character(group), " ", FR, "%"), radius=1))

基本馅饼

It's quite simple but acceptable.

How can I get something similar with ggplot2 or lattice?

After much trial and error I've got

ggplot(mydata, aes(x = factor(1), y=FR,fill=factor(group)) ) + geom_bar(width = 1,stat="identity")+coord_polar(theta = "y") 

ggplot

It's much more complex and ugly. Isn't it supposed to be easy? ggplot books only give some examples and discourage from using pie charts.

Lattice is even worse, you need many many lines to get it's scaring.

Could anybody help me top get a nice and simple Pie chart, please? For example something like...

示例1

示例2

Isn't there any R package able to do it easily, without 20 lines of code?

Why not a square pie chart ?

devtools::install_github("hrbrmstr/waffle")
library(waffle)

mydata <- c(`A`=20, `B`=32, `0`=32, `AB`=16)
waffle(mydata, title = "Yummy waffle pie!")

在此处输入图像描述


If you have multiple dimensions of information, another option could be sunburstR . Using browsers data from @rawr post you could do:

library(sunburstR)
library(dplyr)
library(tidyr)
browsers %>%
  unite(bv, browser, version, sep = "-") %>%
  select(bv, share) %>%
  sunburst(., count = TRUE)

在此处输入图像描述

You could use treemap (for an interactive version, try @timelyportfolio's d3treeR package )

library(treemap)
tm <- treemap(
  browsers,
  index=c("browser", "version"),
  vSize="share",
  vColor="share",
  type="value"
)

在此处输入图像描述

You could also use a sankey diagram (from the networkD3 package )

library(networkD3)
df <- browsers %>%
  mutate_each(funs(as.character), browser, version) %>%
  mutate(bn = group_indices_(., .dots = "browser"), 
         cn = max(bn) + row_number()) 

links <- select(df, bn, cn, share)
nodes <- data.frame(name = c("", sort(unique(df$browser)), df$version))

sankeyNetwork(Links = links, Nodes = nodes, Source = "bn",
              Target = "cn", Value = "share", NodeID = "name",
              fontSize = 12, nodeWidth = 30)

在此处输入图像描述

Some handy tips here:

Source: Dark Horse Analytics: Salvaging the Pie

(srsly tho, what's wrong with a bar chart?)

NOTE: I have no idea what Dark Horse Analytics does. This is just my go-to, anti-pie demo image.

You can try with the pie3D() function from the plotrix package:

library(plotrix)
pie3D(mydata$FR, labels = mydata$group, main = "An exploded 3D pie chart", explode=0.1, radius=.9, labelcex = 1.2,  start=0.7)

在此处输入图像描述

After a lot of trial and error, I have decided plotly works best:

library(plotly)
mydata <- data.frame(group=c("A", "B", "0", "AB"), FR=c(20, 32, 32, 16))

q <- plot_ly(mydata, labels = ~group, values = ~FR, type = 'pie') %>%
  layout(title = "Title",          
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
q

This is a png, the original one in Rstudio is interactive when you hover over it.

情节示例

Yes, I have created ggpie to better create pie (2D and 3D), donut and rose pie plot!

This is Vignette .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM