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.