Multiple colors of axis labels

4,413 views
Skip to first unread message

Mateusz

unread,
Feb 2, 2011, 10:19:49 AM2/2/11
to ggplot2
Hi,

Is it possible somehow to have a vector of colors how the labels
should be colored or different colors according to some kind of
factor ?

What I use now is
+ opts(axis.text.x = theme_text(size = 4,angle=90,hjust=1.2),
axis.text.y = theme_text(size = 4,hjust=1.2));

but it would be nice to also set more then one color for labels on the
axis.

Thanks in advance (PS. Example is any plot with labels on X axis).

Brandon Hurr

unread,
Feb 2, 2011, 10:28:56 AM2/2/11
to Mateusz, ggplot2
Yes, in fact here is what my code looks like... 

opts(title="Iceberg - Postharvest Quality"

,strip.background = theme_rect(colour = 'NA', fill = 'NA')

,axis.text.y = theme_text(colour=c( "red", "black", "red", "black", "black", "black", "black", "black", "black", "black"

, "black", "black", "black", "black", "black", "black", "black", "black", "black", "black"

, "black", "black", "black", "black", "black", "black", "black", "black", "black", "black"

, "black", "black", "black", "black", "black", "red", "black", "red", "black", "black"

, "black", "black", "black", "black", "black", "black", "black", "black", "black", "black"

, "black", "black", "black", "black", "red", "black", "black", "black", "black", "red"), hjust=1))+


Kind of messy how I did it, but you could use a if/else statement to define your colors in a vector the length of your factor levels and do it that way... If you want to do this on a continuous axis you might have some trouble (or so I believe.


Brandon 



--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Ann

unread,
Mar 31, 2011, 4:12:55 AM3/31/11
to ggplot2
Hello,

I want to do the same (colour Y axis labels) according to a vector
(linked to a variable). But I have a facet_grid, and therefore if I
use this method, the color vector is adapted to each facet, starting
at the beginning each time.

Here's an example:
###begin example code###
#dataframe
df<-
data.frame(place=c("A","B","C","D","E","F","G","H","I","J"),region=c("I","B","NL","B","B","B","NL","I","NL","I"),type=c("top","middle","bottom","top","top","bottom","top","middle","bottom","bottom"),number=c(7,6,8,9,1,3,5,7,9,1))

#add colour variable, based on the variable 'region'
df$colour<-NA
df[df$region =="B",]$colour<-"red"
df[df$region =="NL",]$colour<-"orange"
df[df$region =="I",]$colour<-"green"

#re-order levels variable 'type'
df$type<-factor(df$type,levels=c("top","middle","bottom"))

#sort dataframe in the same way the graphic will be ordered (so the
colours are in the right order)
sortframe=function(df,...)df[do.call(order,list(...)),]
df<- with(df,sortframe(df,1/as.numeric(type)))

#make a vector of the variable 'colour'
colour<-df$colour

#plot
ggplot(df,aes(number,place)) +
geom_point()+
facet_grid(type~.,scales="free_y",space="free")+
opts(axis.text.y=theme_text(hjust=1,colour=colour,size=10.0))
###end example code###

The dataframe looks like this:
place region type number colour
3 C NL bottom 8 orange
6 F B bottom 3 red
9 I NL bottom 9 orange
10 J I bottom 1 green
2 B B middle 6 red
8 H I middle 7 green
1 A I top 7 green
4 D B top 9 red
5 E B top 1 red
7 G NL top 5 orange


When plotting, we see that there are 3 facets: bottom (4 places),
middle (2 places) and top (4 places). Only the first 4 colours of the
vector are used and applied to each facet seperatly.

Is there a solution to use the vector so it will continues over the
facets (so B is red, H is green, ...)?

Thanks in advance!

Ann Frederix
> > To unsubscribe: email ggplot2+u...@googlegroups.com<ggplot2%2Bunsu...@googlegroups.com >
> > More options:http://groups.google.com/group/ggplot2

Brandon Hurr

unread,
Mar 31, 2011, 4:15:06 AM3/31/11
to Ann, ggplot2
I think the colors just need the "" around them. Otherwise it's looking for a variable called orange not the color orange.

B

Brandon Hurr

unread,
Mar 31, 2011, 4:24:34 AM3/31/11
to Ann, ggplot2
Nevermind, I need to wake up first. I'm not sure how you'd do it with a facet like that. Like you say it's reading the vector of colors from the start for each facet rather than linking it up with the variable as you intend. 

B
Reply all
Reply to author
Forward
0 new messages