堆积堆积的水平条形图

时间:2019-12-17 23:12:10

标签: r rstudio plotly r-plotly ggplotly

我正在尝试使用以下数据以绘图方式制作水平条形图: This is how the database looks

这是我到目前为止尝试创建的代码,但是我不确定其余的方法或我做对的事情:

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 个答案:

答案 0 :(得分:0)

您可以使用ggplot进行绘制并使用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)