Как создать 2 переменные в 2 рядах под осью X в barplot в r (без использования ggplot) - PullRequest
0 голосов
/ 28 марта 2019

Я пытаюсь создать более четкую ось X на гистограмме, но не знаю, как это сделать. Может ли кто-нибудь помочь, пожалуйста?

С моим текущим кодом я могу нарисовать график с двумя переменными по оси X. Но значение 2 переменных стоит рядом друг с другом, что делает это неясным для читателя. Я хочу иметь каждую строку для каждой переменной по оси X. Это мой текущий код и результат

Мой ожидаемый результат будет следующим: по оси X первая строка равна 0,1, 0,0555 и т. Д., А вторая строка - S1-2, S2-3

mxCarabooda <- structure(c(13.47258, 7.430879, 13.47151, 7.53012, 14.83685, 
                           8.940968, 15.37691, 9.617533), .Dim = c(2L, 4L), .Dimnames = list(
                             c("ChangeNP", "ChangeNP.1"), c("Sl-2 0.1", "S2-3 0.055", 
                                                            "S3-4 0.056", "S4-5 0.056")))
mxNeerabup <- structure(c(3.499426, 2.232676, 3.499596, 2.239664, 3.836086, 
                          2.566649, 3.995115, 2.725839), .Dim = c(2L, 4L), .Dimnames = list(
                            c("ChangeNP", "ChangeNP.1"), c("Sl-2 0.01", "S2-3 0.01", 
                                                           "S3-4 0.01", "S4-5 0.02")))

mxNowergup <- structure(c(3.5135, 1.700544, 3.513586, 1.710387, 3.850266, 2.034689, 
                          4.009113, 2.194351), .Dim = c(2L, 4L), .Dimnames = list(
                            c("ChangeNP", "ChangeNP.1"), c("Sl-2 0.02", "S2-3 0.01", 
                                                           "S3-4 0.02", "S4-5 0.02")))



tiff("barplot.tiff", width=260, height=100, units='mm', res=300)
par(mfrow=c(1,3), mar=c(5, 4, 4, 0.2))
colours <- c("gray63","gray87")

barplot(mxCarabooda, main='Carabooda', ylab='Profit loss ($m)',
        xlab='Change in water table at each level of GW cut', beside=TRUE, 
        col=colours, ylim=c(0,30))
legend('topright', bty="n", legend=c('Loss in GM','Loss in adjusted GM'), 
       col=c("gray63","gray87"))

barplot(mxNeerabup,main='Neerabup', ylab='', 
        xlab='Change in water table at each level of GW cut', beside=TRUE, 
        col=colours, ylim=c(0,30))
legend('topright', bty="n", legend=c('Loss in GM','Loss in adjusted GM'), 
       col=c("gray63","gray87"), pch=15)

barplot(mxNowergup,main='Nowergup', ylab='', 
        xlab='Change in water table at each level of GW cut',beside=TRUE, 
        col=colours, ylim=c(0,30))
legend('topright', bty="n", legend=c('Loss in GM','Loss in adjusted GM'), 
       col=c("gray63","gray87"), pch=15)

dev.off()

enter image description here

1 Ответ

0 голосов
/ 28 марта 2019

Подумайте о настройке names.arg из barplot, который по умолчанию используется для атрибута names вашей матрицы.В частности, создайте новые векторы имен, которые переворачивают разделенный пробелами оригинал colnames и заменяют пробел переводом строк:

Метки бар

barsCarabooda <- gsub(" ", "\n", 
                      lapply(strsplit(colnames(mxCarabooda), split=" "), 
                             function(x) paste(rev(x), collapse=" "))
                     )

barsNeerabup <- gsub(" ", "\n", 
                     lapply(strsplit(colnames(mxNeerabup), split=" "), 
                            function(x) paste(rev(x), collapse=" "))
                    )

barsNowergup <- gsub(" ", "\n", 
                     lapply(strsplit(colnames(mxNowergup), split=" "), 
                            function(x) paste(rev(x), collapse=" "))
                    )

Печать с names.arg

par(mfrow=c(1,3), mar=c(5, 4, 4, 0.2))
colours <- c("gray63","gray87")

barplot(mxCarabooda, main='Carabooda', ylab='Profit loss ($m)',
        xlab='Change in water table at each level of GW cut', beside=TRUE, 
        col=colours, ylim=c(0,30), names.arg=barsCarabooda)
legend('topright', bty="n", legend=c('Loss in GM','Loss in adjusted GM'), 
       col=c("gray63","gray87"), pch=15)

barplot(mxNeerabup,main='Neerabup', ylab='', 
        xlab='Change in water table at each level of GW cut', beside=TRUE, 
        col=colours, ylim=c(0,30), names.arg=barsNeerabup)
legend('topright', bty="n", legend=c('Loss in GM','Loss in adjusted GM'), 
       col=c("gray63","gray87"), pch=15)

barplot(mxNowergup,main='Nowergup', ylab='', 
        xlab='Change in water table at each level of GW cut',beside=TRUE, 
        col=colours, ylim=c(0,30), names.arg=barsNowergup)
legend('topright', bty="n", legend=c('Loss in GM','Loss in adjusted GM'), 
       col=c("gray63","gray87"), pch=15)

Bar Plot with Line Break X-Axis Labels

...