<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/burtins-antibiotics-visualization-in-plotly-and-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://moderndata.plot.ly/burtins-antibiotics-visualization-in-plotly-and-r/"> R – Modern Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In this post, we’ll try to re-create Burtin’s antibiotics visualization using Plotly. The post follows Mike Bostock’s original re-creation in Protovis. See <a href="http://mbostock.github.io/protovis/ex/antibiotics-burtin.html">here</a>. </p>

<pre>library(plotly)
library(reshape2)
library(dplyr)

# Data
df &lt;- read.csv(&quot;https://cdn.rawgit.com/plotly/datasets/5360f5cd/Antibiotics.csv&quot;, stringsAsFactors = F)
N &lt;- nrow(df)

# Melting for easier use later on
df$Seq &lt;- 1:N
df &lt;- melt(df, id.vars = c(&quot;Bacteria&quot;, &quot;Gram&quot;, &quot;Seq&quot;)) %>% arrange(Seq)

# Angle generation
theta.start &lt;- (5/2)*pi - pi/10
theta.end &lt;- pi/2 + pi/10
theta.range &lt;- seq(theta.start, theta.end, length.out = N * 4 - 1)
dtheta &lt;- diff(theta.range)[1]

# Fine adjustment for larger ticks (black)
inc &lt;- 0.04

# Angles for larger ticks
big_ticks &lt;- theta.range[1:length(theta.range) %% 4 == 0]
big_ticks &lt;- c(theta.range[1] - dtheta, big_ticks, theta.range[length(theta.range)] + dtheta)

# Angles for smaller ticks
small_ticks &lt;- theta.range[1:length(theta.range) %% 4 != 0]

# Set inner and outer radii
inner.radius &lt;- 0.3
outer.radius &lt;- 1

# Set colors
cols &lt;- c(&quot;#0a3264&quot;,&quot;#c84632&quot;,&quot;#000000&quot;)
pos_col &lt;- &quot;rgba(174, 174, 184, .8)&quot;
neg_col &lt;- &quot;rgba(230, 130, 110, .8)&quot;

# Function to calculate radius given minimum inhibitory concentration
# Scaling function is sqrt(log(x))
radiusFUNC &lt;- function(x){
    min &lt;- sqrt(log(0.001 * 1e4))
    max &lt;- sqrt(log(1000 * 1e4))
    a &lt;- (outer.radius - inner.radius)/(min - max)
    b &lt;- inner.radius - a * max
    rad &lt;- a * sqrt(log(x * 1e4)) + b

    return(rad)
}

SEQ &lt;- function(start, by, length){
    vec &lt;- c()
    vec[1] &lt;- start

    for(i in 2:length){
        vec[i] &lt;- vec[i-1] + by
    }

    return(vec)
}

# Generate x and y coordinates for large ticks
radial_gridlines &lt;- data.frame(theta = big_ticks,
                               x = (inner.radius - inc) * cos(big_ticks),
                               y = (inner.radius - inc) * sin(big_ticks),
                               xend = (outer.radius + inc) * cos(big_ticks),
                               yend = (outer.radius + inc) * sin(big_ticks))

# Generate x and y coordinates for antibiotics
antibiotics &lt;- df
antibiotics$x &lt;- inner.radius * cos(small_ticks)
antibiotics$y &lt;- inner.radius * sin(small_ticks)
antibiotics$xend &lt;- radiusFUNC(df$value) * cos(small_ticks)
antibiotics$yend &lt;- radiusFUNC(df$value) * sin(small_ticks)
antibiotics$text &lt;- with(antibiotics,
                         paste(&quot;&lt;b>Bacteria:&lt;/b>&quot;, Bacteria, &quot;&lt;br>&quot;,
                               &quot;&lt;b>Antibiotic:&lt;/b>&quot;, variable, &quot;&lt;br>&quot;,
                               &quot;&lt;b>Min. conc:&lt;/b>&quot;, value, &quot;&lt;br>&quot;))

# Generate x and y coordinates for white circles (grid)
rad &lt;- c(100, 10, 1, 0.1, 0.01, 0.001)
rad &lt;- c(inner.radius, radiusFUNC(rad))
theta &lt;- seq(0, 2 * pi, length.out = 100)

circles &lt;- lapply(rad, function(x){
    x.coord &lt;- x * cos(theta)
    y.coord &lt;- x * sin(theta)

    return(data.frame(label = which(rad == x),
                      x = x.coord,
                      y = y.coord))
})

circles &lt;- do.call(rbind, lapply(circles, data.frame))

# Generate gram-negative polygon
theta &lt;- seq(big_ticks[1], big_ticks[10], length.out = 100)

gram.neg &lt;- data.frame(theta = theta,
                       x = inner.radius * cos(theta),
                       y = inner.radius * sin(theta))

theta &lt;- rev(theta)

gram.neg &lt;- rbind(gram.neg,
                  data.frame(theta = theta,
                             x = outer.radius * cos(theta),
                             y = outer.radius * sin(theta)))

# Generate gram-positive polygon
theta &lt;- seq(big_ticks[10], big_ticks[length(big_ticks)], length.out = 100)

gram.pos &lt;- data.frame(theta = theta,
                       x = inner.radius * cos(theta),
                       y = inner.radius * sin(theta))

theta &lt;- rev(theta)

gram.pos &lt;- rbind(gram.pos,
                  data.frame(theta = theta,
                             x = outer.radius * cos(theta),
                             y = outer.radius * sin(theta)))

# Text annotations - bacteria name
bacteria.df &lt;- data.frame(bacteria = unique(antibiotics$Bacteria),
                                theta = big_ticks[-length(big_ticks)] - 0.17,
                                stringsAsFactors = F)

bacteria.df$x &lt;- (outer.radius + inc) * cos(bacteria.df$theta)
bacteria.df$y &lt;- (outer.radius + inc) * sin(bacteria.df$theta)
bacteria.df$textangle &lt;- SEQ(-70, by = 21, length = nrow(bacteria.df))
bacteria.df$textangle[9:nrow(bacteria.df)] &lt;-
    (bacteria.df$textangle[9:nrow(bacteria.df)] - 90) - 90

bacteria.df &lt;- lapply(1:nrow(bacteria.df), function(x){

    list(x = outer.radius*cos(bacteria.df$theta[x]) ,
         y = outer.radius*sin(bacteria.df$theta[x]),
         xref = &quot;plot&quot;, yref = &quot;plot&quot;,
         xanchor = &quot;center&quot;, yanchor = &quot;middle&quot;,
         text = bacteria.df$bacteria[x],
         showarrow = F,
         font = list(size = 12, family = &quot;arial&quot;),
         textangle = bacteria.df$textangle[x])
})

# Title
bacteria.df[[17]] &lt;- list(x = 0, y = 1,
                          xref = &quot;paper&quot;, yref = &quot;paper&quot;,
                          xanchor = &quot;left&quot;, yanchor = &quot;top&quot;,
                          text = &quot;&lt;b>Burtin’s Antibiotics&lt;/b>&quot;,
                          showarrow = F,
                          font = list(size = 30, family = &quot;serif&quot;))

# Text annotations - scale
scale.annotate &lt;- data.frame(x = 0,
                             y = c(inner.radius, radiusFUNC(c(100, 10, 1, 0.1, 0.01, 0.001))),
                             scale = c(&quot;&quot;, &quot;100&quot;, &quot;10&quot;, &quot;1&quot;, &quot;0.1&quot;, &quot;0.01&quot;,&quot;0.001&quot;))

# Plot
p &lt;- plot_ly(x = ~x, y = ~y, xend = ~xend, yend = ~yend,
        hoverinfo = &quot;text&quot;,
        height = 900, width = 800) %>%

    # Gram negative sector
    add_polygons(data = gram.neg,
                 x = ~x, y = ~y,
                 line = list(color = &quot;transparent&quot;),
                 fillcolor = neg_col,
                 inherit = F,
                 hoverinfo = &quot;none&quot;) %>%

    # Gram positive sector
    add_polygons(data = gram.pos,
                 x = ~x, y = ~y,
                 line = list(color = &quot;transparent&quot;),
                 fillcolor = pos_col,
                 inherit = F,
                 hoverinfo = &quot;none&quot;) %>%

    # Antibiotics
    add_segments(data = antibiotics %>% filter(variable == &quot;Penicillin&quot;),
                 text = ~text,
                 line = list(color = cols[1], width = 7)) %>%

    add_segments(data = antibiotics %>% filter(variable == &quot;Streptomycin&quot;),
                 text = ~text,
                 line = list(color = cols[2], width = 7)) %>%

    add_segments(data = antibiotics %>% filter(variable == &quot;Neomycin&quot;),
                 text = ~text,
                 line = list(color = cols[3], width = 7)) %>%

    # Black large ticks
    add_segments(data = radial_gridlines, line = list(color = &quot;black&quot;, width = 2),
                 hoverinfo = &quot;none&quot;) %>%

    # White circles
    add_polygons(data = circles,
                 x = ~x, y = ~y,
                 group_by = ~label,
                 line = list(color = &quot;#eeeeee&quot;, width = 1),
                 fillcolor = &quot;transparent&quot;,
                 inherit = F,
                 hoverinfo = &quot;none&quot;) %>%

    # Scale labels
    add_text(data = scale.annotate, x = ~x, y = ~y, text = ~scale,
             inherit = F,
             textfont = list(size = 10, color = &quot;black&quot;),
             hoverinfo = &quot;none&quot;) %>%

    # Gram labels
    add_markers(x = c(-0.05, -0.05), y = c(-1.4, -1.5),
                marker = list(color = c(neg_col, pos_col), size = 15),
                inherit = F, hoverinfo = &quot;none&quot;) %>%

    add_text(x = c(0.15, 0.15), y = c(-1.4, -1.5), text = c(&quot;Gram Negative&quot;, &quot;Gram Positive&quot;),
             textfont = list(size = 10, color = &quot;black&quot;),
             inherit = F, hoverinfo = &quot;none&quot;) %>%

    # Antibiotic legend
    add_markers(x = c(-0.15, -0.15, -0.15), y = c(0.1, 0, -0.1),
                marker = list(color = c(cols), size = 15, symbol = &quot;square&quot;),
                inherit = F, hoverinfo = &quot;none&quot;) %>%

    add_text(x = c(0.05, 0.05, 0.05), y = c(0.1, 0, -0.1),
             text = c(&quot;Penicillin&quot;, &quot;Streptomycin&quot;, &quot;Neomycin&quot;),
             textfont = list(size = 10, color = &quot;black&quot;),
             inherit = F, hoverinfo = &quot;none&quot;) %>%


    layout(showlegend = F,
           xaxis = list(showgrid = F, zeroline = F, showticklabels = F, title = &quot;&quot;, domain = c(0.05, 0.95)),
           yaxis = list(showgrid = F, zeroline = F, showticklabels = F, title = &quot;&quot;, domain = c(0.05, 0.95)),
           plot_bgcolor = &quot;rgb(240, 225, 210)&quot;,
           paper_bgcolor = &quot;rgb(240, 225, 210)&quot;,

           # Annotations
           annotations = bacteria.df)
p</pre>

<p><iframe width="450" src="http://plot.ly/~riddhiman/415.embed"></iframe></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://moderndata.plot.ly/burtins-antibiotics-visualization-in-plotly-and-r/"> R – Modern Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/a0l2jrDf2LE"></a></div>