1

I am trying to generate a bar plot with a categorical X axis and two different y axis. I am trying to use twoord.plot to generate the bar plot as follows:

x <- c("A","B","C","D","E")
ry <- c(0.1,0.2,0.3,0.4,0.5)
ly <- c(0.15,0.25,0.35,0.45,0.55)
library(plotrix)
twoord.plot(x,ry,x,ly,
            xlab="xLabel",
            ylab="yLabel",
            rylab="ryLabel",
            main="Main",
            type=c("bar","l"),lcol=rainbow(length(x)),rcol=4)

However, I am getting an error "Error in plot.window(...) : invalid 'xlim' value".

Is there a way to work with categorical/character variables as x-axis? Also, is there a way to rotate the X-axis labels so that they show up at 45 degrees?

I have been able to get this code to work with the following changes:

xNumeric <- seq(1:length(x))
twoord.plot(xNumeric,ly,xNumeric,ry,
              xlab="xLabel",
              ylab="yLabel",
              rylab="ryLabel",
              main="Main",
              type=c("bar","o"),lcol=rainbow(length(x)),rcol = 4,xticklab = x)

However, I still need to figure out how to rotate the X-axis labels as well as adding a legend to differentiate between which is the box plot and which is the line plot. Any help on this would be appreciated

Thank you.

Ravi
  • 3,223
  • 7
  • 37
  • 49
  • It's an old question, but just for the record here's an answer: The x-axis variable needs to be a factor. If it's of type `string` you get an error. So simply surround any string vector that you want for the x-axis within a call to `as.factor(...)`, the the standard `plot()` R command will work. For other plotting libraries it depends on their respective implementation. – Mörre Apr 25 '15 at 13:39

1 Answers1

1

This isn't in plotrix, but...

ry <- c(0.1,0.2,0.3,0.4,0.5)
ly <- c(15,35,65,75,80)
x <- 1:5
xlabs <- c("A","B","C","D","E")
barplot(ly, xaxt="n", yaxt="n", xlab="xLabel", ylab="lyLabel", ylim=c(0,100))
axis(2, seq(0,100,by=5), seq(0,100,by=5), las=2) # you can adjust positions of ly labels
par(new=TRUE)
plot(ry~x, xaxt="n", yaxt="n", xlab="", ylab="", ylim=c(0,1))
axis(1, 1:5, xlabs)
axis(4, 1:10/10, 1:10/10, las=2) # you can adjust positions of ry labels
mtext("ryLabel", 4, line=2)

And you would obviously need to edit a bit to get the colors, etc. that you seem to be going for.

Thomas
  • 43,637
  • 12
  • 109
  • 140
  • Thanks, Thomas. Your solution seems to work only if both the ry and ly have the similar scales. I tried the following two examples. Neither worked ry <- c(0.1,0.2,0.3,0.4,0.5) ly <- c(15,35,65,75,80) ry <- c(0.1,0.2,0.3,0.4,0.5) ly <- c(0.5,1.5,2,5,6) – Ravi May 02 '13 at 12:03
  • Sorry, this depended on where `axis(2,...)` was in the code. I've edited it. As long as you specify `ylim` in both plot calls, you should be able to fit plots with arbitrary dimensions. – Thomas May 02 '13 at 12:09
  • Just tried it...the y axis tick marks appear exactly at the values specified in the ly and ry vectors. This results in odd spaced values for ly. I have been able to use twoord.plot to get the solution and have updated the original post. – Ravi May 02 '13 at 12:29
  • You have to specify whatever positions/labels you want on the axes. See edited version with explicit `ylim` and `axis` values. – Thomas May 02 '13 at 12:33