2016-06-23 44 views
0

Ich versuche, anderen Text zu haben jede Facette von gestapelten Balkendiagramme in R überlagert, aber ich bin mit in die folgenden Fehler:Schreiben verschiedener Text in jeder Facette von ggplot

Error: Aesthetics must either be length one, or the same length as the dataProblems:xval, yval, lab
In addition: Warning messages:
1: In eval(expr, envir, enclos) : NAs introduced by coercion
2: In eval(expr, envir, enclos) : NAs introduced by coercion

Der Code, den ich versuchte zu laufen ist unten:

library(ggplot2) 

m40<-c(3.92,5.88,20.59,69.61) #n=102 
m50<-c(5.97,7.46,18.66,67.91) #n=134 
m60<-c(8.82,13.73,22.55,54.90) #n=102 
m70<-c(10.26,10.26,23.08,56.41) #n=39 
mall<-c(6.63,9.02,20.69,63.66) #n=377 
w40<-c(12.68,10.56,26.06,50.70) #n=142 
w50<-c(17.83,14.01,31.21,36.94) #n=157 
w60<-c(10.00,26.25,28.75,35.00) #n=80 
w70<-c(10.87,8.70,32.61,47.83) #n=46 
wall<-c(13.88,14.59,29.18,42.35) #n=425 

gender <- rep(c('Men','Women'), c(20,20)) 
age <- rep(c('40-49', '50-59', '60-69', '70+', 'All'), each = 4) 
values <- c(m40, m50, m60, m70, mall, w40, w50, w60, w70, wall) 
status <- rep(c("Treated and controlled","Treated but uncontrolled","Diagnosed but untreated", 
       "Undiagnosed"), 5) 
grays <- c("Black", "Gray40", "Gray60", "Gray80") 
data = data.frame(gender, age, values, status) 

data$status = factor(data$status,levels(data$status)[c(2,3,1,4)]) 

ann_text <- data.frame(xval=c(0.4,1,2,3,4,5),yval=-3,lab=c("N=","102","134","102","39","377", "N=", "142","157","80","46","425"), 
         gender = c("Men","Men","Men","Men","Men","Men","Women","Women","Women","Women","Women","Women")) 
ann_text 

ggplot(data=data) + #starts graph 
    aes(x=age, y=values, fill=status,order=as.numeric(status)) + #selects variables 
    facet_grid(~gender) + #makes 2 boxes for gender 
    geom_bar(stat="identity") + # height of bars represents values in the data 
    theme(axis.text=element_text(size=18),# changes size of tick mark labels 
     axis.title=element_text(size=20), 
     strip.text.x=element_text(size=20), 
     legend.text=element_text(size=15), 
     panel.grid.major.x = element_blank()) + #removes vertical grid lines 
    scale_y_continuous(breaks = seq(0,100,10)) + # makes tick marks at every 10% 
    ylab("%") + # change y label 
    coord_cartesian(ylim = c(-5,102),xlim = c(0.2,5.8)) + 
    xlab("Age Group, Years") + #capitalize A in x label 
    labs(fill="") + #removes title from legend 
    geom_text(data = ann_text,aes(x=xval,y=yval,label=lab)) + 
    #annotate("text",x=c(0.4,1,2,3,4,5),y=-3,label=c("N:","102","134","102","39","377")) + 
    scale_fill_manual(values=grays, 
        guide = guide_legend(reverse=TRUE)) 

Irgendeine Idee, wie man es repariert?

Antwort

0

Dies funktioniert:

library(ggplot2) 

m40<-c(3.92,5.88,20.59,69.61) #n=102 
m50<-c(5.97,7.46,18.66,67.91) #n=134 
m60<-c(8.82,13.73,22.55,54.90) #n=102 
m70<-c(10.26,10.26,23.08,56.41) #n=39 
mall<-c(6.63,9.02,20.69,63.66) #n=377 
w40<-c(12.68,10.56,26.06,50.70) #n=142 
w50<-c(17.83,14.01,31.21,36.94) #n=157 
w60<-c(10.00,26.25,28.75,35.00) #n=80 
w70<-c(10.87,8.70,32.61,47.83) #n=46 
wall<-c(13.88,14.59,29.18,42.35) #n=425 

gender <- rep(c('Men','Women'), c(20,20)) 
age <- rep(c('40-49', '50-59', '60-69', '70+', 'All'), each = 4) 
values <- c(m40, m50, m60, m70, mall, w40, w50, w60, w70, wall) 
status <- rep(c("Treated and controlled","Treated but uncontrolled","Diagnosed but untreated", 
       "Undiagnosed"), 5) 
grays <- c("Black", "Gray40", "Gray60", "Gray80") 
data = data.frame(gender, age, values, status) 

data$status = factor(data$status,levels(data$status)[c(2,3,1,4)]) 

ann_text <- data.frame(xval=c('40-49', '50-59', '60-69', '70+', 'All', '40-49', '50-59', '60-69', '70+', 'All') ,yval = 50,lab=c("102","134","102","39","377", "142","157","80","46","425"), 
         gender = c("Men","Men","Men","Men","Men","Women","Women","Women","Women","Women")) 


ggplot(data=data) + #starts graph 
    facet_grid(~gender, aes(x=age, y=values, fill=status,order=as.numeric(status))) + #makes 2 boxes for gender 
    geom_bar(stat="identity", aes(x=age, y=values, fill=status,order=as.numeric(status))) + # height of bars represents values in the data 
    theme(axis.text=element_text(size=18),# changes size of tick mark labels 
     axis.title=element_text(size=20), 
     strip.text.x=element_text(size=20), 
     legend.text=element_text(size=15), 
     panel.grid.major.x = element_blank()) + #removes vertical grid lines 
    scale_y_continuous(breaks = seq(0,100,10)) + # makes tick marks at every 10% 
    ylab("%") + # change y label 
    coord_cartesian(ylim = c(-5,102),xlim = c(0.2,5.8)) + 
    xlab("Age Group, Years") + #capitalize A in x label 
    labs(fill="") + #removes title from legend 
    geom_text(data = ann_text,aes(x=xval,y=yval,label=lab)) + 
    #annotate("text",x=c(0.4,1,2,3,4,5),y=-3,label=c("N:","102","134","102","39","377")) + 
    scale_fill_manual(values=grays, 
        guide = guide_legend(reverse=TRUE)) 

enter image description here

+0

ich die Änderungen im Datenrahmen hinzuzufügen vergaß ich. Jetzt ist es reproduzierbar. Das Ergebnis ist noch lange nicht perfekt, aber ich habe das Problem hauptsächlich aus der Frage gelöst. Von hier aus sollte es recht einfach sein, das gewünschte Grundstück zu bekommen. – Alex

+0

Danke @Alex für Ihre Hilfe! Gibt es eine Möglichkeit für mich, die Altersgruppe "Alle" anders aussehen zu lassen als die Altersgruppen? Beispielsweise den Text "Alle" fett formatieren, die Schattierung der altersspezifischen gestapelten Balkendiagramme mit Kreuzschraffur versehen oder zusätzlichen Platz zwischen den altersspezifischen Balkendiagrammen und dem Balkendiagramm "Alle" schaffen? –