Horizontal Bar Charts in R
How to make a horizontal bar chart in R. Examples of grouped, stacked, overlaid, and colored horizontal bar charts.
Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.
Basic Horizontal Bar Chart
library(plotly)
fig <- plot_ly(x = c(20, 14, 23), y = c('giraffes', 'orangutans', 'monkeys'), type = 'bar', orientation = 'h')
fig
Colored Horizontal Bar Chart
library(plotly)
y <- c('giraffes', 'orangutans', 'monkeys')
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(y, SF_Zoo, LA_Zoo)
fig <- plot_ly(data, x = ~SF_Zoo, y = ~y, type = 'bar', orientation = 'h', name = 'SF Zoo',
marker = list(color = 'rgba(246, 78, 139, 0.6)',
line = list(color = 'rgba(246, 78, 139, 1.0)',
width = 3)))
fig <- fig %>% add_trace(x = ~LA_Zoo, name = 'LA Zoo',
marker = list(color = 'rgba(58, 71, 80, 0.6)',
line = list(color = 'rgba(58, 71, 80, 1.0)',
width = 3)))
fig <- fig %>% layout(barmode = 'stack',
xaxis = list(title = ""),
yaxis = list(title =""))
fig
Color Palette for Bar Chart
library(plotly)
y <- c('The course was effectively<br>organized',
'The course developed my<br>abilities and skills for<br>the subject',
'The course developed my<br>ability to think critically about<br>the subject',
'I would recommend this<br>course to a friend')
x1 <- c(21, 24, 27, 29)
x2 <-c(30, 31, 26, 24)
x3 <- c(21, 19, 23, 15)
x4 <- c(16, 15, 11, 18)
x5 <- c(12, 11, 13, 14)
data <- data.frame(y, x1, x2, x3, x4, x5)
top_labels <- c('Strongly<br>agree', 'Agree', 'Neutral', 'Disagree', 'Strongly<br>disagree')
fig <- plot_ly(data, x = ~x1, y = ~y, type = 'bar', orientation = 'h',
marker = list(color = 'rgba(38, 24, 74, 0.8)',
line = list(color = 'rgb(248, 248, 249)', width = 1)))
fig <- fig %>% add_trace(x = ~x2, marker = list(color = 'rgba(71, 58, 131, 0.8)'))
fig <- fig %>% add_trace(x = ~x3, marker = list(color = 'rgba(122, 120, 168, 0.8)'))
fig <- fig %>% add_trace(x = ~x4, marker = list(color = 'rgba(164, 163, 204, 0.85)'))
fig <- fig %>% add_trace(x = ~x5, marker = list(color = 'rgba(190, 192, 213, 1)'))
fig <- fig %>% layout(xaxis = list(title = "",
showgrid = FALSE,
showline = FALSE,
showticklabels = FALSE,
zeroline = FALSE,
domain = c(0.15, 1)),
yaxis = list(title = "",
showgrid = FALSE,
showline = FALSE,
showticklabels = FALSE,
zeroline = FALSE),
barmode = 'stack',
paper_bgcolor = 'rgb(248, 248, 255)', plot_bgcolor = 'rgb(248, 248, 255)',
margin = list(l = 120, r = 10, t = 140, b = 80),
showlegend = FALSE)
# labeling the y-axis
fig <- fig %>% add_annotations(xref = 'paper', yref = 'y', x = 0.14, y = y,
xanchor = 'right',
text = y,
font = list(family = 'Arial', size = 12,
color = 'rgb(67, 67, 67)'),
showarrow = FALSE, align = 'right')
# labeling the percentages of each bar (x_axis)
fig <- fig %>% add_annotations(xref = 'x', yref = 'y',
x = x1 / 2, y = y,
text = paste(data[,"x1"], '%'),
font = list(family = 'Arial', size = 12,
color = 'rgb(248, 248, 255)'),
showarrow = FALSE)
fig <- fig %>% add_annotations(xref = 'x', yref = 'y',
x = x1 + x2 / 2, y = y,
text = paste(data[,"x2"], '%'),
font = list(family = 'Arial', size = 12,
color = 'rgb(248, 248, 255)'),
showarrow = FALSE)
fig <- fig %>% add_annotations(xref = 'x', yref = 'y',
x = x1 + x2 + x3 / 2, y = y,
text = paste(data[,"x3"], '%'),
font = list(family = 'Arial', size = 12,
color = 'rgb(248, 248, 255)'),
showarrow = FALSE)
fig <- fig %>% add_annotations(xref = 'x', yref = 'y',
x = x1 + x2 + x3 + x4 / 2, y = y,
text = paste(data[,"x4"], '%'),
font = list(family = 'Arial', size = 12,
color = 'rgb(248, 248, 255)'),
showarrow = FALSE)
fig <- fig %>% add_annotations(xref = 'x', yref = 'y',
x = x1 + x2 + x3 + x4 + x5 / 2, y = y,
text = paste(data[,"x5"], '%'),
font = list(family = 'Arial', size = 12,
color = 'rgb(248, 248, 255)'),
showarrow = FALSE)
# labeling the first Likert scale (on the top)
fig <- fig %>% add_annotations(xref = 'x', yref = 'paper',
x = c(21 / 2, 21 + 30 / 2, 21 + 30 + 21 / 2, 21 + 30 + 21 + 16 / 2,
21 + 30 + 21 + 16 + 12 / 2),
y = 1.15,
text = top_labels,
font = list(family = 'Arial', size = 12,
color = 'rgb(67, 67, 67)'),
showarrow = FALSE)
fig
Bar Chart with Line Plot
library(plotly)
y <- c('Japan', 'United Kingdom', 'Canada', 'Netherlands', 'United States', 'Belgium', 'Sweden', 'Switzerland')
x_saving <- c(1.3586, 2.2623000000000002, 4.9821999999999997, 6.5096999999999996,
7.4812000000000003, 7.5133000000000001, 15.2148, 17.520499999999998)
x_net_worth <- c(93453.919999999998, 81666.570000000007, 69889.619999999995, 78381.529999999999,
141395.29999999999, 92969.020000000004, 66090.179999999993, 122379.3)
data <- data.frame(y, x_saving, x_net_worth)
fig1 <- plot_ly(x = ~x_saving, y = ~reorder(y, x_saving), name = 'Household savings, percentage of household disposable income',
type = 'bar', orientation = 'h',
marker = list(color = 'rgba(50, 171, 96, 0.6)',
line = list(color = 'rgba(50, 171, 96, 1.0)', width = 1)))
fig1 <- fig1 %>% layout(yaxis = list(showgrid = FALSE, showline = FALSE, showticklabels = TRUE, domain= c(0, 0.85)),
xaxis = list(zeroline = FALSE, showline = FALSE, showticklabels = TRUE, showgrid = TRUE))
fig1 <- fig1 %>% add_annotations(xref = 'x1', yref = 'y',
x = x_saving * 2.1 + 3, y = y,
text = paste(round(x_saving, 2), '%'),
font = list(family = 'Arial', size = 12, color = 'rgb(50, 171, 96)'),
showarrow = FALSE)
fig2 <- plot_ly(x = ~x_net_worth, y = ~reorder(y, x_saving), name = 'Household net worth, Million USD/capita',
type = 'scatter', mode = 'lines+markers',
line = list(color = 'rgb(128, 0, 128)'))
fig2 <- fig2 %>% layout(yaxis = list(showgrid = FALSE, showline = TRUE, showticklabels = FALSE,
linecolor = 'rgba(102, 102, 102, 0.8)', linewidth = 2,
domain = c(0, 0.85)),
xaxis = list(zeroline = FALSE, showline = FALSE, showticklabels = TRUE, showgrid = TRUE,
side = 'top', dtick = 25000))
fig2 <- fig2 %>% add_annotations(xref = 'x2', yref = 'y',
x = x_net_worth, y = y,
text = paste(x_net_worth, 'M'),
font = list(family = 'Arial', size = 12, color = 'rgb(128, 0, 128)'),
showarrow = FALSE)
fig <- subplot(fig1, fig2)
fig <- fig %>% layout(title = 'Household savings & net worth for eight OECD countries',
legend = list(x = 0.029, y = 1.038,
font = list(size = 10)),
margin = list(l = 100, r = 20, t = 70, b = 70),
paper_bgcolor = 'rgb(248, 248, 255)',
plot_bgcolor = 'rgb(248, 248, 255)')
fig <- fig %>% add_annotations(xref = 'paper', yref = 'paper',
x = -0.14, y = -0.15,
text = paste('OECD (2015), Household savings (indicator), Household net worth (indicator). doi: 10.1787/cfc6f499-en (Accessed on 05 June 2015)'),
font = list(family = 'Arial', size = 10, color = 'rgb(150,150,150)'),
showarrow = FALSE)
fig
Reference
See https://plotly.com/r/reference/#bar for more information and chart attribute options!
What About Dash?
Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.
Learn about how to install Dash for R at https://dashr.plot.ly/installation.
Everywhere in this page that you see fig
, you can display the same figure in a Dash for R application by passing it to the figure
argument of the Graph
component from the built-in dashCoreComponents
package like this:
library(plotly)
fig <- plot_ly()
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... )
library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)
app <- Dash$new()
app$layout(
htmlDiv(
list(
dccGraph(figure=fig)
)
)
)
app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)