3

I am cross posting from another forum (https://www.biostars.org/p/268514/#268559) where I got useful comments but still am confused by output of ggplot2.

I am trying to get a simple black outline on circles but my code is now giving my random colours and seems to be ignoring my scale_colour_manual(values = cols) argument. When I run the following code:

# creating color palette

> cols <- c("red" = "red", "orange" = "orange", "nonsignificant" = "darkgrey", "Increased" = "#00B2FF", "Decreased" = "#00B2FF")

# Make a basic ggplot2 object

> vol <- ggplot(data, aes(x = lfc, y = pval, color = color, labels=gene))

# inserting mnaual colors as per color pallette  with term "scale_colour_manual(values = cols)" below

> vol +   
  ggtitle(label = "Volcano Plot", subtitle = "Colored by fold-change direction") +
  geom_point(size = 2.5, alpha = 1, na.rm = T) +
  scale_colour_manual(values = cols) +
  theme_bw(base_size = 14) + 
  theme(legend.position = "right") + 
  xlab(expression(log[2]("VitD" / "Carrier"))) + 
  ylab(expression(-log[10]("FDR"))) + # Change Y-Axis label
  geom_hline(yintercept = 1, colour="#990000", linetype="dashed") + geom_vline(xintercept = 0.586, colour="#990000", linetype="dashed") + geom_vline(xintercept = -0.586, colour="#990000", linetype="dashed") + 
  scale_y_continuous(trans = "log1p") 

I get a beautiful graph with my defined colour palette:

Color volcano plot

Next, I want to add a border to the points and so I only change 2 pieces of code. At the start I make the basic ggplot object with "fill = color" instead of "color = color"

> vol <- ggplot(data, aes(x = lfc, y = pval, fill = color, labels=gene))

And I add "shape = 21, colour = "black"" to the "geom_point" argument:

> vol +   
  ggtitle(label = "Volcano Plot", subtitle = "Colored by fold-change direction") +
  geom_point(size = 2.5, alpha = 1, na.rm = T, shape = 21, colour = "black") +
  scale_colour_manual(values = cols) +
  theme_bw(base_size = 14) + 
  theme(legend.position = "right") + 
  xlab(expression(log[2]("VitD" / "Carrier"))) + 
  ylab(expression(-log[10]("FDR"))) + l
  geom_hline(yintercept = 1, colour="#990000", linetype="dashed") + geom_vline(xintercept = 0.586, colour="#990000", linetype="dashed") + geom_vline(xintercept = -0.586, colour="#990000", linetype="dashed") + 
  scale_y_continuous(trans = "log1p") 

And suddenly I have the a plot with what seems to be a random selection of 5 colours; and if I print the colors:

print(cols)

red      orange         nonsignificant      Increased      Decreased 
"red"    "orange"      "darkgrey"           "#00B2FF"      "#00B2FF" 

The palette is still there.

M--
  • 25,431
  • 8
  • 61
  • 93
reubenmcg
  • 371
  • 4
  • 18
  • Possible duplicate of [Place a border around points](https://stackoverflow.com/questions/10437442/place-a-border-around-points) – M-- Aug 21 '17 at 21:15

2 Answers2

5

When you change color = cols to fill = cols, you should also change scale_color_manual(...) to scale_fill_manual(...)

Brian
  • 7,900
  • 1
  • 27
  • 41
2

I usually solve this problem just by adding larger black points behind coloured ones.

ggplot(mtcars, aes(wt, qsec)) +
    geom_point(color = "black", size = 2.5) +
    geom_point(size = 2, aes(color = factor(am)))

enter image description here

pogibas
  • 27,303
  • 19
  • 84
  • 117
  • Thanks for the reply, This works but when I have lots of points like in the bottom right and left of my graph these black circles are behind all my data points. – reubenmcg Aug 21 '17 at 21:13
  • 2
    There are times when I prefer this method to using a shape with both stroke color and fill. When you have a dense cluster of points, you end up with a blobby filled shape outlined in a single black outline, which can look better than multiple overlapping filled circles. – Brian Aug 21 '17 at 23:08