Group columns in the R bar graph

Asked

Viewed 885 times

2

I need to generate a bar graph 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 bars of "Canvas for Android" and "Canvas for iOS", just below them I should put a label "Mobile device" and also need to group the other bars adding a logo "Computer".

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 wonder how I can diminish the legend, because it is standing on top of one of the bars.

1 answer

3


Let’s go in pieces,

a) To group the bars, include the space array space = c(0.5, 0, .5, 0, 0, 0) in the building code of your bar chart. This option specifies the space before each bar, and you can put a single option or a spacing for each bar of the chart.

b) To decrease the caption, create it with the command legend and modify the option cex - just don’t forget to remove the label option inside the chart code

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

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

The final code remained

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"))
  • Perfect Rafael! All commands worked beautifully!

  • Thanks a lot! The first command mtext I was showing the old labels, I made small changes to your code and the chart was the way I wanted it! Thank you Rafael! You’re saving my TCC rsrs.

  • I’m glad it worked out. Consider vote and accept my answer if she solved your problem

Browser other questions tagged

You are not signed in. Login or sign up in order to post.