Bubble chart with ggplot2

I want to print a bubble chart in R. The problem I am facing is that both x and yy axes are discrete. Theoretically, this means that many data points (bubbles) end at the same coordinate. I would prefer them to scatter around the data point, but still within the quadrant, which clearly shows that the bubble refers to the corresponding x / y coordinate.

I think this is best demonstrated with a small example. The following code should highlight the problem:

# Example require(ggplot2) zz <- textConnection("Row PowerSource ProductSegment Price Model ManufacturingLocation Quantity 1 High SegmentA Low ModA LocationA 5000 2 Low SegmentB Low ModB LocationB 25000 3 High SegmentC Low ModC LocationC 15000 4 Low SegmentD High ModD LocationD 30000 5 High SegmentE High ModE LocationA 2500 6 Low SegmentA Low ModF LocationB 110000 7 High SegmentB Low ModG LocationC 20000 8 Low SegmentC Low ModH LocationD 3500 9 High SegmentD Low ModI LocationA 65500 10 Low SegmentE Low ModJ LocationB 145000 11 High SegmentA Low ModK LocationC 15000 12 Low SegmentB Low ModL LocationD 5000 13 High SegmentC Low ModM LocationA 26000 14 Low SegmentD Low ModN LocationB 14000 15 High SegmentE Mid ModO LocationC 75000 16 Low SegmentA High ModP LocationD 33000 17 High SegmentB Low ModQ LocationA 14000 18 Low SegmentC Mid ModR LocationB 33000 19 High SegmentD High ModS LocationC 95000 20 Low SegmentE Low ModT LocationD 4000 ") df2 <- read.table(zz, header= TRUE) close(zz) df2 ggplot(df2, aes(x = ManufacturingLocation, y = PowerSource, label = Model)) + geom_point(aes(size = Quantity, colour = Price)) + geom_text(hjust = 1, size = 2) + scale_size(range = c(1,15)) + theme_bw() 

How can I scatter bubbles a bit to show the different products in each category and their quantity?

(Sorry, I can’t add an image at the moment because of too few reputations)

enter image description here

+7
r ggplot2 bubble-chart
source share
1 answer

As Tom Martens noted, corrective alpha can show any overlap. The following alpha level:

 ggplot(df2, aes(x = ManufacturingLocation, y = PowerSource, label = Model)) + geom_point(aes(size = Quantity, colour = Price, alpha=.02)) + geom_text(hjust = 1, size = 2) + scale_size(range = c(1,15)) + theme_bw() 

leads to:

enter image description here

Using geom_jitter instead of a dot in combination with alpha:

 ggplot(df2, aes(x = ManufacturingLocation, y = PowerSource, label = Model)) + geom_jitter(aes(size = Quantity, colour = Price, alpha=.02)) + geom_text(hjust = 1, size = 2) + scale_size(range = c(1,15)) + theme_bw() 

produces the following:

enter image description here

EDIT: To avoid an artifact in the legend, alpha should be placed outside aes:

 ggplot(df2, aes(x = ManufacturingLocation, y = PowerSource, label = Model)) + geom_point(aes(size = Quantity, colour = Price),alpha=.2) + geom_text(hjust = 1, size = 2) + scale_size(range = c(1,15)) + theme_bw() 

as a result of:

enter image description here

and

  ggplot(df2, aes(x = ManufacturingLocation, y = PowerSource, label = Model)) + geom_jitter(aes(size = Quantity, colour = Price),alpha=.2) + geom_text(hjust = 1, size = 2) + scale_size(range = c(1,15)) + theme_bw() 

as a result of:

enter image description here

EDIT 2: So, it took a while to figure out.

I followed the example to which I referred in my comment. I have adjusted the code according to your needs. First of all, I created the jitter values ​​off the graph:

 df2$JitCoOr <- jitter(as.numeric(factor(df2$ManufacturingLocation))) df2$JitCoOrPow <- jitter(as.numeric(factor(df2$PowerSource))) 

Then I called these values ​​in geom_point and geom_text x and y coordinates inside aes. This worked by flailing the bubbles and corresponding shortcuts to them. However, he messed up the x and y axis labels, so I replaced them, as you can see in scale_x_discrete and scale_y_discrete. Here is the chart code:

 ggplot(df2, aes(x = ManufacturingLocation, y = PowerSource)) + geom_point(data=df2,aes(x=JitCoOr, y=JitCoOrPow,size = Quantity, colour = Price), alpha=.5)+ geom_text(data=df2,aes(x=JitCoOr, y=JitCoOrPow,label=Model)) + scale_size(range = c(1,50)) + scale_y_discrete(breaks =1:3 , labels=c("Low","High"," "), limits = c(1, 2))+ scale_x_discrete(breaks =1:4 , labels=c("Location A","Location B","Location C","Location D"), limits = c(1,2,3,4))+ theme_bw() 

What gives this conclusion:

enter image description here

You can adjust the size of the bubbles using scale_size above. I exported this image with dimensions of 1000 * 800.

Regarding your request for adding borders, I think this is not necessary. This is very clear in this plot where there are bubbles, and I think the borders will make it a little ugly. However, if you still want boundaries, I will see what I can do.

+14
source share

All Articles