Stacked line graph

I have the following data.table:

> df
# A tibble: 18 × 3
     std  near   off
   <dbl> <dbl> <dbl>
 1   0.3 0.849 0.904
 2   0.4 0.850 0.916
 3   0.5 0.859 0.924
 4   0.6 0.872 0.930
 5   0.7 0.885 0.936
 6   0.8 0.891 0.938
 7   0.9 0.902 0.938
 8   1   0.897 0.938
 9   1.1 0.902 0.938
10   1.2 0.898 0.937
11   1.3 0.897 0.936
12   1.4 0.898 0.934
13   1.5 0.896 0.932
14   1.6 0.894 0.928
15   1.7 0.888 0.927
16   1.8 0.890 0.924
17   1.9 0.881 0.917
18   2   0.876 0.914

and the below code where I create two (separate) line graphs:

library(ggplot2)
library(dplyr)
library(tibble)
library(data.table)

wd <- "path/"

df <- fread(paste0(wd, "r2_la.csv"))

df$near <- format(df$near, digits = 4)
df$off <- format(df$off, digits = 4)
df$std <- format(df$std, digits = 2)

df$near <- as.numeric(df$near)
df$off <- as.numeric(df$off)
df$std <- as.numeric(df$std)

# Create subsets
g1 <- subset(df, std == 0.8)
g2 <- subset(df, std == 1)

# plot the data
ggplot(df, aes(x = std, y = near)) + 
  geom_line( color = "midnightblue", linewidth = 0.3, group = 1) +
  geom_point(shape = 24, fill = "midnightblue", size = 4, alpha = I(0.3)) +
  geom_point(data = g1, shape = 24, fill = "midnightblue", size = 4) +  # this adds a red point
  geom_text(data = g1, label = "0.8", vjust = -0.8, hjust = 1) + # this adds a label for the red point 
  scale_x_continuous(breaks = seq(0.3, 2.1, 0.2)) +
  theme(
    plot.title = element_text(color = "black", size = 17, face = "bold", hjust = 0.5),
    plot.subtitle = element_text(size = 10, face = "bold", hjust = 0.5, color = "black"),
    plot.caption = element_text(face = "italic", hjust = 0), 
    panel.background = element_rect(fill = 'transparent'), 
    axis.line.x = element_line(size = 1, linetype = "solid", colour = "lightgrey"),
    axis.line.y = element_line(size = 1, linetype = "solid", colour = "lightgrey"),
  ) +
  xlab("PSF width in units of pixels") + labs(y = expression("R"^2)) +
  theme(
    axis.title.x = element_text(size = 20),
    axis.title.y = element_text(size = 20),
    axis.text = element_text(size = 17, color = "black")
  )

# plot the data
ggplot(df, aes(x = std, y = off)) + 
  geom_line( color = "springgreen4", linewidth = 0.3, group = 1) +
  geom_point(shape = 21, fill = "springgreen4", size = 4, alpha = I(0.3)) +
  geom_point(data = g2, shape = 21, fill = "springgreen4", size = 4) +  # this adds a red point
  geom_text(data = g2, label = "1", vjust = -0.8, hjust = -0.1) + # this adds a label for the red point 
  scale_x_continuous(breaks = seq(0.3, 2.1, 0.2)) +
  theme(
    plot.title = element_text(color = "black", size = 17, face = "bold", hjust = 0.5),
    plot.subtitle = element_text(size = 10, face = "bold", hjust = 0.5, color = "black"),
    plot.caption = element_text(face = "italic", hjust = 0), 
    panel.background = element_rect(fill = 'transparent'), 
    axis.line.x = element_line(size = 1, linetype = "solid", colour = "lightgrey"),
    axis.line.y = element_line(size = 1, linetype = "solid", colour = "lightgrey"),
  ) +
  xlab("PSF width in units of pixels") + labs(y = expression("R"^2)) +
  theme(
    axis.title.x = element_text(size = 20),
    axis.title.y = element_text(size = 20),
    axis.text = element_text(size = 17, color = "black")
  )

The plots of the two pieces of code are:
For the g1 subset:

For the g2 subset:

How can I combine the two line graphs (keeping their visualization as it is) to a single stacked line graph?

Windows 11, R 4.3.2, RStudio 2023.12.1 Build 402.

Move the aes() from ggplot() to each geom_*()
Simplified example:

ggplot(df) + 
  geom_line(aes(x = std, y = near), color = "midnightblue", linewidth = 0.3, group = 1) +
  geom_line(aes(x = std, y = off), color = "springgreen4", linewidth = 0.3, group = 1)
1 Like

@mduvekot's solution is good. I chose to pivot the data, which makes it easy to get a legend explaining the colors.

library(tidyverse)
df <- read.csv("~/R/Play/Dummy.csv", sep = " ")
df$near <- format(df$near, digits = 4)
df$off <- format(df$off, digits = 4)
df$std <- format(df$std, digits = 2)

df$near <- as.numeric(df$near)
df$off <- as.numeric(df$off)
df$std <- as.numeric(df$std)

# Create subsets
g1 <- subset(df, std == 0.8)
g2 <- subset(df, std == 1)

df <- df |> pivot_longer(cols = near:off, names_to = "Dist")


# plot the data  "midnightblue"  "springgreen4" shape = 24, shape = 21,
ggplot(df, aes(x = std, y = value, color = Dist)) + 
  geom_line(linewidth = 0.3) +
  geom_point(size = 4, alpha = I(0.3)) +
  geom_point(aes(x = std, y = near),inherit.aes = FALSE, data = g1, shape = 24, fill = "midnightblue", size = 4) +  # this adds a red point
  geom_text(aes(x = std, y = near),inherit.aes = FALSE, data = g1, label = "0.8", vjust = -0.8, hjust = 1) + # this adds a label for the red point 
  geom_point(aes(x = std, y = off), inherit.aes = FALSE, data = g2, shape = 21, fill = "springgreen4", size = 4) +  # this adds a red point
  geom_text(aes(x = std, y = off), data = g2,inherit.aes = FALSE, label = "1", vjust = 1, hjust = -0.7) + # this adds a label for the red point 
  scale_x_continuous(breaks = seq(0.3, 2.1, 0.2)) +
  scale_color_manual(values = c(near="midnightblue", off="springgreen4")) +
  scale_fill_manual(values = c(near="midnightblue", off="springgreen4")) +
  theme(
    plot.title = element_text(color = "black", size = 17, face = "bold", hjust = 0.5),
    plot.subtitle = element_text(size = 10, face = "bold", hjust = 0.5, color = "black"),
    plot.caption = element_text(face = "italic", hjust = 0), 
    panel.background = element_rect(fill = 'transparent'), 
    axis.line.x = element_line(size = 1, linetype = "solid", colour = "lightgrey"),
    axis.line.y = element_line(size = 1, linetype = "solid", colour = "lightgrey"),
  ) +
  xlab("PSF width in units of pixels") + labs(y = expression("R"^2)) +
  theme(
    axis.title.x = element_text(size = 20),
    axis.title.y = element_text(size = 20),
    axis.text = element_text(size = 17, color = "black")
  )
#> Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
#> ℹ Please use the `linewidth` argument instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.

Created on 2024-02-25 with reprex v2.0.2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.