使用 R 中的 ggplot 从两个数据帧绘制箱线图和带有双 y 轴的线
Boxplot and line with dual y-axis from two data frame using ggplot in R
我正在使用 ggplot 将箱线图和线放在同一个图中。我有两个数据框,这里是这两个 DF 的片段:
TMA.core variable value
1 I-5 H&E 356642.6
2 B-1 H&E 490276.9
3 B-13 H&E 460831.8
4 L-11 H&E 551614.2
5 B-6 H&E 663711.8
6 F-10 H&E 596832.8
(变数很多。)
TMA.core Mean CoV
I-5 390829.7 0.15181577
B-1 414909.9 0.21738852
B-13 500829.8 0.39049256
L-11 537229.7 0.07387486
B-6 575698.9 0.44764127
F-10 589245.2 0.15382864
我想做的是使用第一个数据框绘制箱线图,然后为相应的 TMA 核心绘制 CoV,并使用 geom_line 连接。
我的代码是:
ggplot() +
geom_boxplot(data = Merge_stats_melt, aes(x = reorder(TMA.core, value, FUN = mean), y = value)) +
geom_line(data = Merge_stas_mean_order, aes(x = reorder(TMA.core, Mean), y = CoV, group = 1)) +
scale_y_continuous(
# Add a second axis and specify its features
sec.axis = sec_axis(~./1000000, name = 'CoV')
)
使用这些代码我可以绘制箱线图,但线始终是 y = 0 处的水平线。
如何解决这个问题?
使用一个或两个数据框并不重要。只记得相应地调整 y 美学,你忘记了。
library(ggplot2)
library(scales)
找到双轴的理想比例因子
ratio <- max(Merge_stats_melt$value) / max(Merge_stas_mean_order$CoV)
ggplot() +
geom_boxplot(data = Merge_stats_melt, aes(x = reorder(TMA.core, value, FUN = mean), y = value)) +
geom_line(data = Merge_stas_mean_order, aes(x = reorder(TMA.core, Mean), y = CoV*ratio, group = 1)) +
scale_y_continuous(labels=comma,
sec.axis = sec_axis(~./ratio, name = 'CoV')
)
我正在使用 ggplot 将箱线图和线放在同一个图中。我有两个数据框,这里是这两个 DF 的片段:
TMA.core variable value
1 I-5 H&E 356642.6
2 B-1 H&E 490276.9
3 B-13 H&E 460831.8
4 L-11 H&E 551614.2
5 B-6 H&E 663711.8
6 F-10 H&E 596832.8
(变数很多。)
TMA.core Mean CoV
I-5 390829.7 0.15181577
B-1 414909.9 0.21738852
B-13 500829.8 0.39049256
L-11 537229.7 0.07387486
B-6 575698.9 0.44764127
F-10 589245.2 0.15382864
我想做的是使用第一个数据框绘制箱线图,然后为相应的 TMA 核心绘制 CoV,并使用 geom_line 连接。 我的代码是:
ggplot() +
geom_boxplot(data = Merge_stats_melt, aes(x = reorder(TMA.core, value, FUN = mean), y = value)) +
geom_line(data = Merge_stas_mean_order, aes(x = reorder(TMA.core, Mean), y = CoV, group = 1)) +
scale_y_continuous(
# Add a second axis and specify its features
sec.axis = sec_axis(~./1000000, name = 'CoV')
)
使用这些代码我可以绘制箱线图,但线始终是 y = 0 处的水平线。
如何解决这个问题?
使用一个或两个数据框并不重要。只记得相应地调整 y 美学,你忘记了。
library(ggplot2)
library(scales)
找到双轴的理想比例因子
ratio <- max(Merge_stats_melt$value) / max(Merge_stas_mean_order$CoV)
ggplot() +
geom_boxplot(data = Merge_stats_melt, aes(x = reorder(TMA.core, value, FUN = mean), y = value)) +
geom_line(data = Merge_stas_mean_order, aes(x = reorder(TMA.core, Mean), y = CoV*ratio, group = 1)) +
scale_y_continuous(labels=comma,
sec.axis = sec_axis(~./ratio, name = 'CoV')
)