在ggplot中编辑图例标签 [英] Editing legend labels in ggplot

查看:216
本文介绍了在ggplot中编辑图例标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  data 
中值最小值最大值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

$ b

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

  scalability_graph < - 函数(data){
h < - ggplot(data)
h <-h +
geom_ribbon (x = no_of_threads,y = median,color =#CC873D))+
scale_x_continuous(线程数 ,lim = c(0,20))+
scale_y_continuous(Response time,lim = c(0,13))+
opts(legend.position = c(0.20,0.90))





脚本产生这样的情节:

解决方案

您可以通过添加以下代码来完成此操作:

  + 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 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())


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:

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

解决方案

you can do that by adding this one:

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

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())

这篇关于在ggplot中编辑图例标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆