gpt4 book ai didi

r - 如何使用 ggplot 突出显示更改线型或大小的线的特定区域

转载 作者:行者123 更新时间:2023-12-02 18:16:44 25 4
gpt4 key购买 nike

我有以下数据框:

test = structure(list(Student = c("Ana", "Brenda", "Max", "Ana", "Brenda", 
"Max", "Ana", "Brenda", "Max"), Month = structure(c(1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 3L), .Label = c("January", "February", "March"
), class = "factor"), Grade = c(7L, 6L, 7L, 8L, 6L, 7L, 5L, 10L,
10L), Change = c("February", "February", "February", "February",
"February", "February", "February", "February", "February")), row.names = c(NA,
-9L), class = "data.frame")

我绘制了每个学生几个月的成绩,并想知道是否有一种简单的方法来强调每条绘制线的特定部分,该部分对应于成绩变化最大的时间段(此信息已存在于数据集中:“更改”列。在本例中,对于所有学生来说,时间范围为二月到三月。

尝试使用类似的逻辑 here, to change the color a specific part of the line我尝试更改线条的线型和/或大小(因为我已经使用颜色来分组和显示每个学生),以突出显示线条的特定部分。然而,事情似乎并不那么简单。

我的尝试如下:

ggplot(test, aes(x = Month, y = Grade, color = Student, group = Student)) + 
+ geom_point() + geom_line(data = test, aes(linetype = (ifelse(Month == Change, "solid", "dashed"))))

这产生了错误:

Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line

还有

ggplot(test, aes(x = Month, y = Grade, color = Student, group = Student)) + 
geom_point() + geom_line(data = test, aes(size = (ifelse(Month == Change, 1, 0.8))))

这有点像我正在寻找的,但看起来很糟糕,而且看起来并不像它使用我试图指定的行的大小:

This is how it looks

如何修复它?提前致谢! n_n

最佳答案

不要指定 aes 格式的大小。使用缩放功能

有关生产线设计的其他建议,请参见下文。

test = structure(list(Student = c("Ana", "Brenda", "Max", "Ana", "Brenda", 
"Max", "Ana", "Brenda", "Max"), Month = structure(c(1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 3L), .Label = c("January", "February", "March"
), class = "factor"), Grade = c(7L, 6L, 7L, 8L, 6L, 7L, 5L, 10L,
10L), Change = c("Februrary", "Februrary", "Februrary", "Februrary",
"Februrary", "Februrary", "Februrary", "Februrary", "Februrary"
)), row.names = c(NA, -9L), class = "data.frame")
## typo corrected
test$Change <- "February"

library(ggplot2)

ggplot(test, aes(x = Month, y = Grade, color = Student, group = Student)) +
geom_point() +
## don't specify size in aes
geom_line(data = test, aes(size = Month == Change)) +
## change the size scale
scale_size_manual(values = c(`TRUE` = 2, `FALSE` = .8))

另一种选择可能是使用ggforce::geom_link函数,它可以在两点之间插入美学。

library(ggforce)

ggplot(test, aes(x = Month, y = Grade, color = Student, group = Student)) +
geom_point() +
geom_link2(data = test, aes(size = Grade, ), lineend = "round")

尝试更改线路类型时失败。在这种情况下,请使用 geom_segment - 不过您将需要一些数据转换。

library(tidyverse)

test %>%
group_by(Student) %>%
mutate(xend = lead(Month), yend = lead(Grade)) %>%
ggplot(aes(x = Month, y = Grade, color = Student, group = Student)) +
geom_point() +
geom_segment(aes(xend = xend, yend = yend, linetype = Month == Change)) +
# need to specify your x
scale_x_discrete(limits = unique(test$Month))
#> Warning: Removed 3 rows containing missing values (geom_segment).

关于r - 如何使用 ggplot 突出显示更改线型或大小的线的特定区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71486356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com