1

I am trying to draw a plot in R using barplot(). But I am running into a few problems. First I am attaching an image of the plot and the code I am using to plot this. Then I am describing the problems.

The Image:

image

Here is the code snippet that I am using to generate this plot.

    par(mar=c(14, 7, 2, 2), mgp=c(5,2,0))

        midpts<-barplot(x[,input$year], 
                        main=input$year,
                        ylab="Number of Units",
                        xlab="Product",col='maroon')
        vps <- baseViewports()
        pushViewport(vps$inner, vps$figure, vps$plot)

        grid.text(rownames(x),
                  x = unit(midpts, "native"), y=unit(-1, "lines"),
                  just="right", rot=90)
        popViewport(3)

The problems:

1) This whole plot also looks very small overall and I would like this to be larger.

2) Some markers in the Y axis are not visible. The missing markers depend on the "Year" dropdown. For some years, all the Y axis markers will appear, for others, a couple will be missing like for this one.

3) Can someone also tell me how can I make my X label ("Product") and Y label ("Number of Units") bold and distinct so that they can be differentiated from the markers?

Note: This plot is part of a R-Shiny dashboard, but I don't think that this is relevant for making the changes.

Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
Tuhin
  • 157
  • 12

1 Answers1

2

Fixes

1) Size

You have a fixed area to visualize the plot, so it is hard to see how can you make it larger.

One option to fix this would be to reduce the size of text labels and margins, so there will be more area for the bars.

2) Y axis markers

This is because of 2 reasons: 1 - you show them aligned in parallel with the y-axis, so they take a lot of space, and 2 - the font is big, so they overlap. In such case it's better to not show some of the labels than for them to overlap.

To fix this you can consider rotating the labels by 90 degrees.

3) Bold labels

You can add labels to the plot via a a separate function title() which controls these options.

4) Extra

I suggest to drop the "viewPort" thing you are doing, as this is specific to grid graphics.

Result

In total, here is a version with code that should fix most of your issues. You didn't provide the dataset you were using, so I used "USArrests" instead.

par(mar=c(7, 7, 2, 2), mgp=c(5,2,0))

mids <- barplot(USArrests$Assault, col='maroon', las=2, cex.names=0.7, cex.axis=0.7)
mtext(rownames(USArrests), 1, at=mids, las=2, cex=0.7)

title("Assault")
title(xlab="states", font.lab=2)
title(ylab="Number of Units", font.lab=2)

result

Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
  • I am using that viewport thing for the grid.text function which helps me display the X axis markers (the products) properly. It wasn't showing properly and a stack overflow search led me to that solution. Let me see if this works and I'll let you know. – Tuhin Mar 30 '20 at 10:46
  • Thanks, this works. Just one question again. In your example, see how some of the bars are going beyond the last Y-axis marker. The same is happening for me. Is there any way to fix that? I would like the bar to stay below the last Y axis marker, I feel it would look nicer that way. Also, Im extremely sorry about not being able to provide a dataset as this was something work-related. – Tuhin Mar 30 '20 at 10:53
  • 1
    @Tuhin I have an answer for that in another question: https://stackoverflow.com/a/50193822/1953718 , see if it works for your case. – Karolis Koncevičius Mar 30 '20 at 10:57
  • 1
    It worked perfectly. Even automatically adjusted the markers based on the year drop down. Fantastic and such a simple answer. – Tuhin Mar 30 '20 at 11:12