1
votes

This is my attempt:

  ggplot() + 
  geom_point(data = standard_expression_TS.kmeans.df, aes(A, B, color = "DF1")) +
  geom_point( data = experimental_expression_TS.kmeans.df, aes(A, B, color = "DF2"), shape = 1, alpha = 0.8) +
  geom_point(data = as.data.frame(standard_expression_TS.kmeans$centers), aes(A, B, color='DF3'), shape=4, size=8) + 
  labs(title="Expression Scatterplot") + theme(plot.title = element_text(hjust = 0.5)) +
  scale_colour_manual(values=c("DF1"="black", "DF2"="blue", "DF3"="red"), name="Genes")

And this is how my plot looks:enter image description here

I am trying to add a legend that has each of the 3 point types (black dot, blue circle, red x) followed by a string ("DF1", "DF2", "DF3"). If possible, I would love to have it ON the plot, flush with the bottom left corner.

Note: I am looking for a way of doing this that avoids melting all the data together.

EDIT: When I change the names of "DF1-3", it stops working.

ggplot() + 
  geom_jitter(data = mtcars, aes(hp, mpg, color = "Standard")) +
  geom_jitter(data = mtcars, aes(hp, mpg, color = "Experimental"), shape = 1, alpha = 0.8) +
  geom_jitter(data = mtcars, aes(hp, mpg, color='Center'), shape=4, size=8) + 
  labs(title="Expression Scatterplot") + 
  theme(plot.title = element_text(hjust = 0.5), 
        legend.position = c(0, 0),
        legend.justification = c("left", "bottom")) +
  scale_colour_manual(values=c("Standard"="black", "Experimental"="blue", "Center"="red"), name="Genes") +
  guides(color = guide_legend(override.aes = list(shape = c(16, 1, 4), size = c(1.5, 1.5, 8))))

1

1 Answers

1
votes

Using mtcars as example dataset and geom_jitter instead of geom_point this could be achieved like so:

  1. The legend can be styled via guide_legend which allows you to override the default aes to specifiy the shape and size of each legend key

  2. The positioning of the legend could be achieved via theme options legend.position and legend.justification. legend.justification sets the x- and y-axis anchor for the positioning of the legend, while legend.position (between 0 and 1) sets the position relative to these anchor.

library(ggplot2)

ggplot() + 
  geom_jitter(data = mtcars, aes(hp, mpg, color = "DF1")) +
  geom_jitter(data = mtcars, aes(hp, mpg, color = "DF2"), shape = 1, alpha = 0.8) +
  geom_jitter(data = mtcars, aes(hp, mpg, color='DF3'), shape=4, size=8) + 
  labs(title="Expression Scatterplot") + 
  theme(plot.title = element_text(hjust = 0.5), 
        legend.position = c(0, 0),
        legend.justification = c("left", "bottom")) +
  scale_colour_manual(values=c("DF1"="black", "DF2"="blue", "DF3"="red"), name="Genes") +
  guides(color = guide_legend(override.aes = list(shape = c(16, 1, 4), size = c(1.5, 1.5, 8))))

The easiest way to change the labels in the legend is to set them via labels in scale_color_manual:

library(ggplot2)

ggplot() + 
  geom_jitter(data = mtcars, aes(hp, mpg, color = "DF1")) +
  geom_jitter(data = mtcars, aes(hp, mpg, color = "DF2"), shape = 1, alpha = 0.8) +
  geom_jitter(data = mtcars, aes(hp, mpg, color='DF3'), shape=4, size=8) + 
  labs(title="Expression Scatterplot") + 
  theme(plot.title = element_text(hjust = 0.5), 
        legend.position = c(0, 0),
        legend.justification = c("left", "bottom")) +
  scale_colour_manual(values = c(DF1 = "black", DF2 = "blue", DF3 = "red"), 
                      labels = c(DF1 = "Standard", DF2 = "Experimental", DF3 = "Center"),
                      name = "Genes") +
  guides(color = guide_legend(override.aes = list(shape = c(16, 1, 4), size = c(1.5, 1.5, 8))))