Question

I just would like to add color on a plot but it seems that the classic col function does not work when a factor variable is used as one of the axis. Here is an example of what I do:

df<-data.frame(x=runif(1000,1,1000))

df$class<-Hmisc::cut2(df$x, g=10)

df.agg<-aggregate(df$x, by=list(df$class), FUN=sum)

plot(df.agg$Group.1, df.agg$x, col="red")

The plot values appear black. How to add color on this type of graph ? enter image description here

Was it helpful?

Solution

In this case you are actually making a boxplot because x is factor and y is numeric. For boxplot line colors are controled with argumnt border=

plot(df.agg$Group.1, df.agg$x, border="red")

You can see it by making the same plot with function boxplot().

boxplot(df.agg$x~df.agg$Group.1, , border="red")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top