Adding lines or points to an existing barplot
Sometimes you will need to add some points to an existing barplot. You might try
par(mfrow = c(1,2)) df <- data.frame(stolpec1 = 10 * runif(10), stolpec2 = 30 * runif(10)) barplot(df$stolpec1) lines(df$stolpec2/10) #implicitno x = 1:10 points(df$stolpec2/10)
but you will get a funky looking line/points. It’s a bit squeezed. This happens because bars are not drawn at intervals 1:10, but rather on something else. This “else” can be seen if you save your barplot object. You will notice that it’s a matrix object with one column – these are values that are assumed on x axis. Now you need to feed this to your lines/points function as a value to x argument and you’re all set.
df.bar <- barplot(df$stolpec1) lines(x = df.bar, y = df$stolpec2/10) points(x = df.bar, y = df$stolpec2/10)
Another way of plotting this is using plotrix package. The controls are a bit different and it takes some time getting used to it.
library(plotrix) barp(df$stolpec1, col = "grey70") lines(df$stolpec2/10) points(df$stolpec2/10)
Leave a Reply Cancel reply
Recent Posts
- How to branch/fork a (StatET) project with SVN
- Show me yours and I’ll show you mine
- Getting knitr to work with StatET
- Write data (frame) to Excel file using R package xlsx
- Adding lines or points to an existing barplot
- Modeling sound pressure level of a rifle shot
- Dump R datasets into a single file
- Bootstrap, strap-on, anal-yzing… statistics is getting weirder by the moment
- How to calculate with dates and hours in R
- Building an R package (under Windows) without C, C++ or FORTRAN code


Thx, was a great help. I did something with more than four data sets and some barplots and points. So your post was a good starting point.
Alex