Showing posts with label not. Show all posts
Showing posts with label not. 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

Tuesday, January 28, 2014

Delete "Not Existing" files in Windows 7

Sometimes even after we delete a file or move it, its ghost copy might still remain in the original folder. The ghost file might have been created due to a race condition. When we try to delete the file, we get an error stating "Cannot Delete the File, The file does not exist".

In that case, use a command prompt in windows and navigate to the folder. Use "dir /x" command to see if the ghost file is being listed. As per the reference [1], if you can identify the 8.3 format filename in the output, you can use it to delete the file using "del <filename>" command from the prompt. An easier way would be to use wildcard filename to delete the ghost file. The wildcard command might look like " del *file.txt".


References:
1. http://superuser.com/questions/388860/cant-delete-files-that-do-not-exist-but-appear-in-my-download-directory