gpt4 book ai didi

r - 如何更改特定线条的粗细和/或在多线图中添加形状?

转载 作者:行者123 更新时间:2023-12-02 03:33:32 29 4
gpt4 key购买 nike

下面有一个名为 molten.data 的数据。我有这段代码可以绘制我想要的线条,但我需要更改一条特定线条 (G11F:G11M) 的粗细,使其看起来比其他线条更粗,或者最好为数据添加形状在那条线上的点。我们怎样才能做到呢?

我有代码:

ggplot(molten.data, aes(variable, value,group= key.related,colour=key.related)) + 
geom_line() +
geom_point()

数据:

molten.data<- structure(list(key.related = c("G11F:G11F", "G11F:G11F", "G11F:G11F", 
"G11F:G11M", "G11F:G11F", "G11F:G11M", "G11F:AOGC-02-0079", "G11F:G11F",
"G11F:G11M"), variable = structure(c(1L, 2L, 3L, 3L, 4L, 4L,
4L, 5L, 5L), .Label = c("IBS_2_samples", "IBS_4_samples", "IBS_8_samples",
"IBS_16_samples", "IBS_32_samples"), class = "factor"), value = c(0.533,
1.01, 1.11, 0.132, 1.22, 0.353, 0.0658, 1.33, 0.534)), .Names = c("key.related",
"variable", "value"), row.names = c(1L, 82L, 163L, 168L, 244L,
249L, 260L, 325L, 330L), class = "data.frame")

最佳答案

您可以设置一列来注明是否应突出显示观察结果,并将其映射到大小、形状、线型等。

您可以使用ifelse来做到这一点:

library(tidyverse)

molten.data %>%
mutate(hilite = ifelse(key.related == "G11F:G11M", 2, 1))
#> key.related variable value hilite
#> 1 G11F:G11F IBS_2_samples 0.5330 1
#> 2 G11F:G11F IBS_4_samples 1.0100 1
#> 3 G11F:G11F IBS_8_samples 1.1100 1
#> 4 G11F:G11M IBS_8_samples 0.1320 2
#> 5 G11F:G11F IBS_16_samples 1.2200 1
#> 6 G11F:G11M IBS_16_samples 0.3530 2
#> 7 G11F:AOGC-02-0079 IBS_16_samples 0.0658 1
#> 8 G11F:G11F IBS_32_samples 1.3300 1
#> 9 G11F:G11M IBS_32_samples 0.5340 2

我的偏好是根据因素来进行。 forcats::fct_other 允许您选择要保留的级别,并将其他所有内容标记为“其他”。想象一下,您有几个键想要保留其原始名称,还有很多键想要归入“其他”类别 - 随着数据变得更加复杂,它变得非常有用。

然后我将其输入到 ggplot 中,使用 hilite 来指定突出显示。一种方法是尺寸;您可以调整尺寸以使其更加引人注目,或者获得任何合适的尺寸:

molten.data %>%
mutate(hilite = as.factor(key.related) %>% fct_other(keep = c("G11F:G11M"), other_level = "Other keys")) %>%
ggplot(aes(x = variable, y = value, color = key.related, group = key.related, size = hilite)) +
geom_line() +
geom_point(size = 2) +
scale_size_manual(values = c("G11F:G11M" = 1.5, "Other keys" = 0.5), guide = F)

或者保持尺寸统一但改变形状。在这里,我设置了形状值,以便非突出显示的点获得正常的圆形点,突出显示的点获得三角形。

molten.data %>%
mutate(hilite = as.factor(key.related) %>% fct_other(keep = c("G11F:G11M"), other_level = "Other keys")) %>%
ggplot(aes(x = variable, y = value, color = key.related, group = key.related)) +
geom_line() +
geom_point(aes(shape = hilite), size = 2) +
scale_shape_manual(values = c("G11F:G11M" = 17, "Other keys" = 16), guide = F)

reprex package于2018年7月1日创建(v0.2.0)。

关于r - 如何更改特定线条的粗细和/或在多线图中添加形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51119948/

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