Plot with bars and line : how to add the line legend ?

59 views
Skip to first unread message

jsubei

unread,
Mar 19, 2013, 9:28:27 AM3/19/13
to ggp...@googlegroups.com

Dear list,

I use the following code to draw the attached plot.

resultPlot <- ggplot()
resultPlot <- resultPlot + geom_bar(data=df, mapping=aes(x=month, y=value, fill=gear_type, order=gear_type), stat="identity", width=0.8) + scale_fill_brewer(palette="Set1", name="Gear type")
resultPlot <- resultPlot + geom_errorbar(data=dfPrev5Years, mapping=aes(x=month, ymax = mean_prev_5_years + stddev_prev_5_years, ymin=mean_prev_5_years - stddev_prev_5_years), width=0.25, color="dimgray")
resultPlot <- resultPlot + geom_line(data=dfPrev5Years, mapping=aes(x=month, y=mean_prev_5_years))
resultPlot <- resultPlot + xlab("Month") + ylab("Catches in tons") + ggtitle(paste(levels(df$species)[1], "monthly catches by gear type on", levels(df$year)[1]))

print(resultPlot)

And I just want to add a legend for the line and for the error bars. How can I do ?

regards,
Norbert

   

jsubei

unread,
Mar 21, 2013, 5:31:32 AM3/21/13
to ggp...@googlegroups.com
No idea ?

Janesh Devkota

unread,
Mar 21, 2013, 12:37:57 PM3/21/13
to jsubei, Ggplot2
Would it be helpful ? Please provide reproducible example . 

library(ggplot2)
library(plyr)

df <- data.frame(A = factor(rep(c('A1', 'A2'), each = 100)),
                 B = rep(1:5, each = 20), y = rnorm(200))
dfstats <- ddply(df, .(A, B), summarise, m = mean(y), s = sd(y))

ggplot(dfstats, aes(x = B, y = m, colour = A)) +
  geom_errorbar(aes(ymin = m - s, ymax = m + s), size = 1,
                position = position_dodge()) +
  ylim(-1.5, 1.5) +
  labs(x = 'B', y = 'y', colour = 'Group') +
  opts(legend.position = c(0.9, 0.92),
       panel.grid.major = theme_blank(),
       panel.grid.minor = theme_blank())


On Thu, Mar 21, 2013 at 4:31 AM, jsubei <jsu...@yahoo.fr> wrote:
No idea ?

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

jsubei

unread,
Mar 22, 2013, 6:20:41 AM3/22/13
to ggp...@googlegroups.com, jsubei
Hi all and thank you Janesh for the interest.

Yes I can provide a consistent example:

####################
library(ggplot2)
sampleNb <- 60
fakeDf <- data.frame(month=as.factor(as.integer(runif(sampleNb, min=1, max=12))), gear_type=as.factor(as.integer(runif(sampleNb, min=1, max=4))), value=rnorm(n=sampleNb), mean_previous=rnorm(n=sampleNb), stddev_previous=rnorm(n=sampleNb)/5)
fakeSecondDf <- data.frame(month=as.factor(as.integer(runif(sampleNb, min=1, max=12))), gear_type=as.factor(as.integer(runif(sampleNb, min=1, max=4))), mean_previous=rnorm(n=sampleNb), stddev_previous=rnorm(n=sampleNb)/5)
fakeSecondDf <- merge(aggregate(mean_previous ~ month, data=fakeDf, sum), aggregate(stddev_previous ~ month, data=fakeDf, mean))
resultPlot <- ggplot()

resultPlot <- resultPlot + geom_bar(data=fakeDf, mapping=aes(x=month, y=value, fill=gear_type, order=gear_type), stat="identity", width=0.8) + scale_fill_brewer(palette="Set1", name="Gear type")
resultPlot <- resultPlot + geom_errorbar(data=fakeSecondDf, mapping=aes(x=month, ymax = mean_previous + stddev_previous, ymin=mean_previous - stddev_previous), width=0.25, color="dimgray")
resultPlot <- resultPlot + geom_line(data=fakeSecondDf, mapping=aes(x=month, y=mean_previous, group="month"))
resultPlot <- resultPlot + xlab("Month") + ylab("Fake value") + ggtitle("monthly fake values by gear type")

print(resultPlot)
####################

And I just need to add a legend for the black line (and for error bars maybe).

An idea ?

Regards,
Norbert

jsubei

unread,
Mar 26, 2013, 5:21:57 AM3/26/13
to ggp...@googlegroups.com, jsubei
Hi all,

Nobody know ?

Ista Zahn

unread,
Mar 26, 2013, 9:01:43 AM3/26/13
to jsubei, ggp...@googlegroups.com
Hi,

AFAIK there is no easy way to get a legend for the different types of
geoms. Legends display aesthetic mappings, and the type of geom is not
considered an aesthetic. If you can tolerate having the "legend"
inside the plot area you can fake it with annotate(), otherwise you
will need to use annotation_custom() and learn enough about the grid
package to figure out how to use it.

Here is a crude example of the first approach:

library(ggplot2)

tmp <- data.frame(x = 1:20, y=(1:20)^2)

ggplot(tmp, aes(x=x, y=y)) +
geom_point() +
geom_line() +
annotate(geom="line", x=c(19:20), y=c(0:0)) +
annotate(geom="point", x=19, y=10) +
annotate(geom="text", x=18, y=2, label="my line label", hjust=1) +
annotate(geom="text", x=18, y=10, label="my point label", hjust=1)

HTH,
Ista

On Tue, Mar 26, 2013 at 5:21 AM, jsubei <jsu...@yahoo.fr> wrote:
> Hi all,
>
> Nobody know ?
>
>

jsubei

unread,
Mar 27, 2013, 5:40:04 AM3/27/13
to ggp...@googlegroups.com, jsubei
Thank you izahn, I will try to do my legend inside the plot like your recommendation.

Regards,
Norbert
Reply all
Reply to author
Forward
0 new messages