在ggplot的每个方面写不同的文本

Writing different text in each facet of ggplot

我试图用不同的文本覆盖 R 中堆叠条形图的每个方面,但我 运行 遇到以下错误:

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

我尝试 运行 的代码如下:

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))

知道如何解决吗?

这个有效:

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))