显示一个不';使用GGR中的ggplot在条形图中不存在数据

显示一个不';使用GGR中的ggplot在条形图中不存在数据,r,ggplot2,bar-chart,R,Ggplot2,Bar Chart,我已经生成了一些数据来说明我遇到的问题: m <- matrix(rep(c(2:7), 38), ncol=19, nrow=38, byrow=T) C1 <- data.frame(factor(m,levels = c(1:7)));C1 cols <- "C" colnames(C1) <- cols Cog = C1 %>% group_by(C ) %>% summarise(count=n()) %>% mutate(pct=co

我已经生成了一些数据来说明我遇到的问题:

m <- matrix(rep(c(2:7), 38), ncol=19, nrow=38, byrow=T)


C1 <- data.frame(factor(m,levels = c(1:7)));C1

cols <- "C"
colnames(C1) <- cols

Cog = C1 %>% group_by(C ) %>%
summarise(count=n()) %>%
mutate(pct=count/sum(count)) 

ggplot(Cog, aes(x=C , y=pct, colour=C , fill=C )) +
geom_bar(stat="identity") +
scale_y_continuous(labels=percent, limits=c(0,0.50)) + 
geom_text(data=Cog, aes(label=paste0(round(pct*100,1),"%"),
                      y=pct+0.012), size=4)
m将
scale\x\u离散(drop=FALSE)
添加到绘图中

library( ggplot2 )
library( scales )
ggplot(Cog, aes(x=C , y=pct, colour=C , fill=C )) +
  geom_bar(stat="identity") +
  scale_y_continuous( labels = percent, limits=c(0,0.50)) + 
  geom_text(data=Cog, aes(label=paste0(round(pct*100,1),"%"),
                          y=pct+0.012), size=4) +
  scale_x_discrete( drop = FALSE )

备选“解决方案”

将数据绑定到数据帧中(感觉有点像作弊,但可以完成任务)

Cog=C1%>%group\U by(C)%>%
汇总(计数=n())%>%
突变(pct=计数/总和(计数))%>%
rbind(c(1,0,0))35;将
比例x_离散(drop=FALSE)
添加到绘图中

library( ggplot2 )
library( scales )
ggplot(Cog, aes(x=C , y=pct, colour=C , fill=C )) +
  geom_bar(stat="identity") +
  scale_y_continuous( labels = percent, limits=c(0,0.50)) + 
  geom_text(data=Cog, aes(label=paste0(round(pct*100,1),"%"),
                          y=pct+0.012), size=4) +
  scale_x_discrete( drop = FALSE )

备选“解决方案”

将数据绑定到数据帧中(感觉有点像作弊,但可以完成任务)

Cog=C1%>%group\U by(C)%>%
汇总(计数=n())%>%
突变(pct=计数/总和(计数))%>%

rbind(c(1,0,0))#谢谢。但这不会将其添加到图例中,也不会在图例上显示0%。谢谢。但这不会将其添加到图例中,也不会在图例上显示0%。