在 ggplot 中编辑图例标签

发布于 2024-12-11 23:46:56 字数 836 浏览 0 评论 0原文

我有一个看起来像这样的数据框:

data
    median  min max no_of_threads
    2.33    2.10    6.85    1
    2.43    2.14    3.41    2
    2.39    2.13    7.90    3
    2.74    2.10    8.30    4
    2.53    2.21    6.69    5

我制作了这个 R 函数,它将 data$min 和 data$max 绘制为范围,将 data$median 绘制为行:

scalability_graph <- function(data){ 
  h <- ggplot(data)
  h <- h + 
      geom_ribbon(aes(x = no_of_threads, ymin = min, ymax = max)) +
      geom_line(aes(x = no_of_threads, y=median, color="#CC873D")) +
      scale_x_continuous("Number of threads", lim=c(0,20)) +
      scale_y_continuous("Response time", lim=c(0,13)) +
      opts(legend.position=c(0.20,0.90))
}

脚本生成此图:

带中位数的范围图

如何更改图例中的标签并用“范围”代替顶部粗体字符串,用“中位数”代替底部标签?

I have a dataframe that looks like this:

data
    median  min max no_of_threads
    2.33    2.10    6.85    1
    2.43    2.14    3.41    2
    2.39    2.13    7.90    3
    2.74    2.10    8.30    4
    2.53    2.21    6.69    5

I made this R function, that plots data$min and data$max as range, and data$median as line:

scalability_graph <- function(data){ 
  h <- ggplot(data)
  h <- h + 
      geom_ribbon(aes(x = no_of_threads, ymin = min, ymax = max)) +
      geom_line(aes(x = no_of_threads, y=median, color="#CC873D")) +
      scale_x_continuous("Number of threads", lim=c(0,20)) +
      scale_y_continuous("Response time", lim=c(0,13)) +
      opts(legend.position=c(0.20,0.90))
}

The script produces this plot:

range graph with median

How to change labels in legend and put "range" instead of top bold string, and "median" instead of bottom label?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虐人心 2024-12-18 23:46:56

您可以通过添加以下内容来做到这一点:

 + scale_colour_manual("range", labels = "median", values = "#CC873D", breaks = "#CC873D") 

在此处输入图像描述

但您的情节和图例应该是这样的?

h <- ggplot(data, aes(no_of_threads, median)) +
  geom_ribbon(aes(ymin = min, ymax = max, fill = factor(1))) +
  geom_line(aes(color=factor(1))) +
  scale_x_continuous("Number of threads", lim=c(0,20)) +
  scale_y_continuous("Response time", lim=c(0,13)) +
  scale_fill_manual(breaks = 1, values = "grey20", labels = "range") + 
  scale_colour_manual(breaks = 1, values = "#CC873D", labels = "median") +
  opts(legend.position=c(0.20,0.85), legend.title = theme_blank(), legend.background = theme_blank())

在此处输入图像描述

you can do that by adding this one:

 + scale_colour_manual("range", labels = "median", values = "#CC873D", breaks = "#CC873D") 

enter image description here

but your plot and legend should be like this?

h <- ggplot(data, aes(no_of_threads, median)) +
  geom_ribbon(aes(ymin = min, ymax = max, fill = factor(1))) +
  geom_line(aes(color=factor(1))) +
  scale_x_continuous("Number of threads", lim=c(0,20)) +
  scale_y_continuous("Response time", lim=c(0,13)) +
  scale_fill_manual(breaks = 1, values = "grey20", labels = "range") + 
  scale_colour_manual(breaks = 1, values = "#CC873D", labels = "median") +
  opts(legend.position=c(0.20,0.85), legend.title = theme_blank(), legend.background = theme_blank())

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文