我下面有一个名为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")
答案 0 :(得分:3)
您可以设置一列以注意是否应突出显示观察值,并将其映射到大小,形状,线型等。
您可以使用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(v0.2.0)于2018-07-01创建。
答案 1 :(得分:1)
根据需要,可以保持或删除尺寸或线型,如下所示-
l <- ifelse(molten.data$key.related == 'G11F:G11M', 1, 2)
ltyp <- ifelse(molten.data$key.related == 'G11F:G11M', 'longdash', '“dotted”')
ggplot(data=molten.data, aes(x=variable, y=value, group= key.related, colour=key.related)) +
geom_line(aes( linetype = ltyp , size = l )) +
geom_point(aes( size = l ))