-2

I want to add the following x-axis label to my bar plot but unfortunately R does not recognize the character '!' and prints dots instead of whitespaces:

 I want:                I get:
 !src x.x.x.x           X.src.x.x.x.x
 !TCP                   X.TCP
 !udp && !src x.x.x.x   X.udp.....src.x.x.x.x

Additionally a would like to increase the margin because the text is to long and when setting the size over 'cex.names=0.6' then it just vanishes!?

wasp256
  • 5,943
  • 12
  • 72
  • 119
  • 1
    This question is not [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Tyler Rinker Jan 27 '13 at 18:24
  • 1
    an attempt at mindreading: you are using `read.table` to input your data and are using the default setting of `check.names=TRUE`. Try `read.table(...,check.names=FALSE)`: you may then also need to surround your variable names with back-quotes ```` to refer to them (e.g. via `$`-indexing: `mydata$`abc`` (formatting may be mangled) – Ben Bolker Jan 27 '13 at 18:32

2 Answers2

2

There are two reason I can think of that R will have substituted X. for instances of !.

  1. I suspect that the labelling you are seeing is due to R's reading of your data. Those column names aren't really syntactically valid and the erroneous character has been replaced by X.. This happens at the data import stage, so I presume you didn't check how R had read your data in?, or
  2. You have a vector and the names of that vector are similarly invalid and R has done the conversion.

However, as you haven't made this reproducible it could be anything.

To deal with case 1 above, either edit your data file to contain valid names or pass check.names = FALSE in your read.table() call used to read in the data. Although doing the latter will make it difficult for you to select variable by name without quoting the name fully.

If you have a vector, then you can reset the names again:

> vec <- 1:5
> names(vec) <- paste0("!",LETTERS[1:5])
> vec
!A !B !C !D !E 
 1  2  3  4  5
> barplot(vec)

Also note that barplot() has a names.arg argument that you can use to pass it the labels to draw beneath each bar. For example:

> barplot(vec, names.arg = paste0("!", letters[1:5]))

which means you don't need to rely on what R has read in/converted for you as you tell it exactly what to label the plot with.

To increase the size of the margin, there are several ways to specify the size but I find setting it in terms of number of lines most useful. You change this via graphical parameter mar, which has the defaults c(5,4,4,2) + 0.1 which correspond to the bottom, left, top, and right margins respectively. Use par() to change the defaults, for example in the code below the defaults are store in op and a much larger bottom margin specified

op <- par(mar = c(10,4,4,2) + 0.1)
barplot(vec, names.arg = paste0("!", letters[1:5]), las = 2)
par(op) ## reset

The las = 2 will rotate the bar labels 90 degrees to be perpendicular to the axis.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
0

One option is to use ann=F and add anotation to the plot using mtext.

 x <- 1:2
 y <- runif(2, 0, 100)
 par(mar=c(4, 4, 2, 4))
 plot(x, y, type="l", xlim=c(0.5, 2.5), ylim=c(-10, 110),
       axes=TRUE, ann=FALSE)

Then add annotation:

 mtext("!udp && !src x.x.x.x ", side=1, line=2)

enter image description here

Edit It is a question of a barplot and not simple plot.

as said in Gavin solution, the names argument can be setted. Here I show an example.

 barplot(VADeaths[1:2,], angle = c(45, 135),
         density = 20, col = "grey",
         names=c("!src x.x.x.x", "!TCP", "!udp && !src x.x.x.x", "UF"),
         horiz=FALSE)

enter image description here

agstudy
  • 119,832
  • 17
  • 199
  • 261
  • As the OP has a barplot (presumably via `barplot()`, this `mtext` solution will not be so applicable, despite being useful advice. – Gavin Simpson Jan 27 '13 at 18:39
  • @GavinSimpson good catch! I haven't test it for barplot..I thought it have ann argument too! – agstudy Jan 27 '13 at 18:41
  • The OP is talking about the labels underneath the bars I suspect, not an axis label per se. – Gavin Simpson Jan 27 '13 at 18:43
  • maybe.. he said "following x-axis label"..it is not clear without a reproducible example.. – agstudy Jan 27 '13 at 18:45
  • Yes, but `cex.names` makes me suspect this is `barplot()` and that the bar labels are the thing of interest. Not least there are three of these that are not drawn correctly. – Gavin Simpson Jan 27 '13 at 18:47
  • @GavinSimpson Right. I update my solution to visualize ( names argument) your idea with an example. – agstudy Jan 27 '13 at 18:58