0
votes

I am trying to make a horizontal bar chart using plotly using the following data: This is how the database looks

This is the code I have tried to create so far, but I'm not sure how to do the rest or if I'm doing it right:

data <- data.frame(Answer = c("Have more than enough money", "Have enough money", "Have just enough money", "Do not have enough money", "Prefer not to answer"), 
                   City_Total = c(11,34,34,16,4), 
                   Auckland = c(11,30,35,19,5), 
                   Hamilton = c(10,28,41,16,5), 
                   Tauranga = c(12,38,35,12,4),
                   Hutt = c(12,39,31,14,3), 
                   Porirua = c(10,36,29,19,5), 
                   Wellington = c(16,42,28,11,3),
                   Christchurch = c(11,41,31,13,4),
                   Dunedin = c(12,41,34,11,2))
1

1 Answers

0
votes

You can just make the plot with ggplot and use ggplotly():

require(plotly)
require(dplyr)

data <- data.frame(Answer = c("Have more than enough money", "Have enough money", "Have just enough money", "Do not have enough money", "Prefer not to answer"), 
                   City_Total = c(11,34,34,16,4), 
                   Auckland = c(11,30,35,19,5), 
                   Hamilton = c(10,28,41,16,5), 
                   Tauranga = c(12,38,35,12,4),
                   Hutt = c(12,39,31,14,3), 
                   Porirua = c(10,36,29,19,5), 
                   Wellington = c(16,42,28,11,3),
                   Christchurch = c(11,41,31,13,4),
                   Dunedin = c(12,41,34,11,2))
m_data <- melt(data)

g <- ggplot(m_data,aes(x=variable, y=value, fill=Answer)) +
  geom_bar(position="stack", stat="identity") + 
  coord_flip() +
  theme_minimal()

ggplotly(g)