根据频率设置geom_line的厚度(如geom_count)

时间:2018-03-02 00:01:41

标签: r ggplot2

我想将geom_line的厚度设置为该路径后面的数据比例,就像geom_count根据重叠的数据比例设置点的大小一样那一点,或找到一个允许我这样做的功能。

如果我能把这个作为一个计数而不是一个比例,我也会很高兴 - 要么会奏效。我附上了图表,灰线表示相同ID之间的连接(即不同类别中的相同个体),如果我可以设置线条的粗细,我可以显示最常见的连接路径。

我目前的代码是:

ggplot(dat, aes(x = Category, y = Metric, group = ID)) +
  geom_line(aes(group = ID), colour = "gray59") + 
  geom_count(aes(size = ..prop.., group = 1), colour = "gray59") + 
  scale_size_area(max_size = 5) +
  theme_bw() + 
  geom_smooth(method = "lm", se = F, colour = "black", 
              aes(group = 1), linetype = "dotdash") +
  xlab("Category") + 
  ylab("Metric") + 
  theme(text = element_text(size = 16))

这是结果图,点大小显示在该点重叠的数据比例,如果可能,我想对线条粗细做同样的事情:

plot

我的搜索到目前为止没有任何帮助,但也许我正在搜索错误的条款。任何帮助将不胜感激!

以下是数据 - 不确定如何将其作为文件上传

dat <- structure(list(IDD = structure(c(1L, 1L, 1L, 1L, 3L, 3L, 4L, 
4L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 2L, 2L, 2L, 2L, 7L, 7L, 7L, 
8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 11L, 11L, 12L, 
12L, 13L, 13L, 13L, 13L, 14L, 14L, 15L, 15L, 15L, 15L, 16L, 16L, 
16L, 16L, 17L, 17L, 18L, 18L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 
21L, 21L, 22L, 22L, 23L, 23L, 24L, 24L, 25L, 25L, 25L, 26L, 26L, 
26L, 26L, 27L, 27L, 28L, 28L, 29L, 29L, 29L, 30L, 30L, 30L, 31L, 
31L, 31L, 31L, 32L, 32L, 33L, 33L, 33L, 34L, 34L, 34L, 34L, 35L, 
35L, 36L, 36L, 36L, 37L, 37L, 37L, 37L, 38L, 38L, 38L, 39L, 39L, 
39L, 40L, 40L, 40L, 41L, 41L, 42L, 42L, 43L, 43L, 44L, 44L, 44L, 
44L, 45L, 45L, 45L, 46L, 46L, 46L, 47L, 47L, 47L, 48L, 48L, 49L, 
49L, 50L, 50L, 51L, 51L, 51L, 51L, 52L, 52L, 53L, 53L, 54L, 54L, 
55L, 55L, 56L, 56L, 57L, 57L, 57L, 58L, 58L, 59L, 59L, 59L, 59L
), .Label = c("ID005", "ID040", "ID128", "ID131", "ID133", "ID134", 
"ID147", "ID149", "ID166", "ID167", "ID175", "ID181", "ID191", 
"ID198", "ID213", "ID235", "ID254", "ID257", "ID259", "ID273", 
"ID279", "ID287", "ID292", "ID299", "ID300", "ID321", "ID334", 
"ID348", "ID349", "ID354", "ID359", "ID377", "ID379", "ID383", 
"ID390", "ID395", "ID409", "ID445", "ID467", "ID469", "ID482", 
"ID492", "ID496", "ID524", "ID526", "ID527", "ID534", "ID535", 
"ID538", "ID545", "ID564", "ID576", "ID578", "ID579", "ID600", 
"ID610", "ID622", "ID631", "ID728"), class = "factor"), Category = c(2L, 
4L, 5L, 5L, 2L, 4L, 1L, 3L, 3L, 4L, 4L, 2L, 4L, 5L, 5L, 5L, 2L, 
5L, 5L, 5L, 3L, 2L, 5L, 4L, 5L, 5L, 4L, 4L, 5L, 5L, 3L, 4L, 5L, 
5L, 2L, 4L, 2L, 5L, 3L, 4L, 5L, 5L, 4L, 5L, 3L, 4L, 5L, 5L, 3L, 
4L, 5L, 5L, 5L, 5L, 2L, 3L, 4L, 4L, 5L, 5L, 5L, 5L, 4L, 4L, 5L, 
5L, 5L, 3L, 4L, 5L, 5L, 4L, 5L, 5L, 1L, 3L, 4L, 4L, 3L, 5L, 3L, 
5L, 2L, 3L, 4L, 3L, 4L, 4L, 3L, 3L, 4L, 4L, 3L, 5L, 3L, 4L, 4L, 
3L, 3L, 4L, 5L, 2L, 3L, 2L, 3L, 4L, 2L, 2L, 3L, 4L, 4L, 5L, 5L, 
2L, 3L, 4L, 2L, 3L, 4L, 3L, 4L, 4L, 5L, 3L, 4L, 1L, 2L, 3L, 4L, 
1L, 3L, 4L, 1L, 3L, 4L, 1L, 3L, 4L, 3L, 4L, 3L, 3L, 2L, 3L, 2L, 
2L, 3L, 3L, 2L, 3L, 2L, 3L, 3L, 4L, 3L, 4L, 3L, 4L, 1L, 2L, 3L, 
2L, 3L, 1L, 3L, 4L, 4L), Metric = c(2, 2, 3.5, 4, 2, 1.5, 2, 
2, 3, 3, 2, 2, 2, 2, 3.5, 3.5, 2, 3, 3.5, 4, 2, 2, 3, 2, 3, 3, 
2, 3, 3, 2.5, 1.5, 3, 3.5, 4, 2, 2, 1.5, 2, 1.5, 2, 2, 2, 2.5, 
3, 2.5, 3.5, 3.5, 3.5, 1.5, 2, 2.5, 2.5, 3.5, 4, 2, 2, 1.5, 3, 
3.5, 3, 3, 3, 3.5, 2.5, 3, 3, 3, 2, 3, 2.5, 2.5, 2, 2, 2, 2, 
2, 2, 2, 2.5, 2.5, 2, 3, 2.5, 2, 2.5, 2, 2.5, 2.5, 2, 2, 2.5, 
3.5, 2, 2.5, 2.5, 2.5, 2.5, 2, 2, 2, 2.5, 2, 2, 1.5, 2, 2, 2.5, 
2, 2, 2.5, 2, 2, 2.5, 2.5, 2.5, 3, 2.5, 2.5, 2.5, 2, 2, 2.5, 
2.5, 2, 2, 2, 2, 1.5, 2, 1.5, 2, 2, 2, 1.5, 2, 2, 2.5, 2.5, 1.5, 
1.5, 2, 2.5, 2, 2, 2, 2, 2.5, 2, 1.5, 2, 2.5, 2, 1.5, 1.5, 1.5, 
2, 2, 2, 2, 2, 1.5, 2, 2.5, 2, 2, 2.5, 2.5)), .Names = c("IDD", 
"Category", "Metric"), class = "data.frame", row.names = c(NA, 
-167L))

1 个答案:

答案 0 :(得分:0)

我对你想如何缩放不同的线段感到有点困惑,但我能够在dat中创建一个比例变量,然后将其作为geom_line()的参数绘制:

dat$thickness <- with(dat, ave(Category, Metric, FUN = prop.table))

ggplot(dat, aes(x = Category, y = Metric, group = ID)) +
  geom_line(aes(group = ID), colour = "gray59", size = dat$thickness) + 
  geom_count(aes(size = ..prop.., group = 1), colour = "gray59") + 
  scale_size_area(max_size = 5) +
  theme_bw() + 
  geom_smooth(method = "lm", se = F, colour = "black", 
              aes(group = 1), linetype = "dotdash") +
  xlab("Category") + 
  ylab("Metric") + 
  theme(text = element_text(size = 16))

这产生了这个情节:

enter image description here