问题
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.
回答1:
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.
回答2:
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)) +
回答3:
To be honest, your code is full of redundance. If you accept an answer, consider accepting the previous answers because they got to the point of the problem. I just add this one to make suggestions to improve the code.
It's not only a matter of style, also of clarity and can reduce problems - In general, ggplot makes a lot automatically for you. E.g., in your plot, there is really no need to manually set the scale_discrete
breaks and to manually set the scale_continuous
limits.
Other comments to the code are in the code block - have a look. Although 10% color blindness might be mildly exaggerated, this is indeed a concern. Have a look at this fabulous website - it will help you to chose nice colors.
library(ggplot2)
teste <- data.table::fread(" 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")
Now the plot
ggplot(data = teste, aes(x = comp, y = ifelse(grepl('down', reg), -counts, counts), fill = reg)) +
## I reduced your statement to a simple "grepl" statement, and you don't really need to name your ifelse arguments
geom_bar(stat = "identity") +
geom_text(aes(label=counts), vjust=0.5, size=3.0, nudge_x = c(-0.1,0.1)) +
### I think nudge_x is what you want - your labels were overlapping
scale_fill_brewer(palette = 'RdBu') + ## that's one of the palettes from colorbrewer
coord_flip()
Created on 2019-08-05 by the reprex package (v0.3.0)
来源:https://stackoverflow.com/questions/57359552/missing-only-one-bar-in-bar-chart