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 8162327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:40:56+00:00 2026-06-06T18:40:56+00:00

Is there a way to sum data with ggplot2 ? I want to do

  • 0

Is there a way to sum data with ggplot2 ?

I want to do a bubble map with the size depending of the sum of z.

Currently I’m doing something like

dd <- ddply(d, .(x,y), transform, z=sum(z))
qplot(x,y, data=dd, size=z)

But I feel I’m writing the same thing twice, I would like to be able to write something

qplot(x,y, data=dd, size=sum(z))

I had a look at stat_sum and stat_summmary but I’m not sure they are appropriate either.

Is it possible to it with ggplot2 ? If not, what would be best way to write those 2 lines.

  • 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-06-06T18:40:57+00:00Added an answer on June 6, 2026 at 6:40 pm

    It can be done using stat_sum within ggplot2. By default, the dot size represents proportions. To get dot size to represent counts, use size = ..n.. as an aesthetic. Counts (and proportions) by a third variable can be obtained by weighting by the third variable (weight = cost) as an aesthetic. Some examples, but first, some data.

    library(ggplot2)
    set.seed = 321
    # Generate somme data
    df <- expand.grid(x = seq(1:5), y = seq(1:5), KEEP.OUT.ATTRS = FALSE)
    df$Count = sample(1:25, 25, replace = F)
    library(plyr)
    new <- dlply(df, .(Count), function(data) matrix(rep(matrix(c(data$x, data$y), ncol = 2), data$Count), byrow = TRUE, ncol = 2))
    df2 <- data.frame(do.call(rbind, new))
    df2$cost <- 1:325
    

    The data contains units categorised according to two factors: X1 and X2; and a third variable which is the cost of each unit.

    Plot 1: Plots the proportion of elements at each X1 – X2 combination. group=1 tells ggplot to calculate proportions out of the total number of units in the data frame.

    ggplot(df2, aes(factor(X1), factor(X2))) + 
      stat_sum(aes(group = 1))
    

    enter image description here

    Plot 2: Plots the number of elements at each X1 – X2 combination.

    ggplot(df2, aes(factor(X1), factor(X2))) + 
      stat_sum(aes(size = ..n..))
    

    enter image description here

    Plot 3: Plots the cost of the elements at each X1 – X2 combination, that is weight by the third variable.

    ggplot(df2, aes(x=factor(X1), y=factor(X2))) + 
         stat_sum(aes(group = 1, weight = cost, size = ..n..)) 
    

    enter image description here

    Plot 4: Plots the proportion of the total cost of all elements in the data frame at each X1 – X2 combination

    ggplot(df2, aes(x=factor(X1), y=factor(X2))) + 
         stat_sum(aes(group = 1, weight = cost)) 
    

    enter image description here

    Plot 5: Plots proportions, but instead of the proportion being out of the total cost across all elements in the data frame, the proportion is out of the cost for elements within each category of X1. That is, within each X1 category, where does the major cost for X2 units occur?

    ggplot(df2, aes(x=factor(X1), y=factor(X2))) + 
         stat_sum(aes(group = X1, weight = cost)) 
    

    enter image description here

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

Sidebar

Related Questions

is there any way how to create something like data.frame object in R that
From a data frame, is there a easy way to aggregate ( sum ,
Is there a way to center text in the cell here? grid.Column(TotalSum, Total Sum,
is there way how to get name ov event from Lambda expression like with
Is there any way to get data from cassandra through phpcassa using a clauses?
1) Is there a way to get the sum of an integer column using
I have a highcharts line graph that shows sales data. Is there a way
Is there some way to sum up two different tweet number from two different
What is the best way to maintain a cumulative sum of a particular data
Is there way to get file from windows xp command prompt? I tried to

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.