在 x 轴上制作具有多个变量的深度剖面,包括误差线
Making a depth profile with multiple variables on the x axis, including error bars
我有一份图表需要添加误差线。我的数据来自一个 CSV 文件,我尝试将其分成 4 个不同的、可行的部分,用于 4 个深度剖面(配体和 logK;第 5 站和第 9 站)。以下代码适用于其中一个配置文件,希望我能够再重新创建此解决方案 3 次。现在,我的代码看起来像这样,我在其中读取了一个 CSV 文件,将其从宽转换为长,并使用 ggplot 函数绘制。但是,我无法想象如何在没有 ggplot 的情况下添加水平误差线,认为误差线的列是我想在图表上绘制的实际点。我觉得这与我一开始的数据争论有关,但我不确定是什么。 (注意:这是我第一次 post 在这里,所以如果它不是一个真正的 reprex,请告诉我!!我尽力说清楚了,但如果不是我会尝试修改)。
What I have so far...without error bars
注意:我的实际情节也调整了许多美学,但为了尽量减少代码,我将这些行省略了。
Figure with error bar data plotted as points on the graph
此图为 gather()
函数调整了行,其中错误列作为长数据测量列的一部分包含在内;下面列出供参考
station5_L1_long <- gather(station5_L1, col_names, measurement, dFe:L1_diff_from_mean, factor_key=TRUE)
Figure with adjusted aesthetics
library(ggplot2) #using the ggplot package to plot
library(magrittr) #using the magrittr package to pipe
library(tidyr) #using the tidyr package to convert between wide and long data forms
dput(ligand_data[1:10, ]) #ligand data frame including both station 5 and 9
#dput() result
structure(list(Station = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = c("5", "9"), class = "factor"), Depth = c(2L,
2700L, 3000L, 30L, 3300L, 3600L, 3900L, 4200L), dFe = c(0.31,
0.65, 0.66, 0.3, 0.65, 0.62, 0.61, 0.61), L1ship_nM = c(1.265,
1.46, NA, 1.365, NA, NA, 1.33, NA), L1lab_nM = c(1.32, 1.93,
1.92, 1.35, 2.23, 1.99, 1.8, 2.4), L1A_nM = c(1.18, 1.37, NA,
1.39, NA, NA, 1.36, NA), L1B_nM = c(1.35, 1.55, NA, 1.34, NA,
NA, 1.3, NA), L1all_nM = c(1.283333333, 1.616666667, NA, 1.36,
NA, NA, 1.486666667, NA), L1freeze2013_nM = c(1.52, NA, NA, NA,
NA, NA, NA, NA), L1_allfreeze_nM = c(1.42, 1.93, NA, 1.35, NA,
NA, 1.8, NA), L1_ALL_nM = c(1.3425, 1.616666667, NA, 1.36, NA,
NA, 1.486666667, NA), L1shipSD_nM = c("0.120208153", "0.127279221",
"", "0.035355339", "", "", "0.042426407", ""), L1allSD_nM = c(0.090737717,
0.285890422, NA, 0.026457513, NA, NA, 0.273007936, NA), L1_allfreezeSD_nM = c(0.141421356,
NA, NA, NA, NA, NA, NA, NA), L1_ALL_SD_nM = c(0.139612559, 0.285890422,
NA, 0.026457513, NA, NA, 0.273007936, NA)), row.names = c(NA,
8L), class = "data.frame")
##################### CLEANING DATA ###########################
ligand_data <- merge(base3_cols, ligand, by.x = 0, by.y = 0, all.x = TRUE) %>%
select(-Row.names)
#filtering for depth profiles
station5_L1 <- ligand_data %>% filter(Station == 5) #filtering ligand df to include just station 5
#changing from wide to long
station5_L1_long <- gather(station5_L1, col_names, measurement, dFe:L1_diff_from_mean, factor_key=TRUE)
################################ PLOTS w/ggplot ##################################
station5L1_depth_profile <- ggplot(data = station5_L1_long,
aes(color = col_names,
shape = col_names,
fill = col_names,
size = 0.25)
) +
geom_point(mapping = aes(
x = as.numeric(measurement),
y = as.numeric(Depth),
size = 0.25
)) +
scale_y_reverse() +
guides(size = FALSE) +
scale_x_continuous(position = "top", breaks = scales::breaks_width(0.5)) + #moves x-axis to top
expand_limits(x = c(0, 2.5)) +
scale_shape_manual(values=c(4, 21, 21, 23, 22, 25, 24, 13, 5))+
scale_fill_manual(#labels = c("dFe", "A", "B", "ship", "lab", "2013", "all pre-2013", "all"),
values=c("#000000", "#74ADD1", "#D73027", "#4575B4", "#ABD9E9",
"#E0F3F8", "#F46D43", "#313695", "#000001")) +
scale_color_manual(values=c("#000000", "#000000", "#000000", "#000000", "#000000",
"#000000", "#000000", "#000000", "#000000"))
station5L1_depth_profile
希望我能正确理解您的问题。这是我的解决方案。首先,我将数据再次拆分为两个数据帧,因为它更容易处理数据透视。接下来,我将它们转换为长格式并准备加入。
加入后我可以绘制它并计算线范围。
下次也请尝试解释您的数据集,因为没有任何进一步的信息很难理解。
library(tidyverse)
# make everything numeric
# as far as I can see this makes sense
df <- df %>%
mutate(
across(everything(), as.numeric)
)
# For easier manipulating we split the df
main_df <- df %>% select(Station:L1_ALL_nM)
sd_df <- df %>% select(Station:Depth, L1shipSD_nM:L1_ALL_SD_nM)
# now we pivot longer
main_df <- main_df %>%
pivot_longer(cols = dFe:L1_ALL_nM, names_to = "col_names", values_to = "val")
sd_df <- sd_df %>%
pivot_longer(cols = L1shipSD_nM:L1_ALL_SD_nM, names_to = "col_names", values_to = "sd") %>%
mutate(
# remove SD from string, we dont need it
col_names = str_replace_all(col_names, "SD", "")
)
# join the tables
plot_df <- main_df %>% full_join(sd_df)
# Plot our result
plot_df %>%
ggplot(
aes(y = Depth, x = val, color = col_names, shape = col_names, fill = col_names)
) +
geom_pointrange(
aes(xmin = val - sd, xmax = val + sd)
)
我有一份图表需要添加误差线。我的数据来自一个 CSV 文件,我尝试将其分成 4 个不同的、可行的部分,用于 4 个深度剖面(配体和 logK;第 5 站和第 9 站)。以下代码适用于其中一个配置文件,希望我能够再重新创建此解决方案 3 次。现在,我的代码看起来像这样,我在其中读取了一个 CSV 文件,将其从宽转换为长,并使用 ggplot 函数绘制。但是,我无法想象如何在没有 ggplot 的情况下添加水平误差线,认为误差线的列是我想在图表上绘制的实际点。我觉得这与我一开始的数据争论有关,但我不确定是什么。 (注意:这是我第一次 post 在这里,所以如果它不是一个真正的 reprex,请告诉我!!我尽力说清楚了,但如果不是我会尝试修改)。
What I have so far...without error bars
注意:我的实际情节也调整了许多美学,但为了尽量减少代码,我将这些行省略了。
Figure with error bar data plotted as points on the graph
此图为 gather()
函数调整了行,其中错误列作为长数据测量列的一部分包含在内;下面列出供参考
station5_L1_long <- gather(station5_L1, col_names, measurement, dFe:L1_diff_from_mean, factor_key=TRUE)
Figure with adjusted aesthetics
library(ggplot2) #using the ggplot package to plot
library(magrittr) #using the magrittr package to pipe
library(tidyr) #using the tidyr package to convert between wide and long data forms
dput(ligand_data[1:10, ]) #ligand data frame including both station 5 and 9
#dput() result
structure(list(Station = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = c("5", "9"), class = "factor"), Depth = c(2L,
2700L, 3000L, 30L, 3300L, 3600L, 3900L, 4200L), dFe = c(0.31,
0.65, 0.66, 0.3, 0.65, 0.62, 0.61, 0.61), L1ship_nM = c(1.265,
1.46, NA, 1.365, NA, NA, 1.33, NA), L1lab_nM = c(1.32, 1.93,
1.92, 1.35, 2.23, 1.99, 1.8, 2.4), L1A_nM = c(1.18, 1.37, NA,
1.39, NA, NA, 1.36, NA), L1B_nM = c(1.35, 1.55, NA, 1.34, NA,
NA, 1.3, NA), L1all_nM = c(1.283333333, 1.616666667, NA, 1.36,
NA, NA, 1.486666667, NA), L1freeze2013_nM = c(1.52, NA, NA, NA,
NA, NA, NA, NA), L1_allfreeze_nM = c(1.42, 1.93, NA, 1.35, NA,
NA, 1.8, NA), L1_ALL_nM = c(1.3425, 1.616666667, NA, 1.36, NA,
NA, 1.486666667, NA), L1shipSD_nM = c("0.120208153", "0.127279221",
"", "0.035355339", "", "", "0.042426407", ""), L1allSD_nM = c(0.090737717,
0.285890422, NA, 0.026457513, NA, NA, 0.273007936, NA), L1_allfreezeSD_nM = c(0.141421356,
NA, NA, NA, NA, NA, NA, NA), L1_ALL_SD_nM = c(0.139612559, 0.285890422,
NA, 0.026457513, NA, NA, 0.273007936, NA)), row.names = c(NA,
8L), class = "data.frame")
##################### CLEANING DATA ###########################
ligand_data <- merge(base3_cols, ligand, by.x = 0, by.y = 0, all.x = TRUE) %>%
select(-Row.names)
#filtering for depth profiles
station5_L1 <- ligand_data %>% filter(Station == 5) #filtering ligand df to include just station 5
#changing from wide to long
station5_L1_long <- gather(station5_L1, col_names, measurement, dFe:L1_diff_from_mean, factor_key=TRUE)
################################ PLOTS w/ggplot ##################################
station5L1_depth_profile <- ggplot(data = station5_L1_long,
aes(color = col_names,
shape = col_names,
fill = col_names,
size = 0.25)
) +
geom_point(mapping = aes(
x = as.numeric(measurement),
y = as.numeric(Depth),
size = 0.25
)) +
scale_y_reverse() +
guides(size = FALSE) +
scale_x_continuous(position = "top", breaks = scales::breaks_width(0.5)) + #moves x-axis to top
expand_limits(x = c(0, 2.5)) +
scale_shape_manual(values=c(4, 21, 21, 23, 22, 25, 24, 13, 5))+
scale_fill_manual(#labels = c("dFe", "A", "B", "ship", "lab", "2013", "all pre-2013", "all"),
values=c("#000000", "#74ADD1", "#D73027", "#4575B4", "#ABD9E9",
"#E0F3F8", "#F46D43", "#313695", "#000001")) +
scale_color_manual(values=c("#000000", "#000000", "#000000", "#000000", "#000000",
"#000000", "#000000", "#000000", "#000000"))
station5L1_depth_profile
希望我能正确理解您的问题。这是我的解决方案。首先,我将数据再次拆分为两个数据帧,因为它更容易处理数据透视。接下来,我将它们转换为长格式并准备加入。
加入后我可以绘制它并计算线范围。
下次也请尝试解释您的数据集,因为没有任何进一步的信息很难理解。
library(tidyverse)
# make everything numeric
# as far as I can see this makes sense
df <- df %>%
mutate(
across(everything(), as.numeric)
)
# For easier manipulating we split the df
main_df <- df %>% select(Station:L1_ALL_nM)
sd_df <- df %>% select(Station:Depth, L1shipSD_nM:L1_ALL_SD_nM)
# now we pivot longer
main_df <- main_df %>%
pivot_longer(cols = dFe:L1_ALL_nM, names_to = "col_names", values_to = "val")
sd_df <- sd_df %>%
pivot_longer(cols = L1shipSD_nM:L1_ALL_SD_nM, names_to = "col_names", values_to = "sd") %>%
mutate(
# remove SD from string, we dont need it
col_names = str_replace_all(col_names, "SD", "")
)
# join the tables
plot_df <- main_df %>% full_join(sd_df)
# Plot our result
plot_df %>%
ggplot(
aes(y = Depth, x = val, color = col_names, shape = col_names, fill = col_names)
) +
geom_pointrange(
aes(xmin = val - sd, xmax = val + sd)
)