[R] Legend (guides) for point and two line graph

Ista Zahn istazahn at gmail.com
Mon Dec 29 15:07:35 CET 2014


Hi David,

1) set size to a fixed value instead of mapping it to a constant,
i.e., geom_line(size = 2) instead of geom_line(aes(size = 2))

2) perhaps

ggplot(rtest, aes(x=Time, y=Calculated,color=Model, group=Model)) +
  geom_line(size = 2) +
  geom_point(aes(y=Observed, shape=""),
             size=6, colour="black") +
               scale_colour_manual(values=c("green","red")) +
  guides(shape = guide_legend("Observed",
                              override.aes=list(shape = 16)))

though this is a bit of a hack.

3) legends are automatically created when you map something to an
aesthetic. see ?guide and guide_legend etc. for customization.

Best,
Ista


On Mon, Oct 13, 2014 at 4:43 PM, David Bourne <david at boomer.org> wrote:
> I’m trying to generate a plot with a series of data points and best fit lines from two stat models. I’m generating the best-fits with another program. I have the data in a csv file as:
>
> Time,Observed,Calculated,Model
> 0.000,0.00000,13.0810,1C
> 0.2500,15.0000,12.5298,1C
> 0.5000,12.0000,12.0018,1C
> 1.000,9.00000,11.0117,1C
> 2.000,8.00000,9.26969,1C
> 4.000,6.50000,6.56882,1C
> 6.000,4.80000,4.65489,1C
> 9.000,3.20000,2.77680,1C
> 12.00,2.10000,1.65641,1C
> 18.00,1.80000,0.589422,1C
> 24.00,0.900000,0.209736,1C
> 0.000,0.00000,21.7130,2C
> 0.2500,15.0000,15.0512,2C
> 0.5000,12.0000,11.8203,2C
> 1.000,9.00000,9.29374,2C
> 2.000,8.00000,7.82242,2C
> 4.000,6.50000,6.20213,2C
> 6.000,4.80000,4.93346,2C
> 9.000,3.20000,3.50010,2C
> 12.00,2.10000,2.48310,2C
> 18.00,1.80000,1.24979,2C
> 24.00,0.900000,0.629039,2C
>
> I read in the data with (R 3.1.1 GUI 1.65 Mavericks build (6784)):
>
>>rtest <- read.csv("rtest.csv",header=TRUE)
>
> Checked with
>
>>rtest
>     Time Observed Calculated Model
> 1   0.00      0.0  13.081000    1C
> 2   0.25     15.0  12.529800    1C
> 3   0.50     12.0  12.001800    1C
> 4   1.00      9.0  11.011700    1C
> 5   2.00      8.0   9.269690    1C
> 6   4.00      6.5   6.568820    1C
> 7   6.00      4.8   4.654890    1C
> 8   9.00      3.2   2.776800    1C
> 9  12.00      2.1   1.656410    1C
> 10 18.00      1.8   0.589422    1C
> 11 24.00      0.9   0.209736    1C
> 12  0.00      0.0  21.713000    2C
> 13  0.25     15.0  15.051200    2C
> 14  0.50     12.0  11.820300    2C
> 15  1.00      9.0   9.293740    2C
> 16  2.00      8.0   7.822420    2C
> 17  4.00      6.5   6.202130    2C
> 18  6.00      4.8   4.933460    2C
> 19  9.00      3.2   3.500100    2C
> 20 12.00      2.1   2.483100    2C
> 21 18.00      1.8   1.249790    2C
> 22 24.00      0.9   0.629039    2C
>
> Generated the graph with:
>
> ggplot(rtest, aes(x=Time, y=Calculated,color=Model, group=Model)) + geom_line(aes(size=2)) + geom_point(aes(y=Observed, size=Observed), size=6, colour="black") + scale_colour_manual(values=c("green","red")) + labs(size="Observed”)
>
> Which resulted in the plot:
>
> http://www.boomer.org/rtest/rtest.pdf
>
> I’d like to:
>
> 1) get rid of the Observed / 2 legend(guide)
> 2) maybe keep the Observed and have a circle, i.e., loose the ‘2’
> 3) understand how to create, format the legend
>
> This seems like a common enough problem but the online documentation, R for Dummies nor the R Graphic Cookbook seems to have an answer (from my reading).
>
> Thanks for any clues/suggestion.
>
> David
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list