An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121123/309ac0dc/attachment.pl>
Barplot with lines
6 messages · Ripples, Jim Lemon, skanap +1 more
On 11/23/2012 06:33 PM, Ripples wrote:
Hi, I'm trying to plot stacked barplot with lines on it. Here is the data. 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 My X-axis is days. I'm looking to plot val1,val2 as stacked bars and score as lines with different y-axis. I could get the bar plot and lines on it but the problem is the bars and lines are not aligning well for same point on X-axis. Note, that there might not be a score for some when there is val1 and val2 or vice versa. Any help in this regard is highly appreciated.
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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121123/335de0ac/attachment.pl>
On 11/24/2012 02:21 AM, skanap wrote:
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!
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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121126/bab0a031/attachment.pl>