本文介绍了使用多个aes()参数设置ggplot2表达式中一行的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 示例<  - 数据

我想为这个数据集中的每个系列绘制一条不同的线:

(xAxis = c(1,2,3,4,5),
ValueA = c(5.5,4.5,4,2.9,2),
ValueB = c(5,5.3,5 ,4.7,4),
ValueC = c(4,3.2,3,4,3),
ValueD = c(5,4.5,3,2.9,3))

遵循 geom_line 和<$ c $在ggplot2包中,我构建了我的图:

  library(ggplot2) 

ggplot(例如,aes(x = xAxis))+
geom_line(aes(y = ValueA))+
geom_line(aes(y = ValueB))+
geom_line(aes(y = ValueC))+
geom_line(aes(y = ValueD))

虽然设置颜色参数会产生问题。使用以下内容似乎为系列添加了标签,但不会影响颜色选择:

  ggplot(例如,aes(x = xAxis) )+ 
geom_line(aes(y = ValueA,color =green))+
geom_line(aes(y = ValueB,color =blue))+
geom_line(aes y = ValueC,color =yellow))+
geom_line(aes(y = ValueD,color =red))

然而,如果我将它们中的每一个设置为红色,则情节理解为将它们全部设置为红色。

  ggplot(例如,aes(x = xAxis))+ 
geom_line(aes(y = ValueA,color = red))+
geom_line(aes(y = ValueB,color =red))+
geom_line(aes(y = ValueC,color =red))+
geom_line(aes(y = ValueD,color =red))

我错过了什么?我已经看到ggplot中的多个系列图的'标准'答案也是使用重塑来融化数据,但我觉得应该有一个体面的解决方案,而不需要这样。

aes()之外移动颜色参数。然后,您将看到您指定的四种颜色。

  ggplot(例如,aes(x = xAxis))+ 
geom_line(aes(y = ValueA),color =green)+
geom_line(aes(y = ValueB),color =blue)+
geom_line(aes(y = ValueC), color =yellow)+
geom_line(aes(y = ValueD),color =red)

I would like to plot a different line for each of my series in this data set:

example <- data.frame(xAxis = c(1, 2, 3, 4, 5),
                  ValueA = c(5.5, 4.5, 4, 2.9, 2),
                  ValueB = c(5, 5.3, 5, 4.7, 4),
                  ValueC = c(4, 3.2, 3, 4, 3),
                  ValueD = c(5, 4.5, 3, 2.9, 3))

Following what seems like the intended usage of geom_line and aes in the ggplot2 package, I construct my graph like so:

library(ggplot2)

ggplot(example, aes(x = xAxis)) + 
    geom_line(aes(y = ValueA)) + 
    geom_line(aes(y = ValueB)) + 
    geom_line(aes(y = ValueC)) + 
    geom_line(aes(y = ValueD))

Setting the colour argument though is creating problems. Using the following seems to label the series, but not affect the colour selection:

ggplot(example, aes(x = xAxis)) + 
geom_line(aes(y = ValueA, colour = "green")) + 
geom_line(aes(y = ValueB, colour = "blue")) + 
geom_line(aes(y = ValueC, colour = "yellow")) + 
geom_line(aes(y = ValueD, colour = "red"))

However, if I set each of them to "red", then the plot understands to set them all to "red".

ggplot(example, aes(x = xAxis)) + 
geom_line(aes(y = ValueA, colour = "red")) + 
geom_line(aes(y = ValueB, colour = "red")) + 
geom_line(aes(y = ValueC, colour = "red")) + 
geom_line(aes(y = ValueD, colour = "red"))

What have I missed? I've seen that a 'standard' answer to multiple series plots in ggplot is to also use reshape to melt the data, but I feel there should be a decent solution here without needing that.

解决方案

The solution is to move the colour argument outside of aes(). Then, you will see the four colours you specified.

ggplot(example, aes(x = xAxis)) + 
geom_line(aes(y = ValueA), colour = "green") + 
geom_line(aes(y = ValueB), colour = "blue") + 
geom_line(aes(y = ValueC), colour = "yellow") + 
geom_line(aes(y = ValueD), colour = "red")

这篇关于使用多个aes()参数设置ggplot2表达式中一行的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 14:06