R语言 条形图的定制

2o7dmzc5  于 2023-02-26  发布在  其他
关注(0)|答案(1)|浏览(146)

我正尝试用下面的数据制作一个堆叠条形图。

library(plotly)
    df_t<-data.frame(
                      Country=c('Aruba','Armenia','Albania','Bahami','Bhutan','Benin','Aruba','Armenia','Albania','Bahami','Bhutan','Benin'),
                      Year=c(2010,2010,2010,2010,2010,2010,2011,2011,2011,2011,2011,2011),
                      Male=c(10,10,10,10,10,10,10,10,10,10,10,10),
                      Female=c(10,10,10,10,10,10,10,10,10,10,10,10))

现在我想用Plotly库做一个图表,我试着用这行代码:

Chart <- plot_ly(df_t, x = ~Year, y = ~Country, name = "Aruba", type = 'bar', mode = 'lines',  
                  line = list(dash = "solid"))

但这不是我需要的,所以谁能帮我解决这个问题,并产生如下所示的图表:

2hh7jdfx

2hh7jdfx1#

https://plotly.com/r/bar-charts/绘制堆叠条形图的一般方法

fig <- plot_ly(df_t, x = ~Country, y = ~Male, type = 'bar', name = 'Male')
    fig <- fig %>% add_trace(y = ~Female, name = 'Female')
    fig <- fig %>% layout(yaxis = list(title = 'Count'), barmode = 'stack')
    
    fig

这仍然需要细化转换为百分比规模,添加额外的x类别等,但应该让你开始。

相关问题