将文本标签添加到来自不同数据集的折线图时出现 geom_text_repel 错误
Error with geom_text_repel when adding text labels to line graph from a different data set
免责声明:我在另一个 中发现了与此问题类似的问题,但解决方案并不是我所需要的。
我有一个数据集,TGA,其中包含一些涉及不同处理的时间和温度序列。
> TGA
# A tibble: 16,662 x 4
`t [s]` `Ts [°C]` `Value [mg]` Treatment
<int> <chr> <chr> <chr>
1 0 28.686 68.9369 C_Water_Air,
2 1 28.657 68.9368 C_Water_Air,
3 2 28.688 68.937 C_Water_Air,
4 3 28.751 68.9373 C_Water_Air,
5 4 28.939 68.9377 C_Water_Air,
6 5 29.123 68.9378 C_Water_Air,
7 6 29.324 68.9381 C_Water_Air,
8 7 29.51 68.9386 C_Water_Air,
9 8 29.721 68.9379 C_Water_Air,
10 9 29.922 68.9341 C_Water_Air,
# ... with 16,652 more rows
然后我将此数据绘制为 geom_path
,我从另一个数据集添加一些文本标签,该数据集是根据 TGA 数据集计算的,称为 decar_cotton_air.
> decar_cotton_Air
Groups: Treatment [6]
`t [s]` `Ts [°C]` `Value [mg]` Treatment round_temp weight_difference reaction
<int> <dbl> <dbl> <chr> <dbl> <dbl> <chr>
1 2629 900. 65.7 C_Water_Air, 900 1.16 Decarbonation
2 2629 900. 45.2 C_TSB_Air, 900 1.57 Decarbonation
3 2630 900. 83.1 C_Sp1_Air, 900 0.972 Decarbonation
4 2630 900. 84.8 C_Sh1_Air, 900 0.763 Decarbonation
5 2629 900. 73.2 C_Positive_Air, 900 1.14 Decarbonation
6 2630 900. 76.7 C_Open_Air, 900 3.90 Decarbonation
基本上,我正在使用第二个数据集来标记我的图表,其中包含治疗名称和一个值,该值是从 x = 600 到 x = 900 的权重差异。到目前为止没问题。
ggplot(TGA, aes(`Ts [°C]`, `Value [mg]`, group = Treatment)) +
geom_path(aes(color = Treatment)) +
labs(x = "Temperature [°C]", y = "Change in mass [mg]", title = "Thermogravimetric curve (TGA)", subtitle = "of lime mortar carbonated with Cotton") +
coord_cartesian(xlim = c(24, 950), ylim = c(45, 90)) +
theme(legend.position = "none") +
geom_vline(xintercept = c(300,600,900), linetype = 3) +
annotate("text", x = 450, y = 90, label = "dehydroxylation") +
annotate("text", x = 750, y = 90, label = "decarbonation") +
geom_text_repel(aes(colour = Treatment), data= decar_cotton_Air, label = decar_cotton_Air$weight_difference, x = 750) +
geom_text_repel(aes(colour = Treatment), data= decar_cotton_Air, label = decar_cotton_Air$Treatment, x = 100, nudge_y = 3)
现在的问题是,当我尝试 运行 具有不同数据集的相同代码时,出现以下代码错误:
Error in FUN(X[[i]], ...) : object 'mean_weight_loss' not found
新数据集如下:
> TGA_averages
# A tibble: 16,662 x 3
# Groups: t [s] [2,777]
`t [s]` Treatment mean_weight_loss
<int> <chr> <dbl>
1 0 C_open 100
2 0 C_positive 100
3 0 C_sh1 100.
4 0 C_sp1 100
5 0 C_tsb 100
6 0 C_water 100
7 1 C_open 100
8 1 C_positive 100.0
9 1 C_sh1 100.
10 1 C_sp1 100.0
# ... with 16,652 more rows
在这段代码中,我清理了治疗标签,mean_weight_loss
是对 TGA$Value [mg]
变量百分比的转换。
新情节的代码如下。我更改了 aes 以适应新数据集,其中 x 是时间而不是温度,y 是 mean_weight_loss 变量。
ggplot(TGA_averages, aes(`t [s]`, mean_weight_loss, group = Treatment)) +
geom_path(aes(color = Treatment)) +
labs(x = "Time [s]", y = "Percentage change of mass [%]", title = "Thermogravimetric curve (TGA)", subtitle = "of lime mortars with Cotton") +
coord_cartesian(xlim = c(0, 3200), ylim = c(93.88025, 100)) +
theme(legend.position = "none") +
geom_vline(xintercept = c(300,600,900), linetype = 3) +
annotate("text", x = 1200, y = 100, label = "dehydroxylation") +
annotate("text", x = 2100, y = 100, label = "decarbonation") +
geom_text_repel(aes(colour = Treatment), data= decar_cotton_Air, label = decar_cotton_Air$weight_difference, x = 750)
我不知道这与我以前的代码有何不同,但我到处搜索,但终究无法解决这个问题。我知道它是 geom_text_repel
,因为 运行 没有它也很好。注意:在这张图片中,我使用 geom_dl(aes(label = Treatment, color = Treatment), method = list(dl.combine("last.qp")))
插入标签,但它并没有给我很好的结果,因为我希望它们在图表中。
问题是 geom_text_repel()
需要 x
和 y
审美。这在第一个有效的示例中不是问题,因为 y
美学被映射到 Value [mg]
,并且该列同时存在于 TGA
和 decar_cotton_Air
.
在第二个示例中,您使用 TGA_averages
和映射 y = mean_weight_loss
进行绘图。由于在这种情况下 geom_text_repel()
设置为查看 decar_cotton_Air
,因此它会期望在 x
和 y
映射中指定一列...在此在这种情况下,它期望指定 x 和 y 列。
解决方案是将 decar_cotton_Air
中的列重命名为 mean_weight_loss
,或者在每个 geom 中单独指定 y
映射,而不是在绘图中整体指定。这里有一些伪代码可以给你一个想法:
ggplot(TGA_averages, aes(x = `t [s]`, group = Treatment)) + # only x and group aes
# specify y aesthetic here
geom_path(aes(y = mean_weight_loss, color = Treatment)) +
labs(x = "Time [s]", y = "Percentage change of mass [%]", title = "Thermogravimetric curve (TGA)", subtitle = "of lime mortars with Cotton") +
coord_cartesian(xlim = c(0, 3200), ylim = c(93.88025, 100)) +
theme(legend.position = "none") +
geom_vline(xintercept = c(300,600,900), linetype = 3) +
annotate("text", x = 1200, y = 100, label = "dehydroxylation") +
annotate("text", x = 2100, y = 100, label = "decarbonation") +
# specify a different y aes here in decar_cotton_Air
geom_text_repel(
aes(colour = Treatment, y = `Value [mg]`),
data= decar_cotton_Air,
label = decar_cotton_Air$weight_difference, x = 750)
如果您需要标签的位置与基础数据相匹配,我建议您引用 decar_cotton_Air
以外的其他数据框,或者合并两个数据集以确保您拥有共同的列。
免责声明:我在另一个
我有一个数据集,TGA,其中包含一些涉及不同处理的时间和温度序列。
> TGA
# A tibble: 16,662 x 4
`t [s]` `Ts [°C]` `Value [mg]` Treatment
<int> <chr> <chr> <chr>
1 0 28.686 68.9369 C_Water_Air,
2 1 28.657 68.9368 C_Water_Air,
3 2 28.688 68.937 C_Water_Air,
4 3 28.751 68.9373 C_Water_Air,
5 4 28.939 68.9377 C_Water_Air,
6 5 29.123 68.9378 C_Water_Air,
7 6 29.324 68.9381 C_Water_Air,
8 7 29.51 68.9386 C_Water_Air,
9 8 29.721 68.9379 C_Water_Air,
10 9 29.922 68.9341 C_Water_Air,
# ... with 16,652 more rows
然后我将此数据绘制为 geom_path
,我从另一个数据集添加一些文本标签,该数据集是根据 TGA 数据集计算的,称为 decar_cotton_air.
> decar_cotton_Air
Groups: Treatment [6]
`t [s]` `Ts [°C]` `Value [mg]` Treatment round_temp weight_difference reaction
<int> <dbl> <dbl> <chr> <dbl> <dbl> <chr>
1 2629 900. 65.7 C_Water_Air, 900 1.16 Decarbonation
2 2629 900. 45.2 C_TSB_Air, 900 1.57 Decarbonation
3 2630 900. 83.1 C_Sp1_Air, 900 0.972 Decarbonation
4 2630 900. 84.8 C_Sh1_Air, 900 0.763 Decarbonation
5 2629 900. 73.2 C_Positive_Air, 900 1.14 Decarbonation
6 2630 900. 76.7 C_Open_Air, 900 3.90 Decarbonation
基本上,我正在使用第二个数据集来标记我的图表,其中包含治疗名称和一个值,该值是从 x = 600 到 x = 900 的权重差异。到目前为止没问题。
ggplot(TGA, aes(`Ts [°C]`, `Value [mg]`, group = Treatment)) +
geom_path(aes(color = Treatment)) +
labs(x = "Temperature [°C]", y = "Change in mass [mg]", title = "Thermogravimetric curve (TGA)", subtitle = "of lime mortar carbonated with Cotton") +
coord_cartesian(xlim = c(24, 950), ylim = c(45, 90)) +
theme(legend.position = "none") +
geom_vline(xintercept = c(300,600,900), linetype = 3) +
annotate("text", x = 450, y = 90, label = "dehydroxylation") +
annotate("text", x = 750, y = 90, label = "decarbonation") +
geom_text_repel(aes(colour = Treatment), data= decar_cotton_Air, label = decar_cotton_Air$weight_difference, x = 750) +
geom_text_repel(aes(colour = Treatment), data= decar_cotton_Air, label = decar_cotton_Air$Treatment, x = 100, nudge_y = 3)
现在的问题是,当我尝试 运行 具有不同数据集的相同代码时,出现以下代码错误:
Error in FUN(X[[i]], ...) : object 'mean_weight_loss' not found
新数据集如下:
> TGA_averages
# A tibble: 16,662 x 3
# Groups: t [s] [2,777]
`t [s]` Treatment mean_weight_loss
<int> <chr> <dbl>
1 0 C_open 100
2 0 C_positive 100
3 0 C_sh1 100.
4 0 C_sp1 100
5 0 C_tsb 100
6 0 C_water 100
7 1 C_open 100
8 1 C_positive 100.0
9 1 C_sh1 100.
10 1 C_sp1 100.0
# ... with 16,652 more rows
在这段代码中,我清理了治疗标签,mean_weight_loss
是对 TGA$Value [mg]
变量百分比的转换。
新情节的代码如下。我更改了 aes 以适应新数据集,其中 x 是时间而不是温度,y 是 mean_weight_loss 变量。
ggplot(TGA_averages, aes(`t [s]`, mean_weight_loss, group = Treatment)) +
geom_path(aes(color = Treatment)) +
labs(x = "Time [s]", y = "Percentage change of mass [%]", title = "Thermogravimetric curve (TGA)", subtitle = "of lime mortars with Cotton") +
coord_cartesian(xlim = c(0, 3200), ylim = c(93.88025, 100)) +
theme(legend.position = "none") +
geom_vline(xintercept = c(300,600,900), linetype = 3) +
annotate("text", x = 1200, y = 100, label = "dehydroxylation") +
annotate("text", x = 2100, y = 100, label = "decarbonation") +
geom_text_repel(aes(colour = Treatment), data= decar_cotton_Air, label = decar_cotton_Air$weight_difference, x = 750)
我不知道这与我以前的代码有何不同,但我到处搜索,但终究无法解决这个问题。我知道它是 geom_text_repel
,因为 运行 没有它也很好。注意:在这张图片中,我使用 geom_dl(aes(label = Treatment, color = Treatment), method = list(dl.combine("last.qp")))
插入标签,但它并没有给我很好的结果,因为我希望它们在图表中。
问题是 geom_text_repel()
需要 x
和 y
审美。这在第一个有效的示例中不是问题,因为 y
美学被映射到 Value [mg]
,并且该列同时存在于 TGA
和 decar_cotton_Air
.
在第二个示例中,您使用 TGA_averages
和映射 y = mean_weight_loss
进行绘图。由于在这种情况下 geom_text_repel()
设置为查看 decar_cotton_Air
,因此它会期望在 x
和 y
映射中指定一列...在此在这种情况下,它期望指定 x 和 y 列。
解决方案是将 decar_cotton_Air
中的列重命名为 mean_weight_loss
,或者在每个 geom 中单独指定 y
映射,而不是在绘图中整体指定。这里有一些伪代码可以给你一个想法:
ggplot(TGA_averages, aes(x = `t [s]`, group = Treatment)) + # only x and group aes
# specify y aesthetic here
geom_path(aes(y = mean_weight_loss, color = Treatment)) +
labs(x = "Time [s]", y = "Percentage change of mass [%]", title = "Thermogravimetric curve (TGA)", subtitle = "of lime mortars with Cotton") +
coord_cartesian(xlim = c(0, 3200), ylim = c(93.88025, 100)) +
theme(legend.position = "none") +
geom_vline(xintercept = c(300,600,900), linetype = 3) +
annotate("text", x = 1200, y = 100, label = "dehydroxylation") +
annotate("text", x = 2100, y = 100, label = "decarbonation") +
# specify a different y aes here in decar_cotton_Air
geom_text_repel(
aes(colour = Treatment, y = `Value [mg]`),
data= decar_cotton_Air,
label = decar_cotton_Air$weight_difference, x = 750)
如果您需要标签的位置与基础数据相匹配,我建议您引用 decar_cotton_Air
以外的其他数据框,或者合并两个数据集以确保您拥有共同的列。