Position of Scale bar
1
0
Entering edit mode
5 months ago
Trinh • 0

Hi, I have 2 dot plots with the same code. However, sometimes p-value bars change position automatically.

enter image description here Please help me, how to anchor it? Thank you very much!

#Dotplot
##########GO analysis done with the GO analysis tool at the CGD (default settings)###############
GO <- readxl::read_excel("Cluster1_GO_CGD.xlsx", sheet = "plotting")
GO
#Upregulation
go1 <- filter(GO, Sample == "MouseUp", Pval_adjust <= 0.05)
p1 <- ggplot(go1[-c(4,5),], aes(x=reorder(GO, -Pval_adjust), y=Input_ratio)) + 
  geom_point(aes(size=hits, col=Pval_adjust)) + 
  scale_size_continuous(name = "No. of Prots", range = c(3,6)) +
  coord_flip() + scale_color_distiller(name = "Adj. p-value", palette = "OrRd") + xlab("") + ylab("Input ratio")
p1 +
  theme(axis.title.y = element_text(face = "plain", size = 18, color = "black", margin = margin(0,0.3,0,0,"cm"))) +
  theme(axis.title.x = element_text(face = "plain", size = 15, color = "black", margin = margin(0.3,0,0,0,"cm"))) +
  theme(axis.text.x = element_text(face = "plain", size = 12, color = "black", angle = 25, vjust = 0.2, hjust = 0.2)) +
  theme(axis.text.y = element_text(face = "plain", size = 25, color = "black", angle = 0, margin = margin(0,0.1,0,0,"cm")))
#Downregulation
go2 <- filter(GO, Sample == "MouseDown", Pval_adjust <= 0.05)
p2 <- ggplot(go2[-c(4,5),], aes(x=reorder(GO, -Pval_adjust), y=Input_ratio)) + 
  geom_point(aes(size=hits, col=Pval_adjust)) + 
  scale_size_continuous(name = "No. of Prots", range = c(3,6)) +
  coord_flip() + 
  scale_color_distiller(name = "Adj. p-value", palette = "PuBu") + xlab("") + ylab("Input ratio")
p2 +
  theme(axis.title.y = element_text(face = "plain", size = 18, color = "black", margin = margin(0,0.3,0,0,"cm"))) +
  theme(axis.title.x = element_text(face = "plain", size = 15, color = "black", margin = margin(0.3,0,0,0,"cm"))) +
  theme(axis.text.x = element_text(face = "plain", size = 12, color = "black", angle = 25, vjust = 0.2, hjust = 0.2)) +
  theme(axis.text.y = element_text(face = "plain", size = 20, color = "black", angle = 0, margin = margin(0,0.1,0,0,"cm")))
library(gridExtra)
cowplot::plot_grid(p1, p2, ncol = 2, labels = c("A", "B"))
ggplot2 • 286 views
ADD COMMENT
0
Entering edit mode
5 months ago
Ram 34k

You can add + guides(col = guide_legend(order = 1), size = guide_legend(order = 2)) to both plots to maintain consistent legend ordering among the multiple legends.

I got this solution from this page: https://www.edureka.co/community/52697/change-the-order-of-multiple-legends-in-ggplot which is the top result when one googles "ggplot order multiple legends". A bit of Google often helps solve most of these problems really quick.

ADD COMMENT

Login before adding your answer.

Traffic: 2236 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6