R 标绘叠加水平条形图

R 标绘叠加水平条形图,r,rstudio,plotly,r-plotly,ggplotly,R,Rstudio,Plotly,R Plotly,Ggplotly,我正在尝试使用以下数据绘制水平条形图: 这是我迄今为止尝试创建的代码,但我不确定如何完成其余部分,或者我是否做对了: 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_T

我正在尝试使用以下数据绘制水平条形图:

这是我迄今为止尝试创建的代码,但我不确定如何完成其余部分,或者我是否做对了:

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))

data您只需使用
ggplot
进行绘图,然后使用
ggplotly()

require(plotly)
需要(dplyr)

melt(数据)中的数据错误:无法找到函数“melt”@Raisham,
melt
可能来自软件包
REPLACE2
。是的,刚刚找到!
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)