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? As you can see the x-axis in both graphs is the same. I'd like to keep it that way in the stacked graph. The only thing that changes is the range of the y-axis.
Windows 11, R 4.3.2, RStudio 2023.12.1 Build 402.
One option would be to reshape your data to long, then map on aesthetics and set your custom colors and shape via scale_xxx_manual
. For the highlighted points and labels you still have to use a subset of the data:
df <- data.frame(
std = c(0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2),
near = c(0.849, 0.85, 0.859, 0.872, 0.885, 0.891, 0.902, 0.897, 0.902, 0.898, 0.897, 0.898, 0.896, 0.894, 0.888, 0.89, 0.881, 0.876),
off = c(0.904, 0.916, 0.924, 0.93, 0.936, 0.938, 0.938, 0.938, 0.938, 0.937, 0.936, 0.934, 0.932, 0.928, 0.927, 0.924, 0.917, 0.914)
)
library(ggplot2)
df <- df |>
tidyr::pivot_longer(
-std
)
# plot the data
ggplot(df, aes(x = std, y = value, shape = name, fill = name)) +
geom_line(aes(color = name), linewidth = 0.3) +
geom_point(size = 4, alpha = .3) +
geom_point(
data = ~ subset(.x, (std == .8 & name == "near") | (std == 1 & name == "off")),
size = 4
) +
geom_text(
data = ~ subset(.x, (std == .8 & name == "near") | (std == 1 & name == "off")),
aes(label = std, hjust = ifelse(name == "near", 1, 0)),
vjust = -0.8
) +
scale_x_continuous(breaks = seq(0.3, 2.1, 0.2)) +
scale_color_manual(
values = c(near = "midnightblue", off = "springgreen4"),
aesthetics = c("color", "fill")
) +
scale_shape_manual(
values = c(near = 24, off = 21)
) +
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 = element_line(size = 1, linetype = "solid", colour = "lightgrey"),
axis.title = element_text(size = 20),
axis.text = element_text(size = 17, color = "black")
) +
labs(
x = "PSF width in units of pixels",
y = expression("R"^2),
color = NULL, fill = NULL, shape = NULL
)