|
| 1 | +--- |
| 2 | +description: How to make Bullet Charts in ggplot2 with Plotly. |
| 3 | +name: Bullet Charts |
| 4 | +permalink: ggplot2/bullet-charts/ |
| 5 | +thumnail_github: bullet-charts.png |
| 6 | +layout: base |
| 7 | +language: ggplot2 |
| 8 | +display_as: financial |
| 9 | +page_type: u-guide |
| 10 | +order: 8 |
| 11 | +output: |
| 12 | + html_document: |
| 13 | + keep_md: true |
| 14 | +--- |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +## Default plot |
| 19 | + |
| 20 | +Control colours with `fill` argument inside `fill()`. |
| 21 | + |
| 22 | + |
| 23 | +``` r |
| 24 | +library(plotly) |
| 25 | +library(ggplot2) |
| 26 | +library(tidyverse) |
| 27 | + |
| 28 | +data <- tibble( |
| 29 | + name = "Example", |
| 30 | + quant_value = 75, |
| 31 | + qualitative = 100 |
| 32 | +) |
| 33 | + |
| 34 | +p <- data %>% |
| 35 | + ggplot(aes(x = quant_value, y = name)) + |
| 36 | + geom_col(aes(x = qualitative), fill = "grey") + |
| 37 | + geom_col(width = 0.5, fill = "black") |
| 38 | + |
| 39 | +ggplotly(p) |
| 40 | +``` |
| 41 | + |
| 42 | +<div class="plotly html-widget html-fill-item" id="htmlwidget-f75aaff13a0a203c4b11" style="width:672px;height:480px;"></div> |
| 43 | +<script type="application/json" data-for="htmlwidget-f75aaff13a0a203c4b11">{"x":{"data":[{"orientation":"v","width":100,"base":0.55000000000000004,"x":[50],"y":[0.89999999999999991],"text":"qualitative: 100<br />name: Example","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(190,190,190,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":75,"base":0.75,"x":[37.5],"y":[0.5],"text":"quant_value: 75<br />name: Example","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(0,0,0,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null}],"layout":{"margin":{"t":26.228310502283104,"r":7.3059360730593621,"b":40.182648401826491,"l":66.484018264840202},"plot_bgcolor":"rgba(235,235,235,1)","paper_bgcolor":"rgba(255,255,255,1)","font":{"color":"rgba(0,0,0,1)","family":"","size":14.611872146118724},"xaxis":{"domain":[0,1],"automargin":true,"type":"linear","autorange":false,"range":[-5,105],"tickmode":"array","ticktext":["0","25","50","75","100"],"tickvals":[0,24.999999999999996,50,75,100],"categoryorder":"array","categoryarray":["0","25","50","75","100"],"nticks":null,"ticks":"outside","tickcolor":"rgba(51,51,51,1)","ticklen":3.6529680365296811,"tickwidth":0.66417600664176002,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":11.68949771689498},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"gridcolor":"rgba(255,255,255,1)","gridwidth":0.66417600664176002,"zeroline":false,"anchor":"y","title":{"text":"quant_value","font":{"color":"rgba(0,0,0,1)","family":"","size":14.611872146118724}},"hoverformat":".2f"},"yaxis":{"domain":[0,1],"automargin":true,"type":"linear","autorange":false,"range":[0.40000000000000002,1.6000000000000001],"tickmode":"array","ticktext":["Example"],"tickvals":[1],"categoryorder":"array","categoryarray":["Example"],"nticks":null,"ticks":"outside","tickcolor":"rgba(51,51,51,1)","ticklen":3.6529680365296811,"tickwidth":0.66417600664176002,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":11.68949771689498},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"gridcolor":"rgba(255,255,255,1)","gridwidth":0.66417600664176002,"zeroline":false,"anchor":"x","title":{"text":"name","font":{"color":"rgba(0,0,0,1)","family":"","size":14.611872146118724}},"hoverformat":".2f"},"shapes":[{"type":"rect","fillcolor":null,"line":{"color":null,"width":0,"linetype":[]},"yref":"paper","xref":"paper","x0":0,"x1":1,"y0":0,"y1":1}],"showlegend":false,"legend":{"bgcolor":"rgba(255,255,255,1)","bordercolor":"transparent","borderwidth":1.8897637795275593,"font":{"color":"rgba(0,0,0,1)","family":"","size":11.68949771689498}},"hovermode":"closest","barmode":"relative"},"config":{"doubleClick":"reset","modeBarButtonsToAdd":["hoverclosest","hovercompare"],"showSendToCloud":false},"source":"A","attrs":{"36486878706f":{"x":{},"y":{},"type":"bar"},"3648390801ff":{"x":{},"y":{}}},"cur_data":"36486878706f","visdat":{"36486878706f":["function (y) ","x"],"3648390801ff":["function (y) ","x"]},"highlight":{"on":"plotly_click","persistent":false,"dynamic":false,"selectize":false,"opacityDim":0.20000000000000001,"selected":{"opacity":1},"debounce":0},"shinyEvents":["plotly_hover","plotly_click","plotly_selected","plotly_relayout","plotly_brushed","plotly_brushing","plotly_clickannotation","plotly_doubleclick","plotly_deselect","plotly_afterplot","plotly_sunburstclick"],"base_url":"https://plot.ly"},"evals":[],"jsHooks":[]}</script> |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +## Size of the plot |
| 49 | + |
| 50 | +To make the plot thinner use `ylim` argument inside `coord_cartesian()`. |
| 51 | + |
| 52 | + |
| 53 | +``` r |
| 54 | +library(plotly) |
| 55 | +library(ggplot2) |
| 56 | +library(tidyverse) |
| 57 | + |
| 58 | +data <- tibble( |
| 59 | + name = "Example", |
| 60 | + quant_value = 75, |
| 61 | + qualitative = 100 |
| 62 | +) |
| 63 | + |
| 64 | +p <- data %>% |
| 65 | + ggplot(aes(x = quant_value, y = name)) + |
| 66 | + geom_col(aes(x = qualitative), fill = "grey") + |
| 67 | + geom_col(width = 0.5, fill = "black") + |
| 68 | + coord_cartesian(ylim = c(0.3, 1.7)) + |
| 69 | + theme_minimal() + |
| 70 | + theme(panel.grid.major.y = element_blank()) |
| 71 | + |
| 72 | +ggplotly(p) |
| 73 | +``` |
| 74 | + |
| 75 | +<div class="plotly html-widget html-fill-item" id="htmlwidget-f58e94ae238d15d3d3d7" style="width:672px;height:480px;"></div> |
| 76 | +<script type="application/json" data-for="htmlwidget-f58e94ae238d15d3d3d7">{"x":{"data":[{"orientation":"v","width":100,"base":0.55000000000000004,"x":[50],"y":[0.89999999999999991],"text":"qualitative: 100<br />name: Example","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(190,190,190,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":75,"base":0.75,"x":[37.5],"y":[0.5],"text":"quant_value: 75<br />name: Example","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(0,0,0,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null}],"layout":{"margin":{"t":26.228310502283104,"r":7.3059360730593621,"b":40.182648401826491,"l":66.484018264840202},"font":{"color":"rgba(0,0,0,1)","family":"","size":14.611872146118724},"xaxis":{"domain":[0,1],"automargin":true,"type":"linear","autorange":false,"range":[-5,105],"tickmode":"array","ticktext":["0","25","50","75","100"],"tickvals":[0,24.999999999999996,50,75,100],"categoryorder":"array","categoryarray":["0","25","50","75","100"],"nticks":null,"ticks":"","tickcolor":null,"ticklen":3.6529680365296811,"tickwidth":0,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":11.68949771689498},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"gridcolor":"rgba(235,235,235,1)","gridwidth":0.66417600664176002,"zeroline":false,"anchor":"y","title":{"text":"quant_value","font":{"color":"rgba(0,0,0,1)","family":"","size":14.611872146118724}},"hoverformat":".2f"},"yaxis":{"domain":[0,1],"automargin":true,"type":"linear","autorange":false,"range":[-0.29999999999999999,2.2999999999999998],"tickmode":"array","ticktext":["Example"],"tickvals":[1],"categoryorder":"array","categoryarray":["Example"],"nticks":null,"ticks":"","tickcolor":null,"ticklen":3.6529680365296811,"tickwidth":0,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":11.68949771689498},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":false,"gridcolor":null,"gridwidth":0,"zeroline":false,"anchor":"x","title":{"text":"name","font":{"color":"rgba(0,0,0,1)","family":"","size":14.611872146118724}},"hoverformat":".2f"},"shapes":[{"type":"rect","fillcolor":null,"line":{"color":null,"width":0,"linetype":[]},"yref":"paper","xref":"paper","x0":0,"x1":1,"y0":0,"y1":1}],"showlegend":false,"legend":{"bgcolor":null,"bordercolor":null,"borderwidth":0,"font":{"color":"rgba(0,0,0,1)","family":"","size":11.68949771689498}},"hovermode":"closest","barmode":"relative"},"config":{"doubleClick":"reset","modeBarButtonsToAdd":["hoverclosest","hovercompare"],"showSendToCloud":false},"source":"A","attrs":{"36486ca2fe7":{"x":{},"y":{},"type":"bar"},"364874f8bdea":{"x":{},"y":{}}},"cur_data":"36486ca2fe7","visdat":{"36486ca2fe7":["function (y) ","x"],"364874f8bdea":["function (y) ","x"]},"highlight":{"on":"plotly_click","persistent":false,"dynamic":false,"selectize":false,"opacityDim":0.20000000000000001,"selected":{"opacity":1},"debounce":0},"shinyEvents":["plotly_hover","plotly_click","plotly_selected","plotly_relayout","plotly_brushed","plotly_brushing","plotly_clickannotation","plotly_doubleclick","plotly_deselect","plotly_afterplot","plotly_sunburstclick"],"base_url":"https://plot.ly"},"evals":[],"jsHooks":[]}</script> |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +### What About Dash? |
| 81 | + |
| 82 | +[Dash for R](https://dashr.plot.ly/) is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library. |
| 83 | + |
| 84 | +Learn about how to install Dash for R at https://dashr.plot.ly/installation. |
| 85 | + |
| 86 | +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](https://dashr.plot.ly/dash-core-components/graph) from the built-in `dashCoreComponents` package like this: |
| 87 | + |
| 88 | + |
| 89 | +``` r |
| 90 | +library(plotly) |
| 91 | + |
| 92 | +fig <- plot_ly() |
| 93 | +# fig <- fig %>% add_trace( ... ) |
| 94 | +# fig <- fig %>% layout( ... ) |
| 95 | + |
| 96 | +library(dash) |
| 97 | +library(dashCoreComponents) |
| 98 | +library(dashHtmlComponents) |
| 99 | + |
| 100 | +app <- Dash$new() |
| 101 | +app$layout( |
| 102 | + htmlDiv( |
| 103 | + list( |
| 104 | + dccGraph(figure=fig) |
| 105 | + ) |
| 106 | + ) |
| 107 | +) |
| 108 | + |
| 109 | +app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE) |
| 110 | +``` |
0 commit comments