Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.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 geom_文本是标记条的最佳方式吗?_R_Ggplot2 - Fatal编程技术网

R geom_文本是标记条的最佳方式吗?

R geom_文本是标记条的最佳方式吗?,r,ggplot2,R,Ggplot2,我有以下数据: 我正在尝试向条中添加标签,标签应该是每个样本的观察次数(间隔列),例如:样本NE1有7次“30到60”。。。等等 我尝试了下面的代码: limit <- data$months + 5 ## set limit for geom_text ggplot(data, aes( x = reorder(interval, months), y=months,fill = SAMPLE))+ geom_bar(position="dodge", stat="identity

我有以下数据:

我正在尝试向条中添加标签,标签应该是每个样本的观察次数(间隔列),例如:样本NE1有7次“30到60”。。。等等

我尝试了下面的代码:

limit <- data$months + 5 ## set limit for geom_text

ggplot(data, aes( x = reorder(interval, months), y=months,fill = SAMPLE))+ 
geom_bar(position="dodge", stat="identity", size=.4, color = 'black')+
xlab('intervals (months)')+
ylab('months')+
geom_text(aes(label= SAMPLE, y = limit),
position=position_dodge(0.9), size=3,angle = 90,check_overlap = TRUE)

limit我不知道如何在不修改数据的情况下获取它,因为
geom\u text
似乎不允许您将
标签的统计数据与指定的
y
组合

我认为,以下内容可以满足您的需要:

data <- data %>% dplyr::group_by(interval, SAMPLE) %>% dplyr::summarise(months = max(months), nobs = n()) 

ggplot(data, aes( x = reorder(interval, months),y = months, fill = SAMPLE))+ 
  geom_bar( position="dodge", stat="identity", size=.4, color = 'black') +
  geom_text(aes(label = nobs), position = position_dodge(.9), hjust = -.5, size=3,angle = 90,check_overlap = TRUE) +
  xlab('intervals (months)')+
  ylab('months')
data%dplyr::group_by(interval,SAMPLE)%>%dplyr::summary(月数=最大月数,nobs=n())
ggplot(数据,aes(x=重新排序(间隔,月),y=月,填充=样本))+
几何图形条(position=“dodge”,stat=“identity”,size=.4,color='black')+
几何图形文字(aes(标签=nobs),位置=position_dodge(.9),hjust=-.5,大小=3,角度=90,检查重叠=TRUE)+
xlab(‘间隔(月)’)+
ylab(“月”)

您希望它看起来像什么
geom_text
是您想要的合适方式。我想要的是每个样本的观察次数,而不是样本名称。非常感谢!