在 ggplot2 中自定义图例文本和颜色

发布于 2024-11-08 22:41:28 字数 1009 浏览 1 评论 0原文

我正在尝试绘制一个图(使用ggplot)来绘制geom_pointgeom_line,其中不同的变量以不同的颜色绘制(根据scale_colour_manual(value = color) ),其中 color 是自定义颜色数组)以及两条水平黑线 (geom_hline)。

当我尝试自定义水平线的图例文本时,我的麻烦出现了。看来我只能选择两者之一:

  1. 水平线的颜色为黑色,但该行的图例文本不正确
  2. 该行的图例文本是正确的,但水平线的颜色由上述 < 确定代码>scale_colour_manual颜色。

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

生成图例中显示“黑色”的黑线

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

生成一条线,该线是我定义的 scale_colour_manual 数组中的下一个颜色,但图例文本是“Mean + 2 Stdev”。

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

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.

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

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

发布评论

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

评论(2

染年凉城似染瑾 2024-11-15 22:41:28

获得所需内容的一种方法是将水平黑线的数据包含在原始数据框中。您的示例并不完全可重现,因此这可能不完全是您想要的代码,但这是总体思路:

将行添加到“数据”,每个“月”级别一个,其中“avgLoss”的值在每行等于“meanplus2sd”。您可能希望所有其他列都包含 NA。所以,

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)

这应该可以帮助你开始,你只需要确保颜色“黑色”位于scale_colour_manual中的正确位置,这可能需要一些调整。

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.

找个人就嫁了吧 2024-11-15 22:41:28

注释可能是一个不错的选择

x <- baseball
tmp <- subset(x, team == c("CHN","NYA"))
cols <- c("8" = "red","4" = "blue","6" = "darkgreen")
meanplus2sd <- mean(tmp$rbi) + 2*sd(tmp$rbi)

plot <- ggplot(tmp, aes(x = year, y = rbi, color = factor(team), order = -as.numeric(team))) + 
geom_line() + geom_point() + geom_hline(aes(yintercept = meanplus2sd)) + 
annotate("text",label="Mean + 2sd",x=min(tmp$year)+10, y=meanplus2sd+10)

Annotate might be a good option

x <- baseball
tmp <- subset(x, team == c("CHN","NYA"))
cols <- c("8" = "red","4" = "blue","6" = "darkgreen")
meanplus2sd <- mean(tmp$rbi) + 2*sd(tmp$rbi)

plot <- ggplot(tmp, aes(x = year, y = rbi, color = factor(team), order = -as.numeric(team))) + 
geom_line() + geom_point() + geom_hline(aes(yintercept = meanplus2sd)) + 
annotate("text",label="Mean + 2sd",x=min(tmp$year)+10, y=meanplus2sd+10)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文