在ggplot2中定制图例文字和颜色 [英] Customizing legend text and colors in ggplot2

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

问题描述

我试图得到一个绘图(使用ggplot),它会绘制 geom_point geom_line ,其中不同的变量是绘制成不同的颜色(根据 scale_colour_manual(value = color)其中 color 是一种自定义颜色数组)到两条水平的黑线( geom_hline )。



当我尝试获取水平线定制。看起来我只能有以下两种:水平线的颜色为黑色,但此线的图例文本为


  1. 不正确

  2. 这条线的图例文本是正确的,但水平线的颜色由上述 scale_colour_manual 颜色确定。 li>

  plot<  -  ggplot (data,aes(x = factor(Month),y = avgLoss,color = type,order = -as.numeric(type)))
+ geom_line()+ geom_point()

meanplus2sd < - mean(data $ avgLoss)+ 2 * sd(data $ avgLoss)

plot < - plot + geom_hline(aes(yintercept = meanplus2sd),color =black)

会在图例中产生黑色线条,表示黑色

  plot < -  plot + geom_hline(aes(yintercept = meanplus2sd,color =Mean + 2 Stdev。))

会生成一行,这是我定义的 scale_colo中的下一个颜色ur_manual 数组,但是图例文本是Mean + 2 Stdev。



除了标准 geom_point + geom_line 绘图之外,为水平线获取自定义颜色和图例文本的任何帮助都非常出色。感谢。

解决方案

获得所需内容的一种方法是将水平黑线的数据包含在原始数据框中。你的例子不是完全可重复的,所以这可能不是你想要的代码,但这是一般的想法:

将行添加到'data',一个用于'月'的每个级别,其中每行中'avgLoss'的值等于'meanplus2sd'。您可能希望所有其他列都包含NAs。因此,

  newData < -  head(data,length(unique(data $ Month)))
newData $ (独立的(数据$月)
新的数据$类型< - rep('平均值+2sd', - 数据$月)
newData $ avgLoss< (数据$ month))
#然后将newData中的所有其他值设置为NA ...并且
data <-rbind(data,newData)

这应该让你开始,你只需确保颜色黑色在scale_colour_manual的正确位置,这可能需要一些小窍门。


I am trying to get a plot (using ggplot) that will plot geom_point and geom_line where different variables are plotted in different colors (according to scale_colour_manual(value = color) where color is a custom array of colors) in addition to two horizontal black lines (geom_hline).

My trouble arises when I attempt to get the legend text for the horizontal lines customized. It appears that I can have only one of the two:

  1. the color of the horizontal line as black but the legend text for this line is incorrect
  2. the legend text for this line is correct but the color of the horizontal line is determined by the aforementioned scale_colour_manual color.

plot <- ggplot(data, aes(x = factor(Month), y = avgLoss, colour = type, order = -as.numeric(type)))
         + geom_line() + geom_point()

meanplus2sd <- mean(data$avgLoss) + 2*sd(data$avgLoss)

plot <- plot + geom_hline(aes(yintercept = meanplus2sd), colour = "black")

produces black line that says "black" in the legend

plot <- plot + geom_hline(aes(yintercept = meanplus2sd, colour = "Mean + 2 Stdev."))

produces a line that is the next color in my defined scale_colour_manual array, but the legend text is "Mean + 2 Stdev."

Any help in getting both custom color and legend text for a horizontal line in addition to the standard geom_point + geom_line plotting would be excellent. Thanks.

解决方案

One way to get what you want would be to include the data for your horizontal black line in the original data frame. You example isn't exactly reproducible, so this may not be exactly the code you want, but this is the general idea:

Add rows to 'data', one for each level of 'Month', where the value of 'avgLoss' in each row is equal to 'meanplus2sd'. You'll probably want all the other columns to contain NAs. So,

newData <- head(data,length(unique(data$Month)))
newData$Month <- unique(data$Month) 
newData$avgLoss <- rep(meanplus2sd,length(unique(data$Month)
newData$type <- rep('Mean + 2sd',length(unique(data$Month)))
#Then set all other values in newData to NA...and,
data <- rbind(data,newData)

That should get you started, you'll just have to make sure that the color "black" is in the right position in scale_colour_manual, which may take some fiddling.

这篇关于在ggplot2中定制图例文字和颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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