Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R plot (hclust) and clusplot titles cut off on top

Tags:

plot

r

I am experiencing the similar problems seen in:

  1. Main title at the top of a plot is cut off
  2. R: Titles cut in half with par()

and i tried the solutions suggested in both posts.

my code used to plot my hclust:

plot(hclust.train, hang = -1)

here's how my hclust plot look like: enter image description here

as suggested in the first post mentioned above, i tried:

par(oma=c(0,0,2,0))

to add border padding; however, the plot with the border look like this: enter image description here

it's simply the cut off version but moved down a bit...

here's how my clusplot plots look like (same problem):

enter image description here

and with the border added: enter image description here

i've also tried using the argument "mar=c(0,0,2,0)", as suggested in the 2nd post mentioned in the beginning, but the result is the same as adding the border padding.

any help is appreciated.

Edit: Thanks to Ben Bolker for a simple quick solution. After using his suggested code:

par(xpd=NA,oma=c(0,0,2,0))

i got the following improved version: enter image description here

my last question would be, how to get rid of the border line that's going through the title, OR move the title position above that border line? Thank you!

like image 766
alwaysaskingquestions Avatar asked Oct 20 '25 09:10

alwaysaskingquestions


1 Answers

You didn't give a reproducible example, but I can reproduce this if I set my margins to all-zero; I can work around it with par(xpd=NA)

hc <- hclust(dist(USArrests), "ave")
plot(hc)  ## looks OK

Setting the margins to zero cuts off the title:

par(mar=c(0,0,0,0))
plot(hc)  ## cut off

Setting the outer margin doesn't help:

par(mar=c(0,0,0,0),oma=c(0,0,2,0)) 
plot(hc)

Setting par(xpd=NA) does help:

par(mar=c(0,0,0,0),xpd=NA)
plot(hc)

If I add box() I get a border line that cuts through the title. The best thing would be to not set the margins to zero, or to set par(mar=c(0,0,2,0)) to make sure there's room for the title at the top, or to use par(oma=c(0,0,2,0)) and then use mtext() to add the title by hand.

like image 94
Ben Bolker Avatar answered Oct 21 '25 22:10

Ben Bolker