ggplot barplot order xlabel [duplicate]












0
















This question already has an answer here:




  • Order Bars in ggplot2 bar graph

    12 answers




I am trying to make a barplot that is not stackable and so far I was able to go quite far.



Since I have found out that ggplot plots the data.frame as is, without ordering I have ordered my data.frame before feeding it to ggplot. Where I ordered it based on word occurences



Both<-Both[order(Both$occurence,decreasing=TRUE),]


The structure looks like now as



 str(Both)
'data.frame': 82 obs. of 3 variables:
$ word : Factor w/ 6382 levels "a","abcc","abda",..: 1595 1994
$ occurence: int 4171 3004 2145 1821 1798 1745 1614 1539 1531 1349 ...
$ sentiment: num 0 0 0 0 0 0 0 0 0 0 ...


Variable



> Both
word occurence sentiment
**19** i 4171 0
**12** m 3004 0
**34** true 2145 0
**36** kubelet 1821 1
**18** normal 1798 1


Plot



ggplot(data=Both,aes(x=word,y=occurence,factor(sentiment),fill=factor(sentiment)))+
coord_flip()+geom_bar(stat="identity",position="identity")
## remove identity to make them stackable. one after the other


but it looks like that ggplot orders my xlabel based on the alphabetical order of the words in other terms the number that is visible in my Both structure and I highlight with ** **.



Why my ordering is not enough? Why ggplot still plots the labels alphabetically?










share|improve this question















marked as duplicate by hrbrmstr ggplot2
Users with the  ggplot2 badge can single-handedly close ggplot2 questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 13:13


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • that highly upvoted absolute duplicate is hit #1 on google for ggplot2 order barplot

    – hrbrmstr
    Nov 15 '18 at 13:14











  • they discuss on factor ordering which based on my understanding is not similar to the data.frame. I am still new to R But I searched for those solutions and I could not apply any of these suggestions I got.

    – Alex
    Nov 15 '18 at 15:41
















0
















This question already has an answer here:




  • Order Bars in ggplot2 bar graph

    12 answers




I am trying to make a barplot that is not stackable and so far I was able to go quite far.



Since I have found out that ggplot plots the data.frame as is, without ordering I have ordered my data.frame before feeding it to ggplot. Where I ordered it based on word occurences



Both<-Both[order(Both$occurence,decreasing=TRUE),]


The structure looks like now as



 str(Both)
'data.frame': 82 obs. of 3 variables:
$ word : Factor w/ 6382 levels "a","abcc","abda",..: 1595 1994
$ occurence: int 4171 3004 2145 1821 1798 1745 1614 1539 1531 1349 ...
$ sentiment: num 0 0 0 0 0 0 0 0 0 0 ...


Variable



> Both
word occurence sentiment
**19** i 4171 0
**12** m 3004 0
**34** true 2145 0
**36** kubelet 1821 1
**18** normal 1798 1


Plot



ggplot(data=Both,aes(x=word,y=occurence,factor(sentiment),fill=factor(sentiment)))+
coord_flip()+geom_bar(stat="identity",position="identity")
## remove identity to make them stackable. one after the other


but it looks like that ggplot orders my xlabel based on the alphabetical order of the words in other terms the number that is visible in my Both structure and I highlight with ** **.



Why my ordering is not enough? Why ggplot still plots the labels alphabetically?










share|improve this question















marked as duplicate by hrbrmstr ggplot2
Users with the  ggplot2 badge can single-handedly close ggplot2 questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 13:13


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • that highly upvoted absolute duplicate is hit #1 on google for ggplot2 order barplot

    – hrbrmstr
    Nov 15 '18 at 13:14











  • they discuss on factor ordering which based on my understanding is not similar to the data.frame. I am still new to R But I searched for those solutions and I could not apply any of these suggestions I got.

    – Alex
    Nov 15 '18 at 15:41














0












0








0









This question already has an answer here:




  • Order Bars in ggplot2 bar graph

    12 answers




I am trying to make a barplot that is not stackable and so far I was able to go quite far.



Since I have found out that ggplot plots the data.frame as is, without ordering I have ordered my data.frame before feeding it to ggplot. Where I ordered it based on word occurences



Both<-Both[order(Both$occurence,decreasing=TRUE),]


The structure looks like now as



 str(Both)
'data.frame': 82 obs. of 3 variables:
$ word : Factor w/ 6382 levels "a","abcc","abda",..: 1595 1994
$ occurence: int 4171 3004 2145 1821 1798 1745 1614 1539 1531 1349 ...
$ sentiment: num 0 0 0 0 0 0 0 0 0 0 ...


Variable



> Both
word occurence sentiment
**19** i 4171 0
**12** m 3004 0
**34** true 2145 0
**36** kubelet 1821 1
**18** normal 1798 1


Plot



ggplot(data=Both,aes(x=word,y=occurence,factor(sentiment),fill=factor(sentiment)))+
coord_flip()+geom_bar(stat="identity",position="identity")
## remove identity to make them stackable. one after the other


but it looks like that ggplot orders my xlabel based on the alphabetical order of the words in other terms the number that is visible in my Both structure and I highlight with ** **.



Why my ordering is not enough? Why ggplot still plots the labels alphabetically?










share|improve this question

















This question already has an answer here:




  • Order Bars in ggplot2 bar graph

    12 answers




I am trying to make a barplot that is not stackable and so far I was able to go quite far.



Since I have found out that ggplot plots the data.frame as is, without ordering I have ordered my data.frame before feeding it to ggplot. Where I ordered it based on word occurences



Both<-Both[order(Both$occurence,decreasing=TRUE),]


The structure looks like now as



 str(Both)
'data.frame': 82 obs. of 3 variables:
$ word : Factor w/ 6382 levels "a","abcc","abda",..: 1595 1994
$ occurence: int 4171 3004 2145 1821 1798 1745 1614 1539 1531 1349 ...
$ sentiment: num 0 0 0 0 0 0 0 0 0 0 ...


Variable



> Both
word occurence sentiment
**19** i 4171 0
**12** m 3004 0
**34** true 2145 0
**36** kubelet 1821 1
**18** normal 1798 1


Plot



ggplot(data=Both,aes(x=word,y=occurence,factor(sentiment),fill=factor(sentiment)))+
coord_flip()+geom_bar(stat="identity",position="identity")
## remove identity to make them stackable. one after the other


but it looks like that ggplot orders my xlabel based on the alphabetical order of the words in other terms the number that is visible in my Both structure and I highlight with ** **.



Why my ordering is not enough? Why ggplot still plots the labels alphabetically?





This question already has an answer here:




  • Order Bars in ggplot2 bar graph

    12 answers








r ggplot2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 13:36









Mikhail Kholodkov

4,96152850




4,96152850










asked Nov 15 '18 at 12:33









AlexAlex

103




103




marked as duplicate by hrbrmstr ggplot2
Users with the  ggplot2 badge can single-handedly close ggplot2 questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 13:13


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by hrbrmstr ggplot2
Users with the  ggplot2 badge can single-handedly close ggplot2 questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 13:13


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • that highly upvoted absolute duplicate is hit #1 on google for ggplot2 order barplot

    – hrbrmstr
    Nov 15 '18 at 13:14











  • they discuss on factor ordering which based on my understanding is not similar to the data.frame. I am still new to R But I searched for those solutions and I could not apply any of these suggestions I got.

    – Alex
    Nov 15 '18 at 15:41



















  • that highly upvoted absolute duplicate is hit #1 on google for ggplot2 order barplot

    – hrbrmstr
    Nov 15 '18 at 13:14











  • they discuss on factor ordering which based on my understanding is not similar to the data.frame. I am still new to R But I searched for those solutions and I could not apply any of these suggestions I got.

    – Alex
    Nov 15 '18 at 15:41

















that highly upvoted absolute duplicate is hit #1 on google for ggplot2 order barplot

– hrbrmstr
Nov 15 '18 at 13:14





that highly upvoted absolute duplicate is hit #1 on google for ggplot2 order barplot

– hrbrmstr
Nov 15 '18 at 13:14













they discuss on factor ordering which based on my understanding is not similar to the data.frame. I am still new to R But I searched for those solutions and I could not apply any of these suggestions I got.

– Alex
Nov 15 '18 at 15:41





they discuss on factor ordering which based on my understanding is not similar to the data.frame. I am still new to R But I searched for those solutions and I could not apply any of these suggestions I got.

– Alex
Nov 15 '18 at 15:41












1 Answer
1






active

oldest

votes


















0














You need to re-order the factor levels from your word column. The order of the levels of that factor will be used to order the x axis.



Use the function factor for example.



require(ggplot2)

df <- data.frame(word = as.factor(c('a','b','c')),
cnt = c(10,20,15))

ggplot(df, aes(x = word, y = cnt)) +
geom_bar(stat = 'identity')

df$word <- factor(df$word, levels = c('b','c','a'))

ggplot(df, aes(x = word, y = cnt)) +
geom_bar(stat = 'identity')





share|improve this answer
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You need to re-order the factor levels from your word column. The order of the levels of that factor will be used to order the x axis.



    Use the function factor for example.



    require(ggplot2)

    df <- data.frame(word = as.factor(c('a','b','c')),
    cnt = c(10,20,15))

    ggplot(df, aes(x = word, y = cnt)) +
    geom_bar(stat = 'identity')

    df$word <- factor(df$word, levels = c('b','c','a'))

    ggplot(df, aes(x = word, y = cnt)) +
    geom_bar(stat = 'identity')





    share|improve this answer






























      0














      You need to re-order the factor levels from your word column. The order of the levels of that factor will be used to order the x axis.



      Use the function factor for example.



      require(ggplot2)

      df <- data.frame(word = as.factor(c('a','b','c')),
      cnt = c(10,20,15))

      ggplot(df, aes(x = word, y = cnt)) +
      geom_bar(stat = 'identity')

      df$word <- factor(df$word, levels = c('b','c','a'))

      ggplot(df, aes(x = word, y = cnt)) +
      geom_bar(stat = 'identity')





      share|improve this answer




























        0












        0








        0







        You need to re-order the factor levels from your word column. The order of the levels of that factor will be used to order the x axis.



        Use the function factor for example.



        require(ggplot2)

        df <- data.frame(word = as.factor(c('a','b','c')),
        cnt = c(10,20,15))

        ggplot(df, aes(x = word, y = cnt)) +
        geom_bar(stat = 'identity')

        df$word <- factor(df$word, levels = c('b','c','a'))

        ggplot(df, aes(x = word, y = cnt)) +
        geom_bar(stat = 'identity')





        share|improve this answer















        You need to re-order the factor levels from your word column. The order of the levels of that factor will be used to order the x axis.



        Use the function factor for example.



        require(ggplot2)

        df <- data.frame(word = as.factor(c('a','b','c')),
        cnt = c(10,20,15))

        ggplot(df, aes(x = word, y = cnt)) +
        geom_bar(stat = 'identity')

        df$word <- factor(df$word, levels = c('b','c','a'))

        ggplot(df, aes(x = word, y = cnt)) +
        geom_bar(stat = 'identity')






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 13:03

























        answered Nov 15 '18 at 12:55









        Wietze314Wietze314

        4,10311128




        4,10311128

















            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine