【问题标题】:How to graph two sets of data with lines and two *different* point symbols with *distinguishable* data point symbols in legend?如何用图例中的*可区分*数据点符号用线条和两个*不同*点符号绘制两组数据?
【发布时间】:2018-09-20 16:03:36
【问题描述】:

我一直在尝试使用Rggplot2 绘制具有不同 点符号 连接具有不同颜色的线的两组数据的图表,但是对于我的生活,我无法通过显示每条曲线的关联数据点符号来正确区分两条曲线。

我可以让图例显示不同的线条颜色。但我无法让图例为每组数据显示不同的数据点符号

以下代码:

df <- data.frame( thrd_cnt=c(1,2,4,8,16),
                 runtime4=c(53,38,31,41,54),
                 runtime8=c(54,35,31,35,44))

library("ggplot2")

print(
    ggplot(data = df, aes(df$thrd_cnt, y=df$runtime, color=)) +
    geom_line(aes(y=df$runtime4, color = "4 cores")) +
    geom_point(aes(y=df$runtime4, color = "4 cores"), fill = "white",
               size = 3, shape = 21) +
    geom_line(aes(y=df$runtime8, color = "8 cores")) +
    geom_point(aes(y=df$runtime8, color = "8 cores"), fill = "white",
               size = 3, shape = 23) +
    xlab("Number of Threads") +
    ylab(substitute(paste("Execution Time, ", italic(milisec)))) +
    scale_x_continuous(breaks=c(1,2,4,8,16)) +
    theme(legend.position = c(0.3, 0.8)) +
    labs(color="# cores")
)

## save a pdf and a png
ggsave("runtime.pdf", width=5, height=3.5)
ggsave("runtime.png", width=5, height=3.5)

输出此图:

plot

但是图例中的数据点符号是不可区分的。图例为两个图表显示了相同的符号(由两个数据点符号相互叠加)。

一种可能的解决方案是将线程数定义为factor,然后我可能能够正确获取图例上的数据点符号,但我仍然不知道该怎么做。

任何帮助将不胜感激。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    如上所述,您需要将gather 数据转换为长格式,以便将cores 变量映射到颜色和形状。要保持与原始绘图中相同的形状和填充选择,请使用scale_shape_manual 设置与cores 的每个级别对应的形状。请注意,您需要为labs() 中的colourshape 图例设置名称,以确保它们一致并且不会产生两个图例。我还使用了mutate,这样cores 的级别就不会混淆包含runtime 这个词。

    df <- data.frame( thrd_cnt=c(1,2,4,8,16),
                      runtime4=c(53,38,31,41,54),
                      runtime8=c(54,35,31,35,44))
    library(tidyverse)
    ggplot(
      data = df %>%
        gather(cores, runtime, runtime4, runtime8) %>%
        mutate(cores = str_c(str_extract(cores, "\\d"), " cores")),
      mapping = aes(x = thrd_cnt, y = runtime, colour = cores)
      ) +
      geom_line() +
      geom_point(aes(shape = cores), size = 3, fill = "white") +
      scale_x_continuous(breaks = c(1, 2, 4, 8, 16)) +
      scale_shape_manual(values = c("4 cores" = 21, "8 cores" = 23)) +
      theme(legend.position = c(0.3, 0.8)) +
      labs(
        x = "Number of Threads",
        y = "Execution Time (millisec)",
        colour = "# cores",
        shape = "# cores"
      )
    

    reprex package (v0.2.0) 于 2018 年 4 月 10 日创建。

    【讨论】:

    • 感谢您维护原始图形格式!
    【解决方案2】:

    shape 也可以,如果您正在使用 df 做更多的事情,那么将其转换并保持长而“整洁”的格式可能是有意义的。

       library("ggplot2")
       df <- data.frame( thrd_cnt=c(1,2,4,8,16),
                  runtime4=c(53,38,31,41,54),
                  runtime8=c(54,35,31,35,44))
    
       df <- df %>% gather("runtime", "millisec", 2:3)
    
       ggplot(data = df, aes(x = thrd_cnt, y = millisec, color = runtime, shape = 
       runtime)) + geom_line() + geom_point()
    

    【讨论】:

    • 仅当 ggplot2 被替换为 tidyverse 时才有效。谢谢!
    【解决方案3】:

    收集到“长”格式的数据框后,将颜色和形状 (pch) 传递给美学参数:

    library(tidyverse)
        df <- data.frame( thrd_cnt=c(1,2,4,8,16),
                          runtime4=c(53,38,31,41,54),
                          runtime8=c(54,35,31,35,44))
    
        df %>% gather(key=run, value=time, -thrd_cnt) %>%
          ggplot(aes(thrd_cnt, time, pch=run, colour=run)) + geom_line() + geom_point()
    

    (请注意代码与原始帖子相比有多简短)

    【讨论】:

    • 代码很简短,因为它没有保留任何格式选择,例如符号大小、符号形状、图例级别、图例位置和轴号。
    猜你喜欢
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    相关资源
    最近更新 更多