[R] Problem using ggplot2 with the legend of a stacked bar chart

John McKown john.archie.mckown at gmail.com
Fri Jul 25 16:03:20 CEST 2014


<sigh/>
Never mind. I had to specify the label in the "labs=()" portion of the
ggplot(). Why, if I'm going to "get it", do I "get it" only _after_ I
post?

On Fri, Jul 25, 2014 at 8:07 AM, John McKown
<john.archie.mckown at gmail.com> wrote:
> I am creating two graphs using ggplot2. The graphs are of the same
> data. One is a stacked bar chart. The other is a line graph. The code
> is below.
>
> <code>
> MSU_graph_m1   <- ggplot(cpprdald2_m1,aes(x=Int_Start,
>                                           y=LicPrLsys4HMSU,
>                                           fill=System_alias,
> #                                          color=System_alias,
>                                           ymax=LicPrLsys4HMSU_max))+
>                   theme(plot.title = element_text(size = rel(3),
> vjust=1, face="bold"),
>                         panel.grid.major = element_line(color="black",size=1),
>                         panel.grid.minor = element_line(color="black",size=1),
>                         legend.position="top",
>                         legend.direction="horizontal",
>                         legend.text = element_text(size=rel(1.5)),
>                         axis.line = element_line(size=3, color="black"),
>                         axis.text = element_text(angle=90, size=rel(0.9)),
>                         axis.title = element_text(size = rel(2)),
>                         axis.ticks = element_line(color="black")) +
>                   labs(x="Date and Time",y="MSUs",title=title)+
>
> guides(fill=guide_legend(reverse=TRUE,title="LPARs",direction="horizontal"))+
>                   scale_x_datetime(breaks = date_breaks('1 day'),
> minor_breaks=date_breaks('1 hour'),  labels=date_format("%b %d
> %H:%M")) +
>                   scale_y_continuous(breaks = seq(0,LicPrLsys4HMSU_max,5L)) +
>                   scale_fill_manual(values=c("yellow","blue")) ;
> MSU_graph_m1b  <- MSU_graph_m1+geom_bar(size=0.7,stat="identity");
> MSU_graph_m1l  <- MSU_graph_m1+geom_line(size=1.5);
> #
> </code>
>
> Now, if I use the "color=System_alias", the legend shows up where I
> want: on the top,  with the horizontal orientation, and labelled
> "LPARs". But I want the order reversed. Which I can't see how to do
> using this method. So I added the "guides=(fill..." line to reverse
> the legend. But now the legend is on the right, vertically oriented,
> and labelled "System_alias" (the variable name).
>
> I now have eye strain from looking at the vignettes and in the book "R
> Graphics Cookbook" and just can't figure out a way to get what I want.
>
> Now, using Rstudio, I examined what is in MSU_graph_m1. And saw a
> variable called "labels". This is a list containing more variables.
> Once of which is called "colour". That variable has the value of
> "System_alias". I found that if I set it to the value of "LPARs", then
> the legend has the proper title. But I don't like doing this sort of
> thing.
>
> --
> There is nothing more pleasant than traveling and meeting new people!
> Genghis Khan
>
> Maranatha! <><
> John McKown



-- 
There is nothing more pleasant than traveling and meeting new people!
Genghis Khan

Maranatha! <><
John McKown



More information about the R-help mailing list