1

I am trying to change the order of points in ggplot, so far I have the following code,

up   <- top.table$adj.P.Val < 0.05 & top.table$logFC > 1
down <- top.table$adj.P.Val < 0.05 & top.table$logFC < -1
non  <- !(up | down)

df_up   <- data.frame(x=aveLogCPM[up]  , y=top.table$logFC[up])
df_down <- data.frame(x=aveLogCPM[down], y=top.table$logFC[down])
df_non  <- data.frame(x=aveLogCPM[non] , y=top.table$logFC[non])

ggplot() +
    geom_point(data = df_up  , aes(x=x, y=y), color = "red") +
    geom_point(data = df_down, aes(x=x, y=y), color = "blue") + 
    geom_point(data = df_non , aes(x=x, y=y), color = "black")

which results in

what I get

however, I want something like this:

what I want

Igor F.
  • 2,649
  • 2
  • 31
  • 39
eonurk
  • 507
  • 2
  • 12
  • sorry for the inconvience, I can't post images directly since I have 9 reputations. – eonurk Jan 07 '20 at 15:14
  • 4
    To my knowledge (hard to check without your data), ggplot will stack the points in the order you supplied them. So you need just to reorder your geom_points: first non-DE, and then Up and Down. – Igor F. Jan 07 '20 at 15:18
  • 1
    you can also lower the alpha in your non, geom_point(data = df_non , aes(x=x, y=y), color = "black",alpha=0.6) maybe – StupidWolf Jan 07 '20 at 15:19

1 Answers1

1

To clarify @Igor F.'s answer:

ggplot() +
    geom_point(data = df_non , aes(x=x, y=y), color = "black") +
    geom_point(data = df_up  , aes(x=x, y=y), color = "red") +
    geom_point(data = df_down, aes(x=x, y=y), color = "blue") 

You could also, as done in your desired image, reduce the size of the black points and increase the size of the red/blue points, in conjunction with the above layering (you'll play around with the actual size value to get it right):

ggplot() +
    geom_point(data = df_non , aes(x=x, y=y), color = "black", size = 1) +
    geom_point(data = df_up  , aes(x=x, y=y), color = "red", size = 3) +
    geom_point(data = df_down, aes(x=x, y=y), color = "blue", size = 3) 
TheSciGuy
  • 1,154
  • 11
  • 22
  • Thank you, this solved my ordering problem. However, I still need to add a legend to the graph corresponding to the different colors. – eonurk Jan 07 '20 at 15:26
  • If you want to keep your dataframes separate (which is not recommended for `ggplot2`) OR you want to merge them, you will be able to do so following this post: https://stackoverflow.com/questions/10349206/add-legend-to-ggplot2-line-plot – TheSciGuy Jan 07 '20 at 15:28