[R] subject: horizontal text in barplot

Marc Schwartz marc_schwartz at comcast.net
Mon Jun 2 15:19:31 CEST 2008


on 06/02/2008 08:00 AM dat wrote:
> 
> I have a problem with the ylabs in barplot. When I draw the bars 
> horizontal the text for the y axis is not drawn horizontal too.
> The text remains vertical. How can I change that. Thank's for your advice.
> There is an example below.
> 
> 
> ##Example
> plot.new()
> jib <- data.frame(c(1:15),c(11:25),c(15:1),c(25:11))
> colnames(jib) <- c("eins","zwei","drei","vier")
> rownames(jib) <-  c("AT",    "BE",    "DK",    "FI",    "FR",    "DE",   
> "GR",    "IE",    "IT",    "LU",    "NL",    "PT",    "ES",    "SE", "UK" )
> jib <- as.matrix(jib)
> jib <- t(jib)
> barplot(jib,  horiz=TRUE)
> jib
> ## End Example


A lot of what you are doing above with respect to managing/structuring 
the example data is not needed, nor is the plot.new().

Here is your jib:

 > jib
      AT BE DK FI FR DE GR IE IT LU NL PT ES SE UK
eins  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
zwei 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
drei 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1
vier 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11


Now use:

jib2 <- matrix(c(1:15, 11:25, 15:1, 25:11), nrow = 4, byrow = TRUE)

rownames(jib2) <- c("eins", "zwei", "drei", "vier")

colnames(jib2) <-  c("AT",    "BE",    "DK",    "FI",    "FR",    "DE",
                      "GR",    "IE",    "IT",    "LU",    "NL",    "PT",
                      "ES",    "SE", "UK" )

 > jib2
      AT BE DK FI FR DE GR IE IT LU NL PT ES SE UK
eins  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
zwei 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
drei 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1
vier 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11


# Now draw the plot, setting 'las' so the labels are
# set to horizontal. See ?par for more information

barplot(jib2,  horiz = TRUE, las = 1)


Data frames are best used when you have columns with differing data 
types (think database table or spreadsheet). When the data is of a 
single type, as in this case, just use a matrix directly.

HTH,

Marc Schwartz



More information about the R-help mailing list