Showing posts with label labels. Show all posts
Showing posts with label labels. Show all posts

Tuesday, November 24, 2015

R: MIssing legend when using scale_fill_manual

When configuring ggplot using scale_fill_manual in R, sometimes the legend might not be displayed. This happens when we specify 'breaks' property within the scale_fill_manual command, and the values we supply don't correspond to the factor levels we use in fill.

For instance, assume we have a data frame with columns 'X', 'Y', and 'Type', where 'Type' contains values 'A' and 'B'. In the below ggplot/ggplot2 command, breaks argument cannot contain a list of strings which do not map to factor levels in column 'Type':
ggplot2(data=df, aes(x=X, y=Y, fill=Type)) + 
scale_fill_manual(values=c("black","red", name="Type", breaks=c("A","B"), labels=c("Type-A","Type-B"))

Instead, we need to specify the breaks as:
ggplot2(data=df, aes(x=X, y=Y, fill=Type)) + 
scale_fill_manual(values=c("black","red", name="Type", breaks=levels(factor(df$Type)), labels=c("Type-A","Type-B"))

Or as below, if we are sure of the factor ordering:
ggplot2(data=df, aes(x=X, y=Y, fill=Type)) + 
scale_fill_manual(values=c("black","red", name="Type", labels=c("Type-A","Type-B"))

References:
1. http://stackoverflow.com/questions/14546858/missing-legend-when-combining-scale-fill-manual-and-scale-x-discrete-in-bar-char

Thursday, July 24, 2014

Latex 'pdfpagelabels' turned off when using Hyperref

I received the following error when using ACM's sig-alternate.cls style file and using the PDFLatex command.

Error:
Package hyperref Warning: Option `pdfpagelabels' is turned off
hyperref because \thepage is undefined.

That seems to be caused by an update of the hyperref package. The workaround is to switch this option off.
Instead of using:
\usepackage{hyperref}


Use:
\PassOptionsToPackage{pdfpagelabels=true}{hyperref}

References:
1. http://www.latex-community.org/forum/viewtopic.php?f=5&t=162

Monday, May 21, 2012

Vertical xtick Labels in Matlab graphs

This was taken from the mailing list entry available at: https://mailman.cae.wisc.edu/pipermail/help-octave/2009-July/036066.html
--------------------------------

here is a script that illustrates how to rotate the xtick label and put them vertically.

clear
## init demo plot
clf
plot(1:4);
xtick=[1 2 3 4];
set(gca,'xtick',xtick);
xticklabel=["a";"b";"c";"d"];
set(gca,'xticklabel',xticklabel);

## get position of current xtick labels
h = get(gca,'xlabel');
xlabelstring = get(h,'string');
xlabelposition = get(h,'position');

## construct position of new xtick labels
yposition = xlabelposition(2);
yposition = repmat(yposition,length(xtick),1);

## disable current xtick labels
set(gca,'xtick',[]);

## set up new xtick labels and rotate
hnew = text(xtick, yposition, xticklabel);
set(hnew,'rotation',90,'horizontalalignment','right');