This is a test version of Biostars. For the public version, visit https://www.biostars.org.
ggplot2, point with border

Hi,

Hoping someone can help with what may seem like a simple question. I would like to use ggplot2 to plot a graph with a black border but a custom fill.

Using the following code I have managed to puoulate the graph as I would like it:

# 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 and more
>vol +   
  scale_colour_manual(values = cols) +
  ggtitle(label = "Volcano Plot", subtitle = "Colored by fold-change direction") +
  geom_point(size = 2.5, alpha = 1, na.rm = T) +
  theme_bw(base_size = 14) + 
  theme(legend.position = "right") + 
  xlab(expression(log[2]("VitD" / "Carrier"))) + 
  ylab(expression(-log[10]("FDR"))) +
  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")

Gives me the plot:

enter image description here

I understand that I need to define points as shape 21 before defining both color and fill t get different colors for border and fill. So I tried adding ,shape = 21, colour = "black" into my geom_point() agreement, but this turns everything black

>  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"))) + 
  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")

enter image description here

THANKS!!

ggplot r

I added code markup to your post for increased readability. You can do this by selecting the text and clicking the 101010 button. When you compose or edit a post that button is in your toolbar, see image below:

101010 Button

2 answers

Hi,

I assume (haven't tested it on your data), that you should merely replace color = color with fill = color in your assignment of vol

Thanks so much,

This has made the following graph now, so colors and black surrounding working now:

enter image description here

Sorry to be a pain, any idea why my color palette has been overwritten somehow?

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

vol + ggtitle(label = "Volcano Plot", subtitle = "Colored by fold-change direction") +
          scale_colour_manual(values = cols) +
          geom_point(size = 2.5, alpha = 1, na.rm = T, shape = 21, colour = "black") +
          theme_bw(base_size = 14) + # change overall theme
          theme(legend.position = "right") + # change the legend
          xlab(expression(log[2]("VitD" / "Carrier"))) + # Change X-Axis label
          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") + # Add p-adj value cutoff line
          scale_y_continuous(trans = "log1p") # Scale yaxis due to large p-values

This is strange. Is the named vector cols still in your environment?

It is indeed (between ** below), which is why I am very confused at the random colour selection?

vol +   
  ggtitle(label = "Volcano Plot", subtitle = "Colored by fold-change direction") +
  geom_point(size = 2.5, alpha = 1, na.rm = T, shape=21, color = "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"))) + 
  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")

hmmm

The only thing(s) I can suggest right now is to modify scale_colour_manual(name='color', values = cols) and to print cols out (just to check it is still there).

You want to change scale_colour_manual to scale_fill_manual

@reubenmcgregor88, Hi thanks for sharing your code, can you also share the code on seting the "nonsignificant" , "Increased", and "Decreased", so that I can labe different color in the volcano plot. Many thanks.

Log in to answer this question.