Created
November 13, 2018 16:44
-
-
Save korkridake/a47104dcfb3f1d02f20721582ea58fde to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x1 <- c(62.7, 89.2) # answered yes; can change, as values come from another function | |
x2 <- sapply(x1, function(x) 100-x) # answered no | |
y <- c("Class1", "Class2") | |
data <- data.frame(y, x1, x2) | |
# data$y <- factor(data$y, levels = c("Class2", "Class1")) | |
library(plotly) | |
top_labels <- c('Yes', 'No') | |
p <- 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))) %>% | |
add_trace(x = ~x2, marker = list(color = 'rgba(71, 58, 131, 0.8)')) %>% | |
layout(xaxis = list(title = "", | |
showgrid = TRUE, | |
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 | |
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) | |
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) %>% | |
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) %>% | |
# labeling the first Likert scale (on the top) | |
add_annotations(xref = 'x', yref = 'paper', | |
x = c(21 / 2, 21 + 120/ 2), | |
y = 1.10, | |
text = top_labels, | |
font = list(family = 'Arial', size = 12, | |
color = 'rgb(67, 67, 67)'), | |
showarrow = TRUE) | |
p |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment