Barplot in r: Increase font size of no argument missing arguments and plot values ​​near string

I almost completed a barplot

in r. But it needs the final touch.

This is my code

HSI <- c(126,104,112,94,86)
HSI <- HSI-100
x <- barplot(HSI, main="HSI of Age", col=rainbow(10), names.arg=Category,
                           cex.names=0.2, las=1, horiz=T,axes=F, xlim=c(-50,50))
axis(1,at=seq(-50,50,10), labels = seq(50,150,10))
text(x, labels = 100+HSI, pos = 2) 

      

And the graph I got enter image description here

What I really want is like this picture below, enter image description here

  • The values
  • - 126.108 .. must be placed correctly (next to the bar).

  • The argument names are so small. I want them to be clearly visible. When I try to increase the size using cex.names it doesn't appear on the graph.

  • Also I tried to include the legend. It didn't work.

  • Also, how to reduce the thickness of the barplot bar?
+3


source to share


3 answers


If you want to use ggplot2

, almost everything can be changed and customized in the graphical grammar. So, for your data here:

HSI <- c(126,104,112,94,86)
HSI <- HSI-100
df <- data.frame(HSI=HSI, 
                 Category=c("Above Rs 1cr", "Rs 50 Lakh to Rs 1cr",
                            "Rs 30 to 50 Lakh", "Rs 20 to 30s Lakh",
                            "Rs 10 to 20 Lakh"))

      

We can use ggplot

to create a bar chart where you can manage many of the items you requested:

ggplot(df, aes(x=Category, y=HSI, fill=Category)) + 
  geom_bar(stat = "identity", aes(width=0.7)) + # adjust width to change thickness
  geom_text(aes(label=HSI, y=HSI+1.1*sign(HSI)),# adjust 1.1 -  to change how far away from the final point the label is
            size=5 # adjust the size of label text           
            )+ 
  coord_flip() + 
  theme_bw()

      



with output:

enter image description here

If you need more information, you will need to read how you can change items. Almost everything can be changed in ggplot

.

+1


source


  • The values
  • - 126.108 .. must be placed correctly (next to the bar).

You can define a vector of values ​​for pos

to define separate positions for each row

  1. The argument names are so small. I want them to be clearly visible. When you try to increase the size using cex.names it won't show up in the graph

You can use the argument cex

in the calltext

  1. Also I tried to include the legend. It didn't work.

cm. ?legend



  1. Also, how to reduce the thickness of the barplot bar?

You can use an argument space

to add a space to the number of columns

Some code

HSI <- c(126,104,112,94,86)
HSI <- HSI-100



x <- barplot(HSI, main="HSI of Age", col=rainbow(10), space=2, 
                                           horiz=T,axes=F, xlim=c(-50,50))

axis(1,at=seq(-50,50,10), labels = seq(50,150,10))

# add vertical line at HSI=100(0)
abline(v=0)

text(HSI, x, labels = 100+HSI, pos = c(2, 4)[(HSI>0)+1], cex=2) 

legend("topright", legend=HSI+100,  title="Mylegend", fill=rainbow(10), horiz=F)

      

What gives

enter image description here

+3


source


These are four different questions. For problem 2, where the names are too small, change cex.names

to something larger (like 1 or 2). I cannot reproduce the problem not occurring in the plot, but one solution to this problem is probably to change the fields of the plot:

par(mar=c(3,6,3,3))

      

The second argument is the left margin.

I don't have enough reputation to add a comment to ask for clarification, so I have to post it here. What is the problem with creating a legend? I can do it well, say, legend(15,5,"legend stuff")

but the best way, since the height of your plot is ambiguous, would be to use the builtin option legend.text

:

legend.text=c("thing 1","thing 2", "thing 3", "thing 4", "thing 5")

      

Just supply this as one argument to the barplot () function.

One solution to Problem 1 is to choose different numbers for text positions. For example:

HSITextPosition = 5*sign(HSI)+HSI
text(HSITextPosition, x, labels = 100+HSI)

      

For problem 4, you need to change the parameter space

in barplot()

, thereby adjusting the space between the columns.

In general, you should read the documentation for the function, barplot()

or whatever function you use, because that explains most of it. Complete code:

HSI <- c(126,104,112,94,86)
HSI <- HSI-100
HSITextPosition = 5*sign(HSI)+HSI

Category <- c("Group 1","Group 2","Group 3","Group 4","Group 5")

par(mar=c(3,6,4,4))
x <- barplot(HSI, main="HSI of Age", col=rainbow(10), names.arg=Category,
         cex.names=1, las=1, horiz=T,axes=F, 
         xlim=c(-50,50),space=2,
         legend.text=c("thing 1","thing 2", "thing 3", "thing 4", "thing 5"))
axis(1,at=seq(-50,50,10), labels = seq(50,150,10))
text(HSITextPosition, x, labels = 100+HSI)

      

enter image description here

+2


source







All Articles