我有以下
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
以及下面的代码,我创建了两个(单独的)折线图:
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")
)
两段代码的绘图分别是:
对于
g1
子集:
对于 g2
子集:
如何将两个折线图(保持其可视化原样)合并为一个堆叠折线图?正如您所看到的,两个图中的 x 轴是相同的。我想在堆积图中保持这种方式。唯一改变的是 y 轴的范围。
Windows 11、R 4.3.2、RStudio 2023.12.1 内部版本 402。
一种选择是将数据重新整形为长,然后映射美学并通过
scale_xxx_manual
设置自定义颜色和形状。对于突出显示的点和标签,您仍然必须使用数据的子集:
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
)