2016-02-03 14 views
5

Auf der linken Seite ist, wie mein Ringdiagramm sieht derzeit wie und nach rechts zu explodieren, wie es aussehen soll:Wie Ringdiagramm Scheiben mit R (ggplot2)

enter image description here

Gibt es eine Möglichkeit, zu explodieren das Donut-Diagramm mit R?

Hier ist mein Code:

ggplot(dat, aes(fill = goalGroups, ymax = ymax, ymin = ymin, xmax = 4.8, xmin = 3)) + 
    geom_rect(color = "white") + 
    coord_polar(theta="y", direction = -1) + 
    xlim(c(0, 8)) + 
    theme_bw() + 
    theme(panel.grid=element_blank()) + 
    theme(axis.text=element_blank()) + 
    theme(axis.ticks=element_blank()) + 
    theme(axis.title.x = element_blank()) + 
    theme(axis.title.y = element_blank()) + 
    theme(panel.border = element_blank()) 

Ich bin für jede Hilfe sehr dankbar!

Antwort

4

Sie können versuchen, xlim und ylim für jede Kategorie

zum Beispiel die Schaffung

Daten

dat = data.frame(count=c(30, 10), category=c("A", "C"),stringsAsFactors = F) 

Zusätzliche calc

dat$fraction = dat$count/sum(dat$count) 
dat = dat[order(dat$fraction), ] 
dat$ymax = cumsum(dat$fraction)-0.01 
dat$ymin = c(0, head(dat$ymax, n=-1))+0.01 
dat$all_=length(unique(dat$category)) 
dat$x1=dat$all_-(1:nrow(dat))*0.5+1 
dat$x2=dat$all_-(1:nrow(dat))*0.5+2 

Plot

p2=ggplot()+aes(ymin=0)+geom_rect(data=dat,aes(fill=category,ymax=ymax, ymin=ymin, xmax=x1, xmin=x2),color = "white")+ 
    ylim(0,1)+ 
    xlim(c(0,3+length(unique(dat$category))))+ 
    coord_polar(theta="y", direction = -1) + 
    theme_bw() + 
    theme(panel.grid=element_blank()) + 
    theme(axis.text=element_blank()) + 
    theme(axis.ticks=element_blank()) + 
    theme(axis.title.x = element_blank()) + 
    theme(axis.title.y = element_blank()) + 
    theme(panel.border = element_blank()) 
p2 

enter image description here

arbeiten auch gut für mehr als zwei Kategorie

dat = data.frame(count=c(30,15, 10), category=c("A", "B", "C"),stringsAsFactors = F) 

enter image description here