Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7444477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:31:22+00:00 2026-05-29T11:31:22+00:00

I’d like to create a bar chart using factors and more than two variables!

  • 0

I’d like to create a bar chart using factors and more than two variables! My data looks like this:

     Var1 Var2 ... VarN Factor1 Factor2
Obs1  1-5 1-5  ... 1-5     
Obs2  1-5 1-5  ... ...
Obs3  ... ...  ... ...

Each datapoint is a likert item ranging from 1-5

Plotting total sums using a dichotomized version (every item above 4 is a one, else 0)

I converted the data using this

MyDataFrame = dichotomize(MyDataFrame,>=4)
p <- colSums(MyDataFrame)
p <- data.frame(names(p),p)
names(p) <- c("var","value")
ggplot(p,aes(var,value)) + geom_bar() + coord_flip()

enter image description here

Doing this i loose the information provided by factor1 etc, i’d like to use stacking in order to visualize from which group of people the rating came

Is there a elegant solution to this problem? I read about using reshape to melt the data and then applying ggplot?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:31:23+00:00Added an answer on May 29, 2026 at 11:31 am

    I would suggest the following: use one of your factor for stacking, the other one for faceting. You can remove position="fill" to geom_bar() to use counts instead of standardized values.

    my.df <- data.frame(replicate(10, sample(1:5, 100, rep=TRUE)), 
                        F1=gl(4, 5, 100, labels=letters[1:4]), 
                        F2=gl(2, 50, labels=c("+","-")))
    my.df[,1:10] <- apply(my.df[,1:10], 2, function(x) ifelse(x>4, 1, 0))
    library(reshape2)
    my.df.melt <- melt(my.df)
    library(plyr)
    res <- ddply(my.df.melt, c("F1","F2","variable"), summarize, sum=sum(value))
    library(ggplot2)
    ggplot(res, aes(y=sum, x=variable, fill=F1)) +
       geom_bar(stat="identity", position="fill") + 
       coord_flip() +
       facet_grid(. ~ F2) + 
       ylab("Percent") + xlab("Item")
    

    enter image description here

    In the above picture, I displayed observed frequencies of ‘1’ (value above 4 on the Likert scale) for each combination of F1 (four levels) and F2 (two levels), where there are either 10 or 15 observations:

    > xtabs(~ F1 + F2, data=my.df)
       F2
    F1   +  -
      a 15 10
      b 15 10
      c 10 15
      d 10 15
    

    I then computed conditional item sum scores with ddply,† using a ‘melted’ version of the original data.frame. I believe the remaining graphical commands are highly configurable, depending on what kind of information you want to display.

    † In this simplified case, the ddply instruction is equivalent to with(my.df.melt, aggregate(value, list(F1=F1, F2=F2, variable=variable), sum)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.