VDFerreira VDFerreira - 2 months ago 24
R Question

Manipulate colours on ggplot2

I want to build a

qplot
using this library.

I have a dataset

Head(data)

tstamp elementid value hours
2016-09-15 15:20:28 IN_TEMP 24.99558 15
2016-09-15 15:20:28 IN_TEMP 25.12237 15
2016-09-15 15:20:29 IN_TEMP 25.44952 15
2016-09-15 15:20:29 IN_TEMP 25.53550 15
2016-09-15 15:20:39 IN_PRESSURE 101.40683 15
2016-09-15 15:20:49 IN_TEMP 25.94596 15


and I created my
qplot


qplot(data = data, y = value, log = "xy", color = elementid ,facets = hours~elementid)


enter image description here

But now I am trying to manipulate the plot.

for example:

I want the "IN_TEMP" div on the left side of the graph
and I want swap the colours of the points (blue become red and red become blue)

Can someone help me?

Answer

So you can create elementid as an ordered factor, R automatically sorts the factor levels alphabetically but you can use the ?factor order like this.

    elementid  <- factor(elementid , levels=c("IN_TEMP","IN_PRESSURE"), ordered=TRUE)

That should solve both your colours and your plot order problem.