ggplot2 geom_bar arrangement

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

ggplot2 geom_bar arrangement

Brian Smith
Hi,

I was trying to draw a geom_bar plot. However, by default, the bars are
arranged according to the label, which I don't want. I want the bars to
appear exactly as they appear in the data frame. For example in the code:

 Lab=c(letters[4:6],letters[1:3])
 valuex = c(3.1,2.3,0.4,-0.4,-1.2,-4.4)
 df <- data.frame(Lab,valuex)
 px <- ggplot(df,aes(Lab,valuex,label=Lab)) + geom_text(aes(y=0)) +
geom_bar(stat = "identity")
 px


The default arranges the bars in order 'a' through 'f', but I want them
arranged as per df.

How can I do this?

thanks!

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: ggplot2 geom_bar arrangement

Adams, Jean
You just have to change the levels of the factor ...

library(ggplot2)

Lab = c(letters[4:6], letters[1:3])
valuex = c(3.1,2.3,0.4,-0.4,-1.2,-4.4)
df <- data.frame(Lab,valuex)

# set the factor levels to the same order as observed in the data frame
df$Lab <- factor(df$Lab, levels=unique(df$Lab))

px <- ggplot(df,aes(Lab,valuex,label=Lab)) +
  geom_text(aes(y=0)) +
  geom_bar(stat = "identity")
px

Jean

On Tue, Jun 27, 2017 at 1:43 PM, Brian Smith <[hidden email]> wrote:

> Hi,
>
> I was trying to draw a geom_bar plot. However, by default, the bars are
> arranged according to the label, which I don't want. I want the bars to
> appear exactly as they appear in the data frame. For example in the code:
>
>  Lab=c(letters[4:6],letters[1:3])
>  valuex = c(3.1,2.3,0.4,-0.4,-1.2,-4.4)
>  df <- data.frame(Lab,valuex)
>  px <- ggplot(df,aes(Lab,valuex,label=Lab)) + geom_text(aes(y=0)) +
> geom_bar(stat = "identity")
>  px
>
>
> The default arranges the bars in order 'a' through 'f', but I want them
> arranged as per df.
>
> How can I do this?
>
> thanks!
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: ggplot2 geom_bar arrangement

Brian Smith
Thanks Jean, that worked!

On Tue, Jun 27, 2017 at 3:58 PM, Adams, Jean <[hidden email]> wrote:

> You just have to change the levels of the factor ...
>
> library(ggplot2)
>
> Lab = c(letters[4:6], letters[1:3])
> valuex = c(3.1,2.3,0.4,-0.4,-1.2,-4.4)
> df <- data.frame(Lab,valuex)
>
> # set the factor levels to the same order as observed in the data frame
> df$Lab <- factor(df$Lab, levels=unique(df$Lab))
>
> px <- ggplot(df,aes(Lab,valuex,label=Lab)) +
>   geom_text(aes(y=0)) +
>   geom_bar(stat = "identity")
> px
>
> Jean
>
> On Tue, Jun 27, 2017 at 1:43 PM, Brian Smith <[hidden email]>
> wrote:
>
>> Hi,
>>
>> I was trying to draw a geom_bar plot. However, by default, the bars are
>> arranged according to the label, which I don't want. I want the bars to
>> appear exactly as they appear in the data frame. For example in the code:
>>
>>  Lab=c(letters[4:6],letters[1:3])
>>  valuex = c(3.1,2.3,0.4,-0.4,-1.2,-4.4)
>>  df <- data.frame(Lab,valuex)
>>  px <- ggplot(df,aes(Lab,valuex,label=Lab)) + geom_text(aes(y=0)) +
>> geom_bar(stat = "identity")
>>  px
>>
>>
>> The default arranges the bars in order 'a' through 'f', but I want them
>> arranged as per df.
>>
>> How can I do this?
>>
>> thanks!
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posti
>> ng-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: ggplot2 geom_bar arrangement

Thomas Mailund-2
In reply to this post by Brian Smith
The order the bars are plotted in is determined by the levels in a factor, and your labels are treated as a factor. You can make sure you keep the order of your labels by simply doing this:

Lab <- factor(Lab, levels = Lab)

before constructing the data frame.

Cheers

On 27 Jun 2017, 20.43 +0200, Brian Smith <[hidden email]>, wrote:

> Hi,
>
> I was trying to draw a geom_bar plot. However, by default, the bars are
> arranged according to the label, which I don't want. I want the bars to
> appear exactly as they appear in the data frame. For example in the code:
>
> Lab=c(letters[4:6],letters[1:3])
> valuex = c(3.1,2.3,0.4,-0.4,-1.2,-4.4)
> df <- data.frame(Lab,valuex)
> px <- ggplot(df,aes(Lab,valuex,label=Lab)) + geom_text(aes(y=0)) +
> geom_bar(stat = "identity")
> px
>
>
> The default arranges the bars in order 'a' through 'f', but I want them
> arranged as per df.
>
> How can I do this?
>
> thanks!
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: ggplot2 geom_bar arrangement

Jeff Newmiller
In reply to this post by Brian Smith
In the general case it is not possible to do as you ask because "Lab" can be duplicated. However, in your specific case it is unique in your data frame, so you just have to control the order of the factor labels instead of letting them be set up in the default manner. Of course, you have to be aware that sticking character vectors into data frames without using the stringsAsFactors argument (read the help on the data.frame function) means they get converted to factors automatically so that is where you have to take control.

df <- data.frame( Lab=factor( Lab, labels=Lab ),valuex)
--
Sent from my phone. Please excuse my brevity.

On June 27, 2017 2:43:34 PM EDT, Brian Smith <[hidden email]> wrote:

>Hi,
>
>I was trying to draw a geom_bar plot. However, by default, the bars are
>arranged according to the label, which I don't want. I want the bars to
>appear exactly as they appear in the data frame. For example in the
>code:
>
> Lab=c(letters[4:6],letters[1:3])
> valuex = c(3.1,2.3,0.4,-0.4,-1.2,-4.4)
> df <- data.frame(Lab,valuex)
> px <- ggplot(df,aes(Lab,valuex,label=Lab)) + geom_text(aes(y=0)) +
>geom_bar(stat = "identity")
> px
>
>
>The default arranges the bars in order 'a' through 'f', but I want them
>arranged as per df.
>
>How can I do this?
>
>thanks!
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.