2

So, here is my issue. I have data similar to the minimal reproducible example below that I want to plot with ggplot2, where a data frame contains one column that I want to use as the x-values for every geom and several columns that I want as different y-values. As you can see from the example below, I want to combine geom_line and geom_point with different shapes for the geom_point values.

My question now is, how do I insert a legend that tells me what columns which shape represents. From what I have learned about ggplot2 so far, a legend is usually generated, if I map some factor (might not be the correct term) to color = or group =, I am wrong? So how can I still get a legend without that prerequisite?

Help is much appreciated!

library(tidyverse)

df <- structure(list(rep = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1), Y_1 = c(0.0198, 0.0108, 0, 0.0117, 0.00931, 0.0089, 0.0115, 
0.00509, 0.00831, 0.0158, 0.0437, 0.0953, 0.267, 0.677, 1.81), 
    Y_2 = c(0.025, 0.00249, 0.00303, 0.00268, 0.0102, 0.0112, 
    0.0231, 0.0326, 0.0575, 0.0852, 0.143, 0.219, 0.384, 0.687, 
    1.01), X = c(0.1, 0.164, 0.268, 0.439, 0.72, 1.18, 1.93, 
    3.16, 5.18, 8.48, 13.9, 22.8, 37.3, 61.1, 100)), row.names = c(NA, 
15L), class = "data.frame")

df_plot <- ggplot(data = df) +
  geom_line(mapping = aes(x = X, y = Y_1)) +
  geom_point(mapping = aes(x = X, y = Y_1), shape = 15) +
  geom_line(mapping = aes(x = X, y = Y_2)) +
  geom_point(mapping = aes(x = X, y = Y_2), shape = 0) +
  scale_x_log10() +
  scale_y_log10() +
  theme_classic()

df_plot

1 Answer 1

2

The way to create a legend is to map the different levels of a variable to an aesthetic scale (in your case, a shape scale). The most idiomatic way to do this in ggplot is reshape your data into long format, by making Y_1 and Y_2 into a single column of y values, with a new column that labels each y value according to the original column it came from. This means you only need a single call to geom_line and a single call to geom_point:

ggplot(data = tidyr::pivot_longer(df, c("Y_1", "Y_2"))) +
  geom_line(mapping = aes(x = X, y = value, group = name)) +
  geom_point(mapping = aes(x = X, y = value, shape = name)) +
  scale_shape_manual(values = c(0, 15)) +
  scale_x_log10() +
  scale_y_log10() +
  labs(shape = "variable") +
  theme_classic()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.