1 year ago

#323023

test-img

Doda

Add counts to stacked bar plot with data from 2 data frames

There are several posts similar to this, but I haven't been able to find the right solution for what is probably a simple problem.

I am trying to produce a stacked bar plot with counts for both stacks, but the data is in two data frames. This may not be ideal, but I am going to have to make these comparison bar charts many times for different data frames, so I am hoping to avoid having to merge them first (unless it's super easy?) Both data frames share the variable "subfamily" - so I think rbinding them would get confusing, but maybe I'm off base. Here is an example of my two data frames, my plot code, and my resulting plot:

> head(fimos)
   motif_id motif_alt_id                unique_id start stop strand   score  p-value q-value matched_sequence subfamily
1: MA0074.1    RXRA::VDR   chrX_72325982-72332009  4211 4225      - 18.3945 4.50e-07   0.542  AGGTCACAGAGTTCC      L1HS
2: MA0074.1    RXRA::VDR chr9_113179383-113185406  5103 5117      + 17.1835 8.14e-07   0.542  GGGTTAATGAGTTCC      L1HS
3: MA0074.1    RXRA::VDR  chr11_90133236-90139266  4006 4020      + 16.8165 9.52e-07   0.542  ggttcaatgggtaca      L1HS
4: MA0074.1    RXRA::VDR  chr14_61521804-61527866  3978 3992      - 14.9725 2.10e-06   0.542  AGGTCACTGAGTTTC      L1HS
5: MA0074.1    RXRA::VDR   chr4_80858870-80864900  2539 2553      - 14.6789 2.42e-06   0.542  AGGTCAGTGGGTTCA      L1HS
6: MA0074.1    RXRA::VDR chr3_158813169-158819183  1026 1040      - 14.3028 2.95e-06   0.542  GTGTCAAAGAGTTCA      L1HS


> head(fl_beds)
    chr    start     stop                 unique subfamily
1: chr1 35031657 35037706 chr1_35031657-35037706      L1HS
2: chr1 67544575 67550598 chr1_67544575-67550598      L1HS
3: chr1 71979382 71985425 chr1_71979382-71985425      L1HS
4: chr1 81404889 81410942 chr1_81404889-81410942      L1HS
5: chr1 84518073 84524089 chr1_84518073-84524089      L1HS
6: chr1 86214203 86220231 chr1_86214203-86220231      L1HS


p <- ggplot(NULL, aes(x= subfamily)) + 
  geom_bar(aes(fill = "fl_beds"), data = fl_beds, alpha = 0.5) +
  geom_bar(aes(fill = "fimos"), data = fimos, alpha = 0.5) +
  theme_classic()
p

rplot1

Here's some things I've been trying to get the count totals for each bar, without success:

p <- ggplot(NULL, aes(x= subfamily)) + 
  geom_bar(aes(fill = "fl_beds"), data = fl_beds, alpha = 0.5) +
  geom_text(stat='count', aes(label=..count..), vjust=-1) +
  geom_bar(aes(fill = "fimos"), data = fimos, alpha = 0.5) +
  geom_text(stat='count', aes(label=..count..), vjust=-1) +
theme_classic()
p



p <- ggplot(NULL, aes(x= subfamily)) + 
  geom_bar(aes(fill = "fl_beds"), data = fl_beds, alpha = 0.5) +
  geom_bar(aes(fill = "fimos"), data = fimos, alpha = 0.5) +
  theme_classic()
p

p + geom_text(aes(label = Frequency), size = 3, hjust = 0.5, vjust = 3, position =     "stack") 

But each time I get this error:

Error in FUN(X[[i]], ...) : object 'subfamily' not found

So, is there a way to do this, or a better way to do the same thing? Thank you so much!

r

ggplot2

geom-bar

geom-text

0 Answers

Your Answer

Accepted video resources