Try to search your question here, if you can't find : Ask Any Question Now ?

ggplot: Grouped, adjacent bars of variable width

HomeCategory: stackoverflowggplot: Grouped, adjacent bars of variable width
Avatarrupesh asked 5 months ago

I’m trying to produce barplot in which the width and height of the bars both convey information: the height is the number of hours spent on a task, the widths respectively indicate the perceived aptitude and importance associated with the task. I’ve managed to produce this monstrosity:

Nested Bars Plot

It’s functional but horrible. I would really like to place the bars alongside one another (rather than overlaying them), so that each activity is represented by two touching bars of the same height (=time spent) but different widths and colors. I’ve been trying to to pass a width argument to this plot:

Plot w/ dodged bars

but setting ‘aes(width = widthVariable)’ gives me overlapping bars (similar to the first image) and the following warning message:

“position_dodge requires non-overlapping x intervals”.

Is there a way of grouping my bars by activity, displaying them adjacently and varying their widths?

Here’s a bit of the df I’m using:

molten = data.frame(Activity = rep(c('Administration','Working with Colleagues','Use of Social Media','Leadership Role'),2),
            variable = c(rep('Importance',4),rep('Competence',4)),
            value = rep(c(3.02,1.71,2.39,3.32),2),
            width = c(3.48,3.52,4.01,2.98,

The second plot is this:

 ggplot(molten, aes(x=Activity, y=value, fill=variable)) + geom_bar(stat='identity',position = 'dodge')

and the first in something like this:

 ggplot(molten, aes(x=Activity, y=value, fill=variable)) + geom_bar(stat='identity',aes(width = width/10))

Although I actually made it using slightly simpler dataframe, which I melt()-ed into the one above.

1 Answers
Best Answer
Avatarnaveen answered 5 months ago
Your Answer

7 + 19 =

Popular Tags

WP Facebook Auto Publish Powered By :