r - Colouring specific label in ggplot depending on the value of the id variable on a long data (irrespectively of the row number) -


let's have long data set , colour specific label on x-axis. in case of example below colour label valiant.

# packs require(ggplot2) require(reshape2)  # data , trans data(mtcars) mtcars$model <- rownames(mtcars) mtcars <- melt(mtcars, id.vars = "model")  # chart ggplot(data = subset(x = mtcars, subset = mtcars$variable == "cyl"),         aes(x = model, y = value)) +     geom_bar(stat = "identity") +     theme(axis.text.x = element_text(angle = 90,                                       colour =                                          ifelse(mtcars$model == "valiant",                                                 "red","black"))) 

the code produces chart below erroneous wrong label coloured.

wrong label

the reason simple created ifelse not match order on axis. can fix code forcing ggplot colour specific row. code below colours right label in particular data.frame used chart row valiant value 31.

# fixed chart ggplot(data = subset(x = mtcars, subset = mtcars$variable == "cyl"),         aes(x = model, y = value)) +     geom_bar(stat = "identity") +     theme(axis.text.x = element_text(angle = 90,                                       colour =                                          ifelse(as.numeric(rownames(mtcars)) == 31,                                                 "red","black"))) 

fixed label

clearly solutions extremely impractical. on actual data i've vast number of observations multiple columns (geo, gender, indicator, value, etc.). data subsequently filtered via subset , different options passed aes settings. trying figure out row should coloured nightmare. i'm looking solution enable me to:

  • relatively effortless indicate specific observation coloured without trying use row numbers
  • ideally use id string way of indicating text wan highlight
  • i encapsulate solution in ggplot2 code, don't want create separate data subsets derive colouring vector doing number of times. unnecessary multiply objects.
  • in practice, want solution work that: irrespectively of on chart, when find string on x-axis make red

the reason first 1 mismatches mtcars$model longer subset plotting, colour vector ifelse(mtcars$model == "valiant","red","black") of length 352 subset plotting of length 32. same problem exists second example, though in case elements of colour (which "black" anyway) dropped don't notice.

unfortunately looks theme(...) doesn't evaluated data column-names available (i.e. can't colour=ifelse(model == "valiant", "red", "black") directly in theme(...) call)

one alternative make model factor , filter on levels(..) == "valiant". if have long dataframe id variable factor anyway (or make sense one).

mtcars$model = factor(mtcars$model) ggplot(data=subset(mtcars, variable == 'cyl'), aes(x=model, y=value)) +     geom_bar(stat="identity") +     theme(axis.text.x=element_text(angle=90,               colour=ifelse(levels(mtcars$model) == 'valiant', 'red', 'black'))) 

(your problem stems feeding subset() ggplot data, , not being able refer particular subset in theme call. don't know if there tricksy way this).


Comments