Adding a horizontal line to geom_bar facet graph

1,418 views
Skip to first unread message

pradip...@samhsa.hhs.gov

unread,
Oct 7, 2011, 5:06:26 PM10/7/11
to ggplot2, pradip...@samhsa.hhs.gov
Hello,

My Geom_bar facet graph was working fine. But the addition of a new
argument (dline) to the function gives me an error, and that is the
issue, which I can't resolve.

Please note that the object "dline" (which I have created, not shown
below) has 3 observations (facets) and 11 variables (which include
"percent" and "age").

What am I doing wrong?

I would appreciate receiving your help.

Thanks,

Pradip



######### Only the relevant part of the script is shown below #######

plotfun <- function(dd, ddlimits,dline, numcol, plotTitle, outFile)
{
p <- ggplot(dd, aes(y=percent, x=race, fill=race ))+
geom_bar(position=dodge) +
geom_errorbar(ddlimits, position=dodge, width=0.3) +
geom_hline(yintercept = dline, colour="red")+
facet_wrap(~ age, ncol=numcol)+
scale_fill_manual(values=colours)+
xlab("")+
ylab("Percentage")+
coord_flip ()+

opts(legend.position="none",
title=plotTitle,
axis.title.y = theme_text(face = 'bold', size = 10),
axis.title.x=theme_text(vjust=1, size=16, face="bold"),
axis.text.y=theme_text(size=24, hjust=1, face="bold"),
axis.text.x=theme_text(angle=90, hjust=1, size=24,
face="bold"),
strip.text.x = theme_text (size=24, face="bold"),
#panel.background=theme_rect(),
plot.title = theme_text(size=1,
face="bold")
)

print (p)
ggsave(file=outFile, width=11, height=7)

}
plotfun(danydis, lanydis, anyline, canydis, tanydis,
'abodill_race.png')

Dennis Murphy

unread,
Oct 7, 2011, 6:01:37 PM10/7/11
to pradip...@samhsa.hhs.gov, ggplot2
Hi:

As you're using it, geom_hline(yintercept = dline, ...) expects dline
to be a constant since it is not within an aes() statement. Inside
aes(), dline would be expected to be a vector. If dline is neither of
these, you may have to rethink your code.

HTH,
Dennis

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

Muhuri, Pradip (SAMHSA/CBHSQ)

unread,
Oct 8, 2011, 10:28:23 AM10/8/11
to Dennis Murphy, ggplot2

Dear Dennis,


Thank you for giving me the hints.

1) I have created an object for geom_hline (for 3 facets) successfully as follows: anyline <- subset (overall, flag_var=='any', select=c(percent, age)).
Per your hint, I have included anyline within the aes (). I it gives me 3 horizontal lines for each of the 3 facets. But I want only 1 horizontal line for each facet.
Any advice or hint to resolve the issue?


p <- ggplot(dd, aes(y=percent, x=race, fill=race ))+
geom_bar(position=dodge) +
geom_errorbar(ddlimits, position=dodge, width=0.3) +

facet_wrap(~ age, ncol=numcol)+
geom_hline(aes(yintercept=anyline$percent, colour="red", linetype=2))+


scale_fill_manual(values=colours)+
xlab("")+
ylab("Percentage")+
coord_flip ()+


2) When using the anyline via an argument (dline), it says dline is not found.

plotfun <- function(dd, ddlimits, dline, numcol, plotTitle, outFile)

plotfun(danydis, lanydis, anyline, canydis, tanydis, 'abodill_race.png')

___________________________

Regards,


Pradip Muhuri

_____________
From: Dennis Murphy [djm...@gmail.com]
Sent: Friday, October 07, 2011 6:01 PM
To: Muhuri, Pradip (SAMHSA/CBHSQ)
Cc: ggplot2
Subject: Re: Adding a horizontal line to geom_bar facet graph

Dennis Murphy

unread,
Oct 8, 2011, 12:10:17 PM10/8/11
to Muhuri, Pradip (SAMHSA/CBHSQ), ggplot2
Untested in the absence of a reproducible example, but try

geom_hline(data = anyline, aes(yintercept=percent), colour="red", linetype=2) +
...

in the middle of the code chunk that generates p.

This is similar to a post from earlier this week:
http://groups.google.com/group/ggplot2/browse_thread/thread/cffc07233c124b33

HTH,
Dennis

Muhuri, Pradip (SAMHSA/CBHSQ)

unread,
Oct 8, 2011, 10:03:02 PM10/8/11
to Dennis Murphy, ggplot2
Dear Dennis,

I have tried what you had suggested. That did not work. Any other suggestions?

Thank you for continued support.

Pradip Muhuri


________________________________________
From: Dennis Murphy [djm...@gmail.com]
Sent: Saturday, October 08, 2011 12:10 PM

Reply all
Reply to author
Forward
0 new messages