0

I have some survey data where I have the number of times a factor gets mentioned during interviews and then the percentage of how many each of these mentions are either positive, neutral or negative. I would like to display this in a circular stacked barchart, with each segment of the plot showing the percentage of the responses for each factor that are positive/neutral/negative and the width of each bar/segment representing the number of responses relating to each factor. I have been able to achieve this using ggplot and coord_polar but the problem I'm having is that each factor around the circle is evenly spaced leaving a gap between the bars. But I was wondering if it is possible to change the position of these factors around the circle resulting in a 'full' pie chart with different sized segments but without gaps between them?

This is what I've done so far;

data <- data.frame(Factor = c("One", "Two", "Three", "Four", "Five", "Six"),  
                   responses = c(23,16,8,16,17,20), 
                   positive = c(65.2,56.3,25,68.8,76.4,50),
                   neutral =c(30.4,12.5,25,12.5,17.6,20),
                   negative=c(4.3,31.3,50,18.8,5.9,30))

data2 <-melt(data, id=c("Factor","responses"))

data2$Factor <- factor(data2$Factor, levels = c("One", "Two", "Three", 
                                                "Four", "Five","Six"))


ggplot(data2) +
  geom_bar(aes(Factor, value, fill=variable, width=responses/25), stat="identity", alpha=0.5) +
  coord_polar() 

Which produced this;

enter image description here

But I'd like something closer to this, in terms of the width of the segments filling the circle, but then to have each segment stacked as above;

enter image description here

Any help would be greatly appreciated! Thanks

J. Cee
  • 49
  • 5
  • This plot would be more informative without warping it into a circle. I'm at least somewhat okay with simple pie charts, but segmenting radially as well is a complete mess. – alan ocallaghan Feb 06 '20 at 12:50

2 Answers2

1

Use width. You're setting it in aes() and ignoring the warning that it's unused.

library("reshape2")
library("ggplot2")

data <- data.frame(Factor = c("One", "Two", "Three", "Four", "Five", "Six"),  
                   responses = c(23,16,8,16,17,20), 
                   positive = c(65.2,56.3,25,68.8,76.4,50),
                   neutral =c(30.4,12.5,25,12.5,17.6,20),
                   negative=c(4.3,31.3,50,18.8,5.9,30))

data2 <-melt(data, id=c("Factor","responses"))

data2$Factor <- factor(data2$Factor, levels = c("One", "Two", "Three", 
                                                "Four", "Five","Six"))


ggplot(data2) +
  geom_bar(aes(Factor, value, fill=variable), width = 1, stat="identity", alpha=0.5) +
  coord_polar() 

This is a better version of the same plot.


ggplot(data2) +
  geom_bar(aes(Factor, value, fill=variable), stat="identity", alpha=0.5)

alan ocallaghan
  • 3,116
  • 17
  • 37
  • The only problem with the first plot there is that although all of the circle is full each of the segments is the same size, but I wanted to know if you could change how the factors are displayed on the plot so that they are not evenly spaced and the size of each segment varies depending on the number of responses recorded. But as you say it may be simpler as a bar plot with different width bars, which I can produce. Thanks though. – J. Cee Feb 06 '20 at 13:41
  • `ggplot(data2) + geom_bar(aes(Factor, value, fill=variable, width=responses/25), stat="identity", alpha=0.5)` – J. Cee Feb 06 '20 at 13:47
  • 1
    It's fundamentally a bad plot as a radially segmented piechart. – alan ocallaghan Feb 06 '20 at 13:59
  • 2
    To clarify, @alanocallaghan is saying the plot is bad because the radial segmentation means that the category that is furthest toward the outside of the circle has exaggeratedly large area so it looks like there are a lot higher proportion "positive" responses than there really are. The stacked bar plot he made shows things without distorting the relative proportions. – qdread Feb 06 '20 at 14:14
  • Thanks I do understand and appreciate the flaws with the radial segmentation and I get why the stacked bar chart better represents that data (I was just still curious if it was possible to alter the positioning of the segments). But yes I'll stick with the bar chart for now and adjust the width of the bars to show the number of responses they represent. – J. Cee Feb 06 '20 at 14:20
0

Moving away from the segmented pie chart I used this example R ggplot stacked geom_rect to create a stacked bar chart where the width of the columns represented the total number of mentions of each factor.

enter image description here

J. Cee
  • 49
  • 5