Reputation: 39
With the help I already got here on SO, I was able to built the barplot I wanted.
Now I'd like to do exact the same plot using new data that is very similar to the previously one, although it's not working and I can't figure out why.
The data is as it follows:
> dput(pcwdegs)
structure(list(comp = c("C0vsC12", "C0vsC12", "C0vsC24", "C0vsC24",
"C0vsT12", "C0vsT12", "C0vsT24", "C0vsT24", "C12vsC24", "C12vsC24",
"C12vsT12", "C12vsT12", "C12vsT24", "C12vsT24", "C24vsT12", "C24vsT12",
"C24vsT24", "C24vsT24", "T12vsT24", "T12vsT24"), reg = c("up",
"down", "up", "down", "up", "down", "up", "down", "up", "down",
"up", "down", "up", "down", "up", "down", "up", "down", "up",
"down"), count = c(7L, 15L, 11L, 5L, 51L, 50L, 49L, 38L, 10L,
2L, 54L, 44L, 55L, 32L, 24L, 32L, 12L, 17L, 31L, 22L)), class = "data.frame", row.names = c(NA,
-20L))
The code and warning messages I'm getting (saying ALL observations were removed) and the plot:
ggplot(data = pcwdegs,
mapping = aes(x = comp, fill = reg,
y = ifelse(test = reg == "down",
yes = -count, no = count))) +
geom_bar(stat = "identity") +
scale_y_continuous(labels = abs, limits = max(pcwdegs$count) * c(-1.05,1.05)) +
geom_text(aes(label=count), vjust=0.5, color="black", size=3.0, nudge_y = c(-1.5,1.5))+
labs(y = "DEGs related to plant cell wall", x = "Groups comparisons") +
scale_fill_manual(values=c("#98FB98","#FA8072","#00FF00","#FF0000"), name = "Expression regulation", labels = c("down-regulated", "up-regulated")) +
scale_x_discrete(limits=c("T12xT24", "C24xT24", "C24xT12", "C12xT24", "C12xT12", "C12xC24", "C0xT24", "C0xT12", "C0xC24","C0xC12")) +
coord_flip()
Warning messages:
1: Removed 20 rows containing missing values (position_stack).
2: Removed 20 rows containing missing values (geom_text).
3: Position guide is perpendicular to the intended axis. Did you mean to specify a different guide `position`?
Looking online for solutions, the suggestions were to check if there were NA values (there weren't) and to set higher limits in limits = max(pcwdegs$count) * c(-1.05,1.05)
(as it was already suggested on my first question and done). So I still have no clue why such similar data is behaving different to the code.
Upvotes: 0
Views: 758
Reputation: 123963
The problem arises because the categories in your new dataset don't fit into the limits defined by scale_x_discrete
. The categories in your new df have an vs
in the middle, while in teh limits it's a x
. Simply replace x
with vs
in the limits and everything is fine.
library(ggplot2)
ggplot(data = pcwdegs,
mapping = aes(x = comp, fill = reg,
y = ifelse(test = reg == "down",
yes = -count, no = count))) +
geom_bar(stat = "identity") +
scale_y_continuous(labels = abs, limits = max(pcwdegs$count) * c(-1.05,1.05)) +
geom_text(aes(label=count), vjust=0.5, color="black", size=3.0, nudge_y = c(-1.5,1.5))+
labs(y = "DEGs related to plant cell wall", x = "Groups comparisons") +
scale_fill_manual(values=c("#98FB98","#FA8072","#00FF00","#FF0000"), name = "Expression regulation", labels = c("down-regulated", "up-regulated")) +
#scale_x_discrete(limits=c("T12xT24", "C24xT24", "C24xT12", "C12xT24", "C12xT12", "C12xC24", "C0xT24", "C0xT12", "C0xC24","C0xC12")) +
scale_x_discrete(limits=c("T12vsT24", "C24vsT24", "C24vsT12", "C12vsT24", "C12vsT12", "C12vsC24", "C0vsT24", "C0vsT12", "C0vsC24","C0vsC12")) +
coord_flip()
Created on 2020-05-23 by the reprex package (v0.3.0)
Upvotes: 1