堆叠的barplot彼此相邻

时间:2016-05-09 21:49:19

标签: r bar-chart stacked

我正在尝试构建一个彼此相邻的堆叠条形图,但只有我能够生产的是两个密封的条形图。这是一个示例数据

par(mfrow=c(1,2))

i<-c(49.36,42.61,40.14,41.33)
ls<-c(49.27,47.4,57.39,56.02)
us<-c(1.37,9.99,2.48,2.65)
dat<-as.matrix(rbind(i,ls,us))
colnames(dat)<-colnames(dat, do.NULL = FALSE, prefix = c("bc","hd","he","mi"))
barplot(dat,col=c("green4","Orange","orangered4"),names.arg = c("BC","HD","HE","MI"))

i<-c(55.76,56.96,52.09,41.76)
ls<-c(39.03,40.05,45.77,50.04)
us<-c(5.21,2.99,2.13,8.20)
dat1<-as.matrix(rbind(i,ls,us))
colnames(dat1)<-colnames(dat1, do.NULL = FALSE, prefix = c("bc","hd","he","mi"))
barplot(dat1,col=c("green4","Orange","orangered4"),names.arg = c("BC","HD","HE","MI"))

这就是结果:

enter image description here 我想要做的是将这两个图组合成单个图,使BC和BC1彼此相邻,HD和HD1彼此相邻,依此类推。我认为函数beside = T在这里很有用,但这就是我所知道的。

由于

1 个答案:

答案 0 :(得分:2)

可能是这样的(我在第二个数据框中更改了列名,因此所有列名都是唯一的):

$('#div_graph_0_1').highcharts({
                chart: {
                    plotBackgroundColor: null,
                    plotBorderWidth: null,
                    plotShadow: false,
                    type: 'pie'
                },
                credits: {
                    enabled: false
                },
                title: {
                    text: 'NEW VISITORS'
                },
                subtitle:{
                    text: pieSubtitleTotal,
                    style: { color: '#f07600' }
                },
                tooltip: {
                    pointFormat: '{point.y}'
                },
                plotOptions: {
                    pie: {
                        states: {
                            hover: {
                            halo: {
                            size: 9,
                            attributes: {
                                fill: '#f07600'}
                                }
                            }
                        },
                        allowPointSelect: true,
                        cursor: 'pointer',
                        dataLabels: {
                            connectorWidth: 0,
                            enabled: true,
                            format: '{point.y}',
                            style: {
                                color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                            }
                        },
                    }
                },
                series: [{
                    name: 'New',
                    colorByPoint: true,
                    data: [{
                        name: 'Repeat',
                        y: subtitleTotal,
                        color: '#fff',
                        borderColor: '#f07600',
                        borderWidth: 2
                    }, {
                        name: 'New',
                        color: '#f07600',
                        borderColor: '#f07600',
                        y: pieTotalVisitors,
                        sliced: true,
                        selected: true
                        }]
                    }],
                     navigation: {
                    buttonOptions: {
                        enabled: false
                        }
                     },
                     function(chart) { // on complete

                         chart.renderer.text('This text is <span style="color: red">styled</span> and <a href="http://example.com">linked</a>', 0, 0)
                             .css({
                                 color: '#4572A7',
                                 fontSize: '16px'
                             })
                             .add();
                     }
            });

enter image description here

您甚至可以按如下方式对条形图进行分组:

# Source: https://stat.ethz.ch/pipermail/r-help/2006-March/101023.html
interleave <- function(v1,v2)
{
  ord1 <- 2*(1:length(v1))-1
  ord2 <- 2*(1:length(v2))
  c(v1,v2)[order(c(ord1,ord2))]
}

i<-c(49.36,42.61,40.14,41.33)
ls<-c(49.27,47.4,57.39,56.02)
us<-c(1.37,9.99,2.48,2.65)
dat<-as.matrix(rbind(i,ls,us))
colnames(dat) <- colnames(dat, do.NULL = FALSE, prefix = c("bc","hd","he","mi"))

i<-c(55.76,56.96,52.09,41.76)
ls<-c(39.03,40.05,45.77,50.04)
us<-c(5.21,2.99,2.13,8.20)
dat1<-as.matrix(rbind(i,ls,us))
colnames(dat1) <- colnames(dat1, do.NULL = FALSE, prefix = c("bc1","hd1","he1","mi1"))

dat2 = cbind(dat, dat1)[ , interleave(colnames(dat), colnames(dat1))]

barplot(dat2, col=c("green4","Orange","orangered4"))

enter image description here