I'm trying to plot my data in the order specified in the table, but I can't work out how. I think I've managed to change the levels(?) but not the factors(?)
Link to my data: https://docs.google.com/spreadsheets/d/1juBhHNjvsm1otxcpEFu-Zi_v5hSrNj94zdXrENOglTs/edit?usp=sharing
So far I have:
BigTree <- read_csv(file = "/cloud/project/Phylogeny/RtreeData.csv")
Bfr <- BigTree[with(BigTree, order(-BfrA, -BfrB, -Bfd, -Ftn, -DPS,
-Rubrerythrin, -EncFtn, -NEEF)), ]
BfrLong <- pivot_longer(Bfr, 2:9)
colnames(BfrLong) <- c("Species", "Protein", "Presence")
This works to reorder the dataframe and put the data in the order I want to plot it, however I can't plot this dataframe. The error says
Error in ggplot(BfrLong, aes(x = Protein, y = Species, fill = Presence)) + : non-numeric argument to binary operator
I tried to get the same result using the factor function as below, but it didn't have the desired effect. What am I doing wrong here?
BigTreeLong$Protein <- factor(BigTreeLong$Protein,
levels = c("BfrA", "BfrB", "Bfd", "Ftn", "DPS",
"Rubrerythrin", "EncFtn", "NEEF"))
How do I change the factors so the order is maintained during plotting?
My heatmap code so far uses the original BigTree dataframe in long form. I want to sort it so the top left corner starts with the most light green and it gradually decreases to mostly dark green in the bottom rows.
ggplot(BigTreeLong, aes(Protein, Species, fill= Presence)) +
geom_tile(color = "white", linetype = 1) +
scale_fill_gradient(low = "darkgreen", high = "olivedrab3") +
labs(title = "Ferritins present in different bacteria",
x = "Ferritin family proteins",
y = "Species",
fill = "Presence of protein") +
theme_minimal() +
theme(axis.text.x = element_text(angle=45, hjust=1, size=8)) +
theme(axis.text.y = element_text(angle=0, hjust=1, size=6)) +
scale_x_discrete(limits = c("BfrA", "BfrB", "Bfd", "Ftn", "DPS",
"EncFtn", "NEEF", "Rubrerythrin"))
This is the heatmap I currently make
I hope I've included everything I needed to!
The levels of your Species column have not been modified in your code, so you will need this line to make it registered :
BfrLong$Species=factor(BfrLong$Species,levels=Bfr$Species)
Pay attention to the name of your objects in your example, there is BfrLong and BigTreeLong but this should work :
BigTree <- read_csv(file = "/cloud/project/Phylogeny/RtreeData.csv")
Bfr <- BigTree[with(BigTree, order(-BfrA, -BfrB, -Bfd, -Ftn, -DPS,
-Rubrerythrin, -EncFtn, -NEEF)), ]
BfrLong <- pivot_longer(Bfr, 2:9)
colnames(BfrLong) <- c("Species", "Protein", "Presence")
BfrLong$Species=factor(BfrLong$Species,levels=Bfr$Species)
ggplot(BfrLong, aes(Protein, Species, fill= Presence)) +
geom_tile(color = "white", linetype = 1) +
scale_fill_gradient(low = "darkgreen", high = "olivedrab3") +
labs(title = "Ferritins present in different bacteria",
x = "Ferritin family proteins",
y = "Species",
fill = "Presence of protein") +
theme_minimal() +
theme(axis.text.x = element_text(angle=45, hjust=1, size=8)) +
theme(axis.text.y = element_text(angle=0, hjust=1, size=6)) +
scale_x_discrete(limits = c("BfrA", "BfrB", "Bfd", "Ftn", "DPS",
"EncFtn", "NEEF", "Rubrerythrin"))