Skip to content

Barplot with lines

6 messages · Ripples, Jim Lemon, skanap +1 more

#
On 11/23/2012 06:33 PM, Ripples wrote:
Hi SK,
Your alignment problem is due to the way barplot spaces the bars. Try this:

mydata<-read.table(textConnection(
  "emp days val1 val2 score
  1 21 1 0 1200
  2 35 1 1 na
  3 42 na na 3000
  4 53 2 1 2100
  5 64 1 0 na
  6 73 na na 1400"),
  header=TRUE,na.strings="na")
barpos<-barplot(as.matrix(mydata[c(1,2,4,5),c("val1","val2")]))

Then use "barpos" as the x locations of your values for the lines.

Jim
#
Hi Jim,

Thank you for the reply. But, when I use Barpos I get this error.

plot(mydata$score,barpos)
Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' and 'y' lengths differ

I need all the points in x-axis that are in 'days' column. If I'm selecting
only points in val1 and val2 I'm missing rows for score.  Thanks again!

SK



--
View this message in context: http://r.789695.n4.nabble.com/Barplot-with-lines-tp4650515p4650557.html
Sent from the R help mailing list archive at Nabble.com.
#
On 11/24/2012 02:21 AM, skanap wrote:
Hi SK,
If you want to get all bars and lines displayed, you can substitute 
zeros for the missing values in val1 and val2. Then your "empty bars" 
will have zero height and your "barpos" will have all the _x_ values you 
need. In the above you are using barpos as the _y_ values.

However, looking at Greg's solution, it is probably better in the 
general case of missing values.

Jim
2 days later