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

  • Home
  • SEARCH
  • 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 9022917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:38:03+00:00 2026-06-16T05:38:03+00:00

I often plot graphs in GNU R / ggplot for some measurements related to

  • 0

I often plot graphs in GNU R / ggplot for some measurements related to bytes. The builtin axis labels are either plain numbers or scientific notation, ie 1 Megabyte = 1e6. I would like SI prefixes (Kilo = 1e3, Mega=1e6, Giga=1e9, etc) instead, i.e. axis should be labelled 1.5K, 5K, 1M, 150M, 4G etc.

I currently use the following code:

si_num <- function (x) {

  if (!is.na(x)) {
    if (x > 1e6) { 
      chrs <- strsplit(format(x, scientific=12), split="")[[1]];
      rem <- chrs[seq(1,length(chrs)-6)];
      rem <- append(rem, "M");
    }

    else if (x > 1e3) { 
      chrs <- strsplit(format(x, scientific=12), split="")[[1]];
      rem <- chrs[seq(1,length(chrs)-3)];
      rem <- append(rem, "K");
    }
    else {
      return(x);
    }

    return(paste(rem, sep="", collapse=""));
  }
  else return(NA);
} 

si_vec <- function(x) {
  sapply(x, FUN=si_num);
}

library("ggplot2");

bytes=2^seq(0,20) + rnorm(21, 4, 2);
time=bytes/(1e4 + rnorm(21, 100, 3)) + 8;

my_data = data.frame(time, bytes);

p <- ggplot(data=my_data, aes(x=bytes, y=time)) +
     geom_point() +
     geom_line() +
     scale_x_log10("Message Size [Byte]", labels=si_vec) +
     scale_y_continuous("Round-Trip-Time [us]");
p;

I would like to know if this solution can be improved, as my one requires a lot of boilerplate code in every graph.

  • 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-16T05:38:04+00:00Added an answer on June 16, 2026 at 5:38 am

    I used library("sos"); findFn("{SI prefix}") to find the sitools package.

    Construct data:

    bytes <- 2^seq(0,20) + rnorm(21, 4, 2)
    time <- bytes/(1e4 + rnorm(21, 100, 3)) + 8
    my_data <- data.frame(time, bytes)
    

    Load packages:

    library("sitools")
    library("ggplot2")    
    

    Create the plot:

    (p <- ggplot(data=my_data, aes(x=bytes, y=time)) +
         geom_point() +
         geom_line() +
         scale_x_log10("Message Size [Byte]", labels=f2si) +
         scale_y_continuous("Round-Trip-Time [us]"))
    

    I’m not sure how this compares to your function, but at least someone else went to the trouble of writing it …

    I modified your code style a little bit — semicolons at the ends of lines are harmless but are generally the sign of a MATLAB or C coder …

    edit: I initially defined a generic formatting function

    si_format <- function(...) {
        function(x) f2si(x,...)
    }
    

    following the format of (e.g) scales::comma_format, but that seems unnecessary in this case — just part of the deeper ggplot2 magic that I don’t fully understand.

    The OP’s code gives what seems to me to be not quite the right answer: the rightmost axis tick is “1000K” rather than “1M” — this can be fixed by changing the >1e6 test to >=1e6. On the other hand, f2si uses lower-case k — I don’t know whether K is wanted (wrapping the results in toupper() could fix this).

    OP results (si_vec):

    enter image description here

    My results (f2si):

    enter image description here

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

Sidebar

Related Questions

I often plot graphs in gnuplot prompt shell, like this: gunuplot> plot sin(x) with
Often times I write some SQL like this: string sql = @ -- Multi-line
Often when writing PHP I'll have it output some HTML like this - echo
Often we have to use some of the methods which don't return anything useful.
Often in programming, it is a very common requirement that some piece of functionality
Often I find myself with some text where I want to exclude a certain
In my work, I frequently display scatterplots using text labels. That's meant a plot()
Often I could use some tools to statically analyze my code in order to
Often I declare classes to wrap a single Plain Old Data value; simple classes
Often I need to temporary comment some code, but there are situations like the

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.