Group columns in the R bar chart

2

I need to generate a bar chart with the following data:

structure(c(38792L, 1227L, 23220L, 4177L, 893L, 331L), .Dim = 6L, .Dimnames = structure(list(c("Canvas para Android", "Canvas para iOS", "Chrome ", "Firefox ", "Navegador não reconhecido", "Safari ")), .Names = ""), class = "table")

I need to group the "Canvas for Android" and "Canvas for iOS" bars, just below them I must put a label "Mobile Device" and also need to group the other bars by adding a "Computer" logo.

I made the chart as follows:

AgUsuPlot<- barplot(table(AgenteDoUsuario[,1]),
        main = "Distribuição de Frequência do Agente do Usuário",
        ylim = c(0,45000),
        xlab="Agente do Usuário",
        col = c("palegreen", "green", "orange", "darkred", "red", "darkblue"),
        ylab="Exibições de Páginas",
        legend = rownames(table(AgenteDoUsuario[,1])),
        cex.axis = 0.6,
        cex.names = 0.6,
        space = 0.5,
        las=1
)
text(x=AgUsuPlot, y=table(AgenteDoUsuario[,1]), label=table(AgenteDoUsuario[,1]), pos = 3, xpd=NA)

I would like to know how I can decrease the caption, as it is getting on top of one of the bars.

    
asked by anonymous 06.11.2017 / 13:04

1 answer

3

Let's break it down,

a) To group the slashes, include the% s of spaces in the building code of your bar graph. This option specifies the space before each bar, and you can place a single option or a space for each bar of the chart.

b) To decrease the caption, create it with the command space = c(0.5, 0, .5, 0, 0, 0) and modify the option legend - just do not forget to remove the caption option within the graphic code

legend("topright", legend = names(AgenteDoUsuario), cex = 0.6,
   fill = c("palegreen", "green", "orange", "darkred", "red", "darkblue"))

c) To place the labels, I used the command cex with the specific words. Also, for better positioning, I took the name of each column of the graph with the mtext option and also put it using the names.arg = NA command.

The final code is

AgenteDoUsuario <- structure(c(38792L, 1227L, 23220L, 4177L, 893L, 331L), .Dim = 6L, .Dimnames = structure(list(  c("Canvas para Android", "Canvas para iOS", "Chrome ", "Firefox ", "Navegador não reconhecido", "Safari ")), .Names = ""), class = "table")

AgUsuPlot<- barplot(AgenteDoUsuario,
                    main = "Distribuição de Frequência do Agente do Usuário",
                    names.arg = NA, ylim = c(0,45000), xlab = "Agente do Usuário",
                    col = c("palegreen", "green", "orange", "darkred", "red", "darkblue"),
                    ylab = "Exibições de Páginas", cex.axis = 0.6,
                    cex.names = 0.6, space = c(0.5, 0, .5, 0, 0, 0),
                    las = 1
)
text(x = AgUsuPlot, y = AgenteDoUsuario, label = AgenteDoUsuario, pos = 3, xpd= NA)
mtext(names(AgenteDoUsuario), side = 1, at = c(1,2,3.5,4.5,5.5,6.5), cex = 0.6)
mtext(c("Dispositivo Mobile", "Computador"), side = 1, at = c(1.5, 5), line = 1)
legend("topright", legend = names(AgenteDoUsuario), cex = 0.6,
   fill = c("palegreen", "green", "orange", "darkred", "red", "darkblue"))
    
06.11.2017 / 13:40