Fine Tuning geom_path

580 views
Skip to first unread message

Lorenzo Isella

unread,
Jul 19, 2011, 4:18:49 PM7/19/11
to ggp...@googlegroups.com
Dear All,
Please consider the snippet at the end of the email.
I would like to do some fine tuning of the resulting plot, namely
(1) have more control of what I see in the legend: e.g. I would like to
show 1893, 1950 and 2005 as years in the legend.
(2) I added a new column to my dataframe (myear$extra): is there any way
I can represent this as a color and fine tune the corresponding legend
as well? This way I would have a plot where the line changes both in
size and color.

Many thanks.

Lorenzo

##############################################################################�
library(ggplot2)
# Generate data
myear <- ddply(movies, .(year), colwise(mean, .(length, rating)))
p <- ggplot(myear, aes(length, rating))
p + geom_path()

# Add aesthetic mappings
p + geom_path(aes(size = year))

#add new column to dataframe
myear$extra<-rev(myear$rating)

Brian Diggs

unread,
Jul 19, 2011, 5:00:28 PM7/19/11
to Lorenzo Isella, ggplot2
On 7/19/2011 1:18 PM, Lorenzo Isella wrote:
> Dear All,
> Please consider the snippet at the end of the email.
> I would like to do some fine tuning of the resulting plot, namely
> (1) have more control of what I see in the legend: e.g. I would like to
> show 1893, 1950 and 2005 as years in the legend.

scale_size(breaks = c(1893, 1950, 2005))

> (2) I added a new column to my dataframe (myear$extra): is there any way
> I can represent this as a color and fine tune the corresponding legend
> as well? This way I would have a plot where the line changes both in
> size and color.

geom_path(aes(size = year, colour = extra))

scale_colour_continuous() #use to define breaks, labels, etc.

> Many thanks.
>
> Lorenzo
>
> ##############################################################################�
>
> library(ggplot2)
> # Generate data
> myear <- ddply(movies, .(year), colwise(mean, .(length, rating)))
> p <- ggplot(myear, aes(length, rating))
> p + geom_path()
>
> # Add aesthetic mappings
> p + geom_path(aes(size = year))
>
> #add new column to dataframe
> myear$extra<-rev(myear$rating)

Must rerun assignment to p after adding the column; otherwise p doesn't
know about the column.

ggplot(myear, aes(length, rating)) +
geom_path(aes(size = year, colour = extra)) +
scale_size(breaks = c(1893, 1950, 2005)) +
scale_colour_continuous()

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

Dennis Murphy

unread,
Jul 19, 2011, 7:29:48 PM7/19/11
to Lorenzo Isella, ggp...@googlegroups.com
To add to Brian's insights, another way to deal with the colors is to
use a gradient. I chose to use gradient2 to restrict the color legend
to three levels; you could play around with scale_colour_gradientn()
if you wanted four or five instead.

p <- ggplot(myear, aes(x = length, y = rating))
p + geom_path(aes(size = year, colour = extra)) +


scale_size(breaks = c(1893, 1950, 2005)) +

scale_colour_gradient2(low = 'black', mid = 'dodgerblue', high = 'orange',
midpoint = 6.0, breaks = c(4.5, 6.0, 7.0))

HTH,
Dennis

On Tue, Jul 19, 2011 at 1:18 PM, Lorenzo Isella
<lorenzo...@gmail.com> wrote:
> Dear All,
> Please consider the snippet at the end of the email.
> I would like to do some fine tuning of the resulting plot, namely
> (1) have more control of what I see in the legend: e.g. I would like to show
> 1893, 1950 and 2005 as years in the legend.
> (2) I added a new column to my dataframe (myear$extra): is there any way I
> can represent this as a color and fine tune the corresponding legend as
> well? This way I would have a plot where the line changes both in size and
> color.
>
> Many thanks.
>
> Lorenzo
>

> ##############################################################################à


> library(ggplot2)
> # Generate data
> myear <- ddply(movies, .(year), colwise(mean, .(length, rating)))
> p <- ggplot(myear, aes(length, rating))
> p + geom_path()
>
> # Add aesthetic mappings
> p + geom_path(aes(size = year))
>
> #add new column to dataframe
> myear$extra<-rev(myear$rating)
>

> --
> 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
>

Reply all
Reply to author
Forward
0 new messages