在第一栏或上面写组标签而不是图例 [英] Write group labels in first bar or above instead of legend

查看:156
本文介绍了在第一栏或上面写组标签而不是图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个堆叠的barplot,其中包含以下数据:

  df < -  expand.grid(name = c(oak ,birch,cedar),
sample = c(one,two),
type = c(sapling,adult,dead))
df $ count< - sample(5:200,size = nrow(df),replace = T)

我生成一个barplot并尝试添加组标签:

  ggplot(df,aes(x = name,y = count,fill = type))+ 
geom_bar(stat =identity)+
coord_flip()+
theme(legend.position =none)+
geom_text(aes(label = type,position =stack))




两张出现三个问题:


  1. 如何才能让标签仅出现在顶栏中?


  2. 如何让标签出现在栏的中央?


  3. ñ我使标签出现在顶部栏的顶部,并通过箭头连接到它们的部分?



解决方案

上面提供了一个链接。这会帮助你。在这里,我有另一个建议。

  set.seed(123)
df< - expand.grid(name = c(oak,birch,cedar),
sample = c(one,two),
type = c(sapling,adult ))
df $ count< - 样本(5:200,size = nrow(df),replace = T)

###排列数据框一个和两个)
库(dplyr)
ana< - df%>%
group_by(名称,类型)%>%
汇总))

#一次绘制一个图形
bob< - ggplot(ana,aes(x = name,y = total,fill = type))+
geom_bar(stat (bob)$ data [[1]]

#计算文字位置&添加文本标签
cathy $ y_pos< - (cathy $ ymin + cathy $ ymax)/ 2
cathy $ label< - rep(c(sampling,adult,dead)) ,次数= 3)

#为顶栏标注数据子集
dan < - cathy [c(7:9),]

#再画一个数字
bob +
annotate(x = dan $ x,y = dan $ y_pos,label = dan $ label,geom =text,size = 3)+
coord_flip ()


I have a stacked barplot with the following data

df <- expand.grid(name = c("oak","birch","cedar"),
    sample = c("one","two"),
    type = c("sapling","adult","dead"))
df$count <- sample(5:200, size = nrow(df), replace = T)

I generate a barplot and try to add the group lables to it:

ggplot(df, aes(x = name, y = count, fill = type)) +
   geom_bar(stat = "identity") +
   coord_flip() +
   theme(legend.position="none") +
   geom_text(aes(label = type, position = "stack")) 

It produces:

Two to three questions arise:

  1. How can I make the labels appear in the top bar only?

  2. How can I make the labels appear in the center of the bar section?

  3. Optionally: How can I make the labels appear on top of the top bar being connected to their sections by arrows?

解决方案

There is a link suggested above. That will help you. Here, I have another suggestion.

set.seed(123)
df <- expand.grid(name = c("oak","birch","cedar"),
                  sample = c("one","two"),
                  type = c("sapling","adult","dead"))
df$count <- sample(5:200, size = nrow(df), replace = T)

### Arrange a data frame (summing up sample one and two)
library(dplyr)
ana <- df %>%
        group_by(name, type) %>%
        summarise(total = sum(count))

# Draw a figure once        
bob <- ggplot(ana, aes(x = name, y = total, fill = type)) +
       geom_bar(stat = "identity", position = "stack")

# Get a data frame for ggplot      
cathy <- ggplot_build(bob)$data[[1]]

# calculate text position & add text labels
cathy$y_pos <- (cathy$ymin + cathy$ymax) / 2
cathy$label <- rep(c("sampling", "adult", "dead"), times = 3)

# Subset the data for labeling for the top bar
dan <- cathy[c(7:9), ]

# Draw a figure again
bob + 
annotate(x = dan$x, y = dan$y_pos, label = dan$label, geom="text", size=3) +
coord_flip()

这篇关于在第一栏或上面写组标签而不是图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆