Re-order variables in ggplot such that a specific name is always in 'nth' position?

ggplots can use reorder to sort variables based on things like alphabetical order, or y-axis value for a plot. However, my situation is a little more complex and I can't figure it out.

My simple plot has 3 values: the intensities of color#1, color#2, and combined.

My goal is to have "combined" ALWAYS appear in the 3rd place, regardless of its value.

How can I accomplish this? I can't sort alphabetically (because the names of color1 and color2 will differ in the future) and I cannot sort by intensity since the 'combined' value will not always relfect color1+color2.

Therefore I need a way to say, "if the name is 'combined' plot it in 3rd place on the x-axis"

library(ggplot2)
mydf <- data.frame("color"=c("blue","gold","combined"),"intensity"=c(10,5,6))

ggplot(mydf, aes(x=color, y=intensity)) + geom_point()

You can try using color as factor with required position of "combined"

library(ggplot2)
mydf <- data.frame("color"=  factor(c("blue","gold","combined"), levels = c("blue","gold","combined")),"intensity"=c(10,5,6))
ggplot(mydf, aes(x=color, y=intensity)) + geom_point()

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.