【问题标题】:R: ggplot2 - Manually set point shape, line type, and colour according to labelR: ggplot2 - 根据标签手动设置点形状、线型和颜色
【发布时间】:2019-10-30 18:09:15
【问题描述】:

我正在尝试使用手动设置颜色和手动设置点和线的形状来制作绘图。我正在使用的数据框具有以下格式:

"n" "times"     "algorithms"    "shapes"    "linetypes"     "colours"
1   0.000271833 "algo1"         "x"         "solid"         "blue"
11  0.000612195 "algo1"         "x"         "solid"         "blue"
1   0.000267802 "algo2"         "x"         "solid"         "red"
11  0.000644297 "algo2"         "x"         "solid"         "red"
1   0.000280468 "algo3"         "x"         "solid"         "green"
11  0.000816817 "algo3"         "x"         "solid"         "green"
1   0.000452015 "algo4"         "x"         "solid"         "black"
11  0.00271677  "algo4"         "x"         "solid"         "black"
1   0.000271255 "algo5"         "o"         "dashed"        "blue"
11  0.000622194 "algo5"         "o"         "dashed"        "blue"
1   0.000271107 "algo6"         "o"         "dashed"        "red"
11  0.000701686 "algo6"         "o"         "dashed"        "red"
1   0.000267631 "algo7"         "o"         "dashed"        "green"
11  0.000723341 "algo7"         "o"         "dashed"        "green"
1   0.000451016 "algo8"         "o"         "dashed"        "black"
11  0.00124079  "algo8"         "o"         "dashed"        "black"

此数据帧包含 8 种不同算法的执行时间。我想实现以下目标:我想为算法alg1alg4 绘制带有十字的点,并为算法alg5alg8 绘制点(如果可能的话)。以类似的方式,我想要连接点的线,以便绘制的线显示每个算法的执行时间的增长,并且我希望一些线是实线,而另一些线​​是虚线。我还想为每种算法使用特定的颜色,如“颜色”列中所示。

现在,我已经取得了相当大的进步:

g <- ggplot(df, aes(x=n, y=times, group=algorithms, linetype=linetypes, pch=shapes, colour=colours)) +
    geom_line() +
    geom_point() +
    theme(legend.position="bottom") +
    xlab("n") +
    ylab(paste0("Execution time (ms)")) +
    ggtitle("asdf")

但这给了我一个最令人不快的传说:

我想要一个图例,在算法名称旁边显示它们对应的线和点类型,并且显然是颜色。

我浏览了这个站点和其他站点,但无济于事。这确实很困难,因为大多数人不会超出手动设置颜色、点的形状和线型的范围。我只需要知道如何做这个额外的步骤。我确信这很容易做到,但我完全不知所措。

谢谢大家。

【问题讨论】:

  • 确实,@dipetkov 做到了。但是,点类型需要更多调整。 “x”和“o”不再适用于他们的解决方案,所以我将它们更改为 4 和 1(“x”->4,“o”->1,参见 sthda.com/english/wiki/ggplot2-point-shapes),现在一切很好。非常感谢。

标签: r ggplot2 legend


【解决方案1】:

您可以手动指定颜色/线型/形状比例。


library("tidyverse")

df <- tibble::tribble(
  ~n, ~times, ~algorithms, ~shapes, ~linetypes, ~colours,
  1L, 0.000271833, "algo1", "x", "solid", "blue",
  11L, 0.000612195, "algo1", "x", "solid", "blue",
  1L, 0.000267802, "algo2", "x", "solid", "red",
  11L, 0.000644297, "algo2", "x", "solid", "red",
  1L, 0.000280468, "algo3", "x", "solid", "green",
  11L, 0.000816817, "algo3", "x", "solid", "green",
  1L, 0.000452015, "algo4", "x", "solid", "black",
  11L, 0.00271677, "algo4", "x", "solid", "black",
  1L, 0.000271255, "algo5", "o", "dashed", "blue",
  11L, 0.000622194, "algo5", "o", "dashed", "blue",
  1L, 0.000271107, "algo6", "o", "dashed", "red",
  11L, 0.000701686, "algo6", "o", "dashed", "red",
  1L, 0.000267631, "algo7", "o", "dashed", "green",
  11L, 0.000723341, "algo7", "o", "dashed", "green",
  1L, 0.000451016, "algo8", "o", "dashed", "black",
  11L, 0.00124079, "algo8", "o", "dashed", "black"
)

df %>%
  ggplot(aes(
    x = n, y = times,
    linetype = algorithms,
    shape = algorithms,
    colour = algorithms
  )) +
  geom_line() +
  # Comment out `geom_point` to check that the line type is 
  # as specified but is overplotted by the shape in the legend
  geom_point(size = 4) +
  xlab("n") +
  ylab(paste0("Execution time (ms)")) +
  ggtitle("asdf") +
  scale_color_manual(
    values = deframe(df %>% select(algorithms, colours))
  ) +
  scale_linetype_manual(
    values = deframe(df %>% select(algorithms, linetypes))
  ) +
  scale_shape_manual(
    values = deframe(df %>% select(algorithms, shapes))
  ) +
  theme(legend.position = "bottom")

reprex package (v0.3.0) 于 2019 年 10 月 30 日创建

【讨论】:

    猜你喜欢
    • 2021-09-05
    • 2013-06-15
    • 1970-01-01
    • 2014-06-14
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 1970-01-01
    • 2022-09-27
    相关资源
    最近更新 更多