Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colorize labels for a group of points

Tags:

r

ggplot2

Is it possible, in ggplot2, to colorize labels for a group of points?

I would like to color some of the left-hand-side text labels in the plot below to show the swing states in red colour, in addition to the red marker shown in the plot itself:

electoral dot plot

The code (with data) is here. -- edited to reflect answer

  • I know how to colour all labels (here too), but that's not what I need here.
  • Ideally, I'd also like to improve the legend but am unsure exactly how.

The plot is far from perfect, so additional suggestions are very welcome. There are far better graphs out there if anyone's interested (but I'm not good enough to code them).

like image 226
Fr. Avatar asked Dec 08 '22 18:12

Fr.


1 Answers

Colors of labels (axis texts) are set by argument element_text= in function theme(). You can set different colors for each label. As there is a column Swing with levels, it can be used to set colors.

dw_plot + theme(axis.text.y = element_text(colour = ifelse(dw_data$Swing=="Swing State","red","grey")))
like image 108
Didzis Elferts Avatar answered Dec 11 '22 07:12

Didzis Elferts