I'm working on a duplicate bar chart using ggplot and I'm almost getting what I want, except for this error I couldn't fix.
The data I'm plotting:
comp reg counts
1 C0xC12 up 569
2 C0xC12 down 845
3 C0xC24 up 420
4 C0xC24 down 227
5 C0xT12 up 2904
6 C0xT12 down 2989
7 C0xT24 up 2536
8 C0xT24 down 2309
9 C12xC24 up 314
10 C12xC24 down 138
11 C12xT12 up 3006
12 C12xT12 down 3082
13 C12xT24 up 2795
14 C12xT24 down 2577
15 C24xT12 up 1381
16 C24xT12 down 1901
17 C24xT24 up 482
18 C24xT24 down 862
19 T12xT24 up 1732
20 T12xT24 down 1464
21 C0xC12 uppcw 7
22 C0xC12 downpcw 15
23 C0xC24 uppcw 10
24 C0xC24 downpcw 4
25 C0xT12 uppcw 56
26 C0xT12 downpcw 58
27 C0xT24 uppcw 49
28 C0xT24 downpcw 41
29 C12xC24 uppcw 8
30 C12xC24 downpcw 2
31 C12xT12 uppcw 64
32 C12xT12 downpcw 50
33 C12xT24 uppcw 48
34 C12xT24 downpcw 39
35 C24xT12 uppcw 35
36 C24xT12 downpcw 40
37 C24xT24 uppcw 11
38 C24xT24 downpcw 15
39 T12xT24 uppcw 33
40 T12xT24 downpcw 30
The code I'm running:
teste <- read.table("teste.txt", sep = "\t", header = TRUE)
ggplot(data = teste,
mapping = aes(x = comp, fill = reg,
y = ifelse(test = reg == "down" | reg == "downpcw",
yes = -counts, no = counts))) +
geom_bar(stat = "identity") +
scale_y_continuous(labels = abs, limits = max(teste$counts) * c(-1,1)) +
geom_text(aes(label=counts), vjust=0.5, color="black", size=3.0, nudge_y = c(-130,130))+
labs(y = "DEGs", x = "Groups comparisons") + scale_fill_manual(values=c("#98FB98","#FA8072","#00FF00","#FF0000")) +
scale_x_discrete(limits=c("T12xT24", "C24xT24", "C24xT12", "C12xT24", "C12xT12", "C12xC24", "C0xT24", "C0xT12", "C0xC24","C0xC12")) +
coord_flip()
That's the plot I'm getting
notice that "C12xT12" biggest bar is not showing, just its size (3082).
I've looked for solutions but found nothing and I can't come up with one alone, so I'm asking if anyone has ever dealt with this issue before.
The problem comes from your scale_y_continuous.
You're setting it to the max of the counts column which cuts off the max column as it's exactly at the limit of the plot.
If you multiply the max with 1.05 instead of 1, everything should work.
Change the corresponding part of the scale_y_continuous to: limits = max(teste$counts) * c(-1.05,1.05).
Also some best practice colour advice: avoid using red-green combination as ~10% of men are colourblind and cannot tell shades of these colours apart.
The line scale_y_continuous(labels = abs, limits = max(teste$counts) * c(-1,1)) +
is "guilty" here, because it limits the axis to exactly 3082. This causes ggplot to clip the bar.
Either leave out the limits = max(teste$counts) * c(-1,1) or use something like scale_y_continuous(labels = abs, limits = max(teste$counts) * c(-1.02,1.02)) +
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With