Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在ggplot2中,我的百分比标签为';t正确铺设_R_Ggplot2 - Fatal编程技术网

R 在ggplot2中,我的百分比标签为';t正确铺设

R 在ggplot2中,我的百分比标签为';t正确铺设,r,ggplot2,R,Ggplot2,我尝试在我的100%堆叠条形图上标注百分比(见图)。我计算下面第一个块中的百分比,然后运行ggplot。 你知道我做错了什么吗?为什么我的百分比是这样显示的 library(scales) percentData = surveyds %>% group_by(qtext, X_Gender, response) %>% summarise(N = n()) %>% mutate(prop = N/sum(N)) percentData$prop = label

我尝试在我的100%堆叠条形图上标注百分比(见图)。我计算下面第一个块中的百分比,然后运行ggplot。 你知道我做错了什么吗?为什么我的百分比是这样显示的

library(scales)
percentData = surveyds %>%
  group_by(qtext, X_Gender, response) %>% 
  summarise(N = n()) %>% 
  mutate(prop = N/sum(N))
percentData$prop = label_percent(accuracy = 1)(percentData$prop) #make percent from decimal

surveyds$responsef = factor(
  surveyds$response,
  levels = c("Poor","Fair","Good","Very good", "Excellent"),
  ordered = TRUE
) #Order bars

#Run visual
ggplot(data =subset(surveyds, !is.na(responsef)))+ #responsef is my response factored
  geom_bar(aes(x = X_Gender, fill = responsef), position = 'fill', width = 0.5)+
  facet_grid(rows = vars(qtext))+ #qtext is my question text
  coord_flip()+
  ylab('Proportion')+
  xlab('question')+
  ggtitle('Change')+
  geom_text(data = percentData, aes(y = N, label = prop, x = X_Gender),
            position=position_fill(vjust=0.5)) 

我想出来了。需要在geom_文本中添加填充参数

geom_text(data = percentData, aes(fill = responsef, y = N, label = prop, x = X_Gender,),
            position=position_fill(vjust=0.5))

我想出来了。需要在geom_文本中添加填充参数

geom_text(data = percentData, aes(fill = responsef, y = N, label = prop, x = X_Gender,),
            position=position_fill(vjust=0.5))

请使用
dput(surveyds)
共享
surveyds
的样本,否则您的问题将无法复制!谢谢你看。不幸的是,我不能分享数据,但我想我知道这个问题。标签是翻转的,从我的图片的顶部开始:1%应该是穷人,7%应该是普通人,15%应该是好人等等。你知道如何将它们反向叠加吗?我尝试了reverse=TRUE,但没有解决问题。geom_text(数据=百分比数据,aes(y=N,label=prop,x=x_性别),position=position\u fill(vjust=1,reverse=TRUE))请使用
dput(surveyds)
共享一个
surveyds
样本,否则您的问题将无法重现!谢谢你看。不幸的是,我不能分享数据,但我想我知道这个问题。标签是翻转的,从我的图片的顶部开始:1%应该是穷人,7%应该是普通人,15%应该是好人等等。你知道如何将它们反向叠加吗?我尝试了reverse=TRUE,但没有解决问题。几何图形文本(数据=百分比数据,aes(y=N,标签=属性,x=x性别),位置=位置填充(vjust=1,reverse=TRUE))