ggplot2, point with border
2
2
Entering edit mode
6.7 years ago

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 • 39k views
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

Ah, gotcha.

Thanks will remember this next time.

ADD REPLY
5
Entering edit mode
6.7 years ago
e.rempel ★ 1.1k

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

ADD COMMENT
1
Entering edit mode

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
ADD REPLY
1
Entering edit mode

You want to change scale_colour_manual to scale_fill_manual

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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).

ADD REPLY
0
Entering edit mode
4.5 years ago
yaleliu73 • 0

@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.

ADD COMMENT

Login before adding your answer.

Traffic: 2845 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