[R] Changing legend to fill colour in ggplot

Brian Diggs diggsb at ohsu.edu
Fri Jun 28 20:51:06 CEST 2013


On 6/27/2013 12:34 AM, Suparna Mitra wrote:
> Hello R experts,
>    I am having a problem to edit legend in ggplot using four variables.
>
> My data structure is :
> str(df)
> 'data.frame': 10 obs. of  6 variables:
>   $ id                        : Factor w/ 2 levels "639A","640": 1 1 1 1 1 2
> 2 2 2 2
>   $ species                   : Factor w/ 5 levels "acinetobacter_sp",..: 2
> 5 1 4 3 2 5 1 4 3
>   $ genome_coverage_bp        : int  8196 3405 8625 22568 2128 6100 1841
> 3914 8487 1064
>   $ genome_length             : int  3571237 2541445 3912725 3479613 5460977
> 3571237 2541445 3912725 3479613 5460977
>   $ genome_coverage_percentage: Factor w/ 10 levels "0.02%","0.04%",..: 8 5
> 7 10 2 6 3 4 9 1
>   $ avg_depth_coverage        : num  121.96 2.81 19.84 399.63 1.64 ...
>
>
> Now what I did is
> p=ggplot(df,aes(genome_coverage_percentage,avg_depth_coverage))+geom_point(aes(colour
> = species,shape = factor(id)))
> p+scale_shape_discrete(name  ="",labels=c("Patient 1", "Patient 2"))
> That creats the plot below.
> But I want to change the circles of legend in fill colour. So that it
> doesn't look like it is only from Patient 1, as that also has circle.
> Can anybody help me please?

You can change the default aesthetics displayed in the legend using the 
override.aes argument to guide_legend.

scale_colour_discrete(guide=guide_legend(override.aes=aes(shape=15)))

Also, when giving data, especially a data set this small, give the 
output of dput(df) as that gives the complete data in a format that can 
be used to recreate it exactly in someoneelse's session. If I had that, 
I would test this to make sure it looks right.

> Thanks a lot in advance :)
> Mitra
>
>
>


-- 
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University



More information about the R-help mailing list