Re: Getting a dashed line into the legend

1,370 views
Skip to first unread message

Carl Duckworth

unread,
Sep 24, 2010, 5:59:11 AM9/24/10
to ggplot2
Here is my data and sample of PDF output.

Cheers,
Carl.
P.S. I have changed the column names in my data to protect the innocent.


On Fri, Sep 24, 2010 at 10:57 AM, Carl Duckworth <carl...@gmail.com> wrote:
Hi all,

I am trying to get a dashed line into the legend.  I have searched
through many posts and Hadley's book but I can't work it out.

I have a stacked bar plot, along with two lines.  It is in black and
white for publication so to differentiate between the lines, one is
solid and one is dashed.  The dashed line shows in the plot but I am
unable to get it to show in the legend.  I am including the pdf output
just to be thorough but can't work out had to include my csv and pdf
files.

Here is my code:

# The plot
samplePlot<- ggplot(sampleDataSet) +
       geom_bar(aes(dayCount, upper, fill = "Upper"), stat = "identity") +
       geom_bar(aes(dayCount, lower, fill = "Lower"), stat = "identity") +
       scale_fill_manual("The Steps", c("Upper" = "grey", "Lower" = "light
grey")) +
       geom_line(aes(dayCount, count, colour = "Count"), size = 0.3) +
       geom_line(aes(dayCount, oldSample, colour = "Old Sample"), linetype =
2, size = 0.3) +
       scale_colour_manual("Count and Old Sample", c("Count" = "black", "Old
Sample" = "black")) +
       opts(title = "Count Plotted with Steps and Old Sample") +
       labs(x = "Day Count", y = "Numbers", fill = "The Steps") +
       theme_bw()
print(samplePlot)

# Output plot as a PDF
pdf("/Users/Carl/Desktop/ggpplot_group_sample.pdf", width = 10, height
= 8)
print(samplePlot)
dev.off()

I have ensured that I have the latest version of R and recently did
the ggplot2 update.

Any help appreciated.


Cheers,
Carl.

ggpplot_group_sample.pdf
sampleData.csv

Carl Duckworth

unread,
Sep 24, 2010, 5:57:16 AM9/24/10
to ggplot2

Brandon Hurr

unread,
Sep 24, 2010, 9:01:31 AM9/24/10
to Carl Duckworth, ggplot2
Carl,
I'm not very good with scale_manual, but I got it to work by
rearranging your data and splitting it into two separate data frames.

SDS.bar<-cbind(sampleDataSet[1], sampleDataSet[3], sampleDataSet[5])
SDS.bar <- melt(SDS.bar, id.vars="dayCount")

SDS.line<-cbind(sampleDataSet[1], sampleDataSet[2], sampleDataSet[6])
SDS.line <- melt(SDS.line, id.vars="dayCount")


samplePlot<- ggplot() +
geom_bar(data=SDS.bar, aes(x=dayCount, y=value, fill=variable),


stat = "identity") +

#scale_fill_grey("The Steps", labels = c("Upper", "Lower"), breaks
= c("Upper", "Lower"), c("Upper" = "grey", "Lower" = "light grey")) +
geom_line(data=SDS.line, aes(dayCount, value, linetype =
variable), size = 0.3) +
#scale_linetype_manual("Count and Old Sample", c(1,2), labels =
c("Count" = "solid", "Old Sample" = "dashed")) +
opts(title = "Count Plotted with Steps and Old Sample",
legend.key.size = unit(2, "lines")) +


labs(x = "Day Count", y = "Numbers", fill = "The Steps") +
theme_bw()
print(samplePlot)

Every time I try and use the manual scales it breaks it though.
Perhaps someone else can fill in the holes or knows a way without
manipulating the data.

Brandon

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

Brandon Hurr

unread,
Sep 24, 2010, 9:39:02 AM9/24/10
to Carl Duckworth, ggplot2
I'm sure it's not quite perfect, but it looks almost exactly like the
one you posted at first and it has the dashed line...

SDS.bar<-cbind(sampleDataSet[1], sampleDataSet[3], sampleDataSet[5])
SDS.bar <- melt(SDS.bar, id.vars="dayCount")

SDS.line<-cbind(sampleDataSet[1], sampleDataSet[2], sampleDataSet[6])
SDS.line <- melt(SDS.line, id.vars="dayCount")

samplePlot<- ggplot() +
geom_bar(data=SDS.bar, aes(x=dayCount, y=value, fill=variable),
stat = "identity") +

scale_fill_grey(name= "The Steps", breaks= c("upper","lower"),
start= 0.8, end=0.65) +


geom_line(data=SDS.line, aes(dayCount, value, linetype =
variable), size = 0.3) +

opts(title = "Count Plotted with Steps and Old Sample",
legend.key.size = unit(2, "lines")) +
labs(x = "Day Count", y = "Numbers", fill = "The Steps") +
theme_bw()

print(samplePlot)

I really need to work on my knowledge of scales. :/

Brandon

Dennis Murphy

unread,
Sep 24, 2010, 8:44:20 PM9/24/10
to Carl Duckworth, ggplot2
Hi:

The problem comes in setting line types; the manual scales don't appear to like mapping text labels to individual numeric values. I tried a number of combinations: different forms of scale_linetype, manual setting of values, breaks and labels...no linetype scale. As a result, it's safer to map linetype by levels of a factor, and to do that, you need to reshape the data to allow a factor to be defined. Inconvenient but necessary, I'm afraid.

I used the reshape() function from base to derive a 'long' data frame, after which I defined a factor 'type' to distinguish Count from oldSample; their values are stacked in a variable cleverly named 'var'. Use the original data set for the bars and the reshaped data frame for the lines.

# Reshape data from 'wide' to 'long', where 'long' means stacking count
# and oldSample values. Stacking lower and upper turns out to be superfluous here,
# but it does show you how to stack more than one set of variables at a time :)
newdat <- reshape(sampleDataSet, idvar = 'dayCount',
                  varying = list(c('lower', 'upper'), c('count', 'oldSample')),
                  v.names = c('position', 'var'),
                  direction = 'long')
# Factor labels are set to what they would correspond in the legend
newdat$type <- factor(rep(c('Count', 'OldSample'), each = 62))


samplePlot<- ggplot(sampleDataSet) +
       geom_bar(aes(dayCount, upper, fill = "Upper"), stat = "identity") +
       geom_bar(aes(dayCount, lower, fill = "Lower"), stat = "identity") +
       scale_fill_manual("The Steps", c("Upper" = "grey", "Lower" = "grey30")) +
       geom_line(data = newdat, aes(dayCount, var, linetype = type), size = 0.3) +
       scale_linetype("Count and\nOld Sample") +
       opts(title = "Count Plotted with Steps and Old Sample") +
       labs(x = "Day Count", y = "Numbers", fill = "The Steps") +
       theme_bw()
print(samplePlot)


Once linetype is mapped to levels of a factor, the problem is solved.

HTH,
Dennis

Reply all
Reply to author
Forward
0 new messages