guides(guide_legend(...)) using manual shapes& linetypes

264 views
Skip to first unread message

Lighton Phiri

unread,
Mar 20, 2013, 10:35:20 AM3/20/13
to ggplot2
So I'm working on aesthetics for my plots and having issues telling
ggplot2 how to alter the legend with a combination of linetypes and
shapes. I basically have two issues here:

(i) What am I doing wrong in my code? How do I make guide_legend(...) work?
guides(linetype=guide_legend(nrow = 2)) has no effect when I expect my
legend to be redered in two rows NOT the default four

(ii) The sample code below uses different linetypes and shapes
(scale_linetype_manual and scale_shape_manual); how do I combine the
two using guides(...)?

********** CODE **********

# import libraries
require(tikzDevice)
require(ggplot2)
require(reshape2)
require(grid)
require(gridExtra)
require(scales)

# import datafeeds
oaipmh_dataset <- read.csv(text="Load,GR,LI,LR,LS
100,0.01,0.01,0.48,0
200,0.01,0.02,0.98,0.01
400,0.01,0.06,2,0
800,0.02,0.17,4.12,0.01
1600,0.02,0.39,5.41,0.01
3200,0.05,0.77,5.94,0.02
6400,0.08,1.53,6.73,0.04
12800,0.15,3.09,8.37,0.07
25600,0.3,6.25,11.68,0.12
51200,0.59,13.38,18.9,0.24
102400,1.2,28.46,33.46,0.49
204800,2.45,61.86,66.95,0.99
409600,4.87,133.94,139.72,1.99
819200,10.72,548.08,586.45,11.61
1638400,608.11,1922.89,1937.09,609.18
")

# convert dataset to molten form for ggplot2
oaipmh_experimentdataset <- melt(oaipmh_dataset, id.vars=c(1))

# add dummy column for faceting
oaipmh_experimentdataset["yaba"] <- "Dummy Facet Text"

ggplot(data=oaipmh_experimentdataset,
aes(x=as.factor(Load),
y=value,
group=variable)) +
geom_line(aes(linetype=variable)) +
geom_point(aes(shape=variable), fill="white") +
scale_shape_manual(values=c(1,4,13,23)) +
scale_linetype_manual(values=c("solid","dotdash","longdash","dotted")) +
facet_wrap(~ yaba, scales="free_x") +
scale_x_discrete(labels=c("100","200","400","800","1.6k","3.2k","6.4k","12.8k","25.6k","51.2k","102.4k","204.8k","409.6k","819.2k","1638.4k"))
+
scale_y_log10(breaks = trans_breaks("log10", function(x)
10^x), labels = trans_format("log10", math_format(10^.x))) +
labs(x="Load size",y="log10(Time [ms])") +
guides(linetype=guide_legend(nrow = 2)) +
theme(legend.title=element_blank(),
#legend.position=c(0.16,0.74),
legend.position="bottom",
#legend.direction="horizontal",
#plot.margin=unit(c(0.2,0.15,0.8,0.8),"cm"),
plot.margin=unit(c(0.2,0.15,0.2,0.2),"cm"),
axis.title.x = element_text(vjust=-0.01),
axis.title.y = element_text(vjust=0.45),
axis.text.x = element_text(angle=90, vjust=0.5))


-- Phiri
http://lightonphiri.org

Dennis Murphy

unread,
Mar 20, 2013, 12:14:32 PM3/20/13
to Lighton Phiri, ggplot2
Hi:

Since you're combining legends, you need to perform the same
operations on both legends. Try this (the primary change is in the
guides() line):

xlabels <- c("100","200","400","800","1.6k","3.2k","6.4k","12.8k",
"25.6k","51.2k","102.4k","204.8k","409.6k","819.2k","1638.4k")

ggplot(data=oaipmh_experimentdataset,
aes(x=as.factor(Load),
y=value,
group=variable)) +
geom_line(aes(linetype=variable)) +
geom_point(aes(shape=variable), fill="white") +
scale_shape_manual(values=c(1,4,13,23)) +
scale_linetype_manual(values=c("solid","dotdash","longdash","dotted")) +
facet_wrap(~ yaba, scales="free_x") +
scale_x_discrete(labels = xlabels) +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x))) +
labs(x="Load size",y="log10(Time [ms])") +
guides(linetype=guide_legend(nrow = 2), shape = guide_legend(nrow = 2)) +
theme(legend.title=element_blank(),
#legend.position=c(0.16,0.74),
legend.position="bottom",
#legend.direction="horizontal",
#plot.margin=unit(c(0.2,0.15,0.8,0.8),"cm"),
plot.margin=unit(c(0.2,0.15,0.2,0.2),"cm"),
axis.title.x = element_text(vjust=-0.01),
axis.title.y = element_text(vjust=0.45),
axis.text.x = element_text(angle=90, vjust=0.5))

> --
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Lighton Phiri

unread,
Mar 20, 2013, 3:54:01 PM3/20/13
to Dennis Murphy, ggplot2
Precisely what I needed. Thank you so much Dennis.

-- Phiri
http://lightonphiri.org
Reply all
Reply to author
Forward
0 new messages