2

Im tring to plot stacked bar plot, and label each bar with value I calculated. I have vector of values for eah bar. If I use the vector as is I get the warning " Error: Aesthetics must be either length 1 or the same as the data (156): label", so I added the vector to the data, using rep for all the rows for each bar, but when I used geom_text, I get for each bar many labels(The number of stacks in each bar).

my code:

g <- ggplot(df, aes(x = RNA, y = Value))
g + geom_bar(aes(fill=fct_reorder(Cancer, Value, sum, desc=TRUE)), width = 0.5, stat="identity") + 
   geom_text(aes(label = lbls), vjust = -0.5, position = position_dodge(0.9))+ 
  theme(axis.text.x = element_text(angle=90, vjust=0.6),
        axis.text.y = element_blank()) +
  scale_fill_manual(values=c("#df4a7a",
                            "#c97b7a",
                            "#de5137",
                            "#d08935",
                            "#a78d57",
                            "#d2d23e",
                            "#cfd88d",
                            "#67993f",
                            "#76d854",
                            "#66db9f",
                            "#529477",
                            "#81dacf",
                            "#6bb2d5",
                            "#6387d7",
                            "#777ba7",)) +
  labs(title="Cancer Types", 
       subtitle="")

My data Frame with the column of values to label:

                              Cancer                  RNA       Value  Tabs
mp.117                        Breast                  snoRNA     3 0.268
tmp.118   Digestive/Gastrointestinal                  snoRNA     0 0.268
tmp.119 Endocrine and Neuroendocrine                  snoRNA     1 0.268
tmp.120                          Eye                  snoRNA     0 0.268
tmp.121                Genitourinary                  snoRNA     0 0.268
tmp.122                    Germ Cell                  snoRNA     0 0.268
tmp.123                  Gynecologic                  snoRNA     0 0.268
tmp.124                Head and Neck                  snoRNA     0 0.268
tmp.125            Hematologic/Blood                  snoRNA     0 0.268
tmp.126              Musculoskeletal                  snoRNA     1 0.268
tmp.127                   Neurologic                  snoRNA     0 0.268
tmp.128         Respiratory/Thoracic                  snoRNA     0 0.268
tmp.129                         Skin                  snoRNA     0 0.268
tmp.143                       Breast circRNA | Circular RNA      3 0.005
tmp.144   Digestive/Gastrointestinal circRNA | Circular RNA      1 0.005
tmp.145 Endocrine and Neuroendocrine circRNA | Circular RNA      1 0.005
tmp.146                          Eye circRNA | Circular RNA      0 0.005
tmp.147                Genitourinary circRNA | Circular RNA      1 0.005
tmp.148                    Germ Cell circRNA | Circular RNA      0 0.005
tmp.149                  Gynecologic circRNA | Circular RNA      4 0.005
tmp.150                Head and Neck circRNA | Circular RNA      3 0.005
tmp.151            Hematologic/Blood circRNA | Circular RNA      0 0.005
tmp.152              Musculoskeletal circRNA | Circular RNA      0 0.005
tmp.153                   Neurologic circRNA | Circular RNA      0 0.005
tmp.154         Respiratory/Thoracic circRNA | Circular RNA      1 0.005
tmp.155                         Skin circRNA | Circular RNA      0 0.005

The vector with the labels:

lbl = c(0.821, 0.899, 0.410, 0.028, 0.257, 0.217, 0.474, 0.220, 0.210, 0.268,   NaN, 0.005)

Thanks!!

2
  • Where do you intend to use lbl? And maybe you can post the data sample in a more easily copied format, like with dput Commented May 16, 2019 at 23:06
  • This post I believe provides the answer you're looking for. You don't need to manually calculate the labels. stackoverflow.com/questions/6644997/… Commented May 16, 2019 at 23:28

1 Answer 1

1
library(ggplot2)
library(dplyr)
library(forcats)


#your plot without geom_text
g <- ggplot(df, aes(x = RNA, y = Value)) + 
  geom_bar(aes(fill=fct_reorder(Cancer, Value, sum, desc=TRUE)), 
           width = 0.5, stat="identity") + 
  theme(axis.text.x = element_text(angle=90, vjust=0.6),
        axis.text.y = element_blank()) +
  scale_fill_manual(values=c("#df4a7a", "#c97b7a","#de5137","#d08935",
                             "#a78d57","#d2d23e","#cfd88d","#67993f",
                             "#76d854","#66db9f","#529477","#81dacf",
                             "#6bb2d5","#6387d7","#777ba7")) +
  labs(title="Cancer Types", subtitle="")



tabdf <- df %>% group_by(RNA) %>% summarise_at(vars(Tabs,Value), list(~mean(.)))

g +  geom_text(data = tabdf ,aes(y=Value, label = Tabs, fill = NULL))

Created on 2019-05-16 by the reprex package (v0.2.1)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.