Little useless-useful R functions – Colourful ggplot line graphs

How about some colours in line graph?

Or even more wacky? Nevertheless, let’s create a function that generates some sample “mocked” data and draws a line chart:

#The function
Colourful_graph <- function(n, x, y){
  df <- data.frame(x=x, y=y, col=n)
  for (i in 1:n){
    #get last x,y
    lastx <- tail(df$x, 1)
    lasty <- tail(df$y, 1)
    col <- sample(1:i, 1, replace = T)
    if (i %% 10 == 0) {
      xx <- runif(1, 0.0, 1.0) + lastx
      yy <- runif(1, 0.0, 1.0) - lasty
    } else  {
    xx <- runif(1, 0.0, 1.0) + lastx
    yy <- runif(1, 0.0, 1.0) - lasty
    }
    # change: col=i for rainbow colours
    df <- rbind(df, c(x=xx, y=yy, col=col)) 
    
  }
  fake <- df
  
  brewColours <- as.integer(length(fake$col))
  ColourfulColours <- colorRampPalette(brewer.pal(8, "Set2"))(brewColours)
  
  # show faked graph
  ggplot(fake, aes(x, y, color = factor(col))) +
    geom_line(color= ColourfulColours) +
    theme(legend.position = "none") +
    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
          panel.background = element_blank(), axis.line = element_blank(), text=element_blank(), line = element_blank())  
  
}

And run all together with three parameters: n – number of data points and x,y for starting point.

#create colourful graph
Colourful_graph(500,0.4,0.3)

In addition, adding some colorbrewer functionalities can also make line chart random with colours. This following section can be added to function to alternate between the colour palettes:

  #brewer sample 8 color palette
  nn <- as.data.frame(brewer.pal.info)
  nn$names <- row.names.data.frame(nn)
  nn8 <- subset(nn, maxcolors==8, select=c(names))
  
  
  brewColours <- as.integer(length(fake$col))
  #ColourfulColours <- colorRampPalette(brewer.pal(8, "Paired"))(brewColours)
  ColourfulColours <- colorRampPalette(brewer.pal(8, sample(nn8$names,1,T)))(brewColours)

Couple of more faked dataset line charts:

As always, code is available in at the Github in same Useless_R_function repository.

Happy R-coding!

Tagged with: , , , , ,
Posted in Uncategorized, Useless R functions
2 comments on “Little useless-useful R functions – Colourful ggplot line graphs
  1. […] Tomaz Kastrun has fun with colors: […]

    Like

  2. […] by data_admin [This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page […]

    Like

Leave a comment

Follow TomazTsql on WordPress.com
Programs I Use: SQL Search
Programs I Use: R Studio
Programs I Use: Plan Explorer
Rdeči Noski – Charity

Rdeči noski

100% of donations made here go to charity, no deductions, no fees. For CLOWNDOCTORS - encouraging more joy and happiness to children staying in hospitals (http://www.rednoses.eu/red-noses-organisations/slovenia/)

€2.00

Top SQL Server Bloggers 2018
TomazTsql

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Discover WordPress

A daily selection of the best content published on WordPress, collected for you by humans who love to read.

Revolutions

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Reeves Smith's SQL & BI Blog

A blog about SQL Server and the Microsoft Business Intelligence stack with some random Non-Microsoft tools thrown in for good measure.

SQL Server

for Application Developers

Business Analytics 3.0

Data Driven Business Models

SQL Database Engine Blog

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Search Msdn

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Data Until I Die!

Data for Life :)

Paul Turley's SQL Server BI Blog

sharing my experiences with the Microsoft data platform, SQL Server BI, Data Modeling, SSAS Design, Power Pivot, Power BI, SSRS Advanced Design, Power BI, Dashboards & Visualization since 2009

Grant Fritchey

Intimidating Databases and Code

Madhivanan's SQL blog

A modern business theme

Alessandro Alpi's Blog

DevOps could be the disease you die with, but don’t die of.

Paul te Braak

Business Intelligence Blog

Sql Insane Asylum (A Blog by Pat Wright)

Information about SQL (PostgreSQL & SQL Server) from the Asylum.

Gareth's Blog

A blog about Life, SQL & Everything ...

SQLPam's Blog

Life changes fast and this is where I occasionally take time to ponder what I have learned and experienced. A lot of focus will be on SQL and the SQL community – but life varies.

William Durkin

William Durkin a blog on SQL Server, Replication, Performance Tuning and whatever else.

$hell Your Experience !!!

As aventuras de um DBA usando o Poder do $hell

Design a site like this with WordPress.com
Get started