A short tutorial on how to use geom_segment() to create a Likert-type plot with ggplot, and then convert this to an interactive plotly chart.
Like ggplot, plotly does not easily accomodate Likert-type charts, and for many of the same reasons – namely that negative bar heights make stacking more complicated. This tutorial uses geom_segment() rather than geom_bar() to achieve the desired layout in ggplot.
We’re using the same pre-summarized data from the Arab Barometer III survey as used in this post, and the data can be downloaded here in CSV format. The data concern public opinion on the future economic outlook in eleven Arab countries. Survey respondnets were asked, “What do you think will be the economic situation in your country during the next few years (3-5 years) compared to the current situation?,” with the response options: “Much worse,” “Somewhat worse,” “Almost the same,” “Somewhat better,” or “Much better.”
Here’s the chart in plotly:
First we get the data and load four required packages.
We difference the data from a zero center-point to obtain staring values for our line segments. The center-point for each country is half the neutral category (“Almost the same”). We then order the factor by the sum of negative responses to obtain the classic “cascade” of Likert-type plots. Finally, we multiply the entire data frame by 100 to get the data into a percent format.
Next, we melt the data frame into a long format. The first half of the melted data frame contains response values, and the second half of the melted data frame contains the starting values we calcuated previously. We need these to be on the same rows in our data frame, because geom_segment requires starting and ending coordinates for each segment. We use the cbind() function to reshape our data frame as required. We assign nicely formatted levels to our factor. And finally, we define a custom color palette loosly based on the pallete from this website; alternatively, one might use a palette from RColorBrewer.
The call to ggplot() is rather strait-forward. From each coordinate, we draw a segment of length and thickness size 6. We also specify a custom string that will be used for plotly’s hovertext.
The plot looks good enough in ggplot, and after we render this with plotly, we can still add a title and customize many features. It’s best to have a fairly simple ggplot object to pass to plotly, because not all ggplot features are currently supported by plotly. For example, some ggthemes render poorly in plotly, and some options, like a horizontal legend, are not yet available.
This final conversion to plotly requires a free subscription to post, but omit the last line here, and the chart will still render locally in RStudio’s viewer or in a browser. Transform a ggplot object to a plotly object with plotly_build(), and then edit the plotly object as necessary. I found this easiest to do with the pipe oberator (%>%). I added a multiline title, using <br> rather than \n for line breaks. I increased the top margin, because of the multiline title. And, I specified a height and width rather than use the autosize option, because the width of my bars (i.e., line segments) are determined by the size I specified in the previous call to ggplot and will not auto-scale with plotly.