Problems plotting a data.frame


I am trying to do a simple graph with plot() function.
When I do the plot with two columns of the data.frame the style options dosen't work. But in lines() function it works well.

I don't jnow th reason. Is there something wrong in my code.

dataf <- data.frame(producto=c("P1","P2","P3","P4","P5","P6","P7","P8"),
ventas = c(20,20,10,30,40,30,10,40),
presup = c(25,18,11,32,34,30,12,30)

plot(dataf$producto, dataf$ventas,type="p", col="blue", ylim=c(0,50), main="Ventas por producto")
# the type="p" and color = "blue" doesn't work

lines(dataf$presup, type="l", col="blue")
# type and color works properly


Super Moderator
it's because you are trying to plot a factor and a continuous response in which case plot() defaults to a boxplot and of course type="p"doesn’t make sense.

It works with following code where you have two continuous variables

plot(dataf$presup,dataf$ventas,type="p", col="blue", ylim=c(0,50), main="Ventas por product")
Last edited:
Thanks bugman! It is confusing when a boxplot appear and you expect a scatter plot.

But I asked myself, how would I do such a plot? The suggestion below is somewhat "crude" and it would be nice if someone suggested something else.

I suggest to plot with a new numeric variable ("") and plot the labels later with the axis command.

# but how do you plot that?
# I add the numeric variable:
dataf <- data.frame(producto=c("P1","P2","P3","P4","P5","P6","P7","P8"),
           =c(   1,  2,   3,   4,   5,   6,   7,   8),
                    ventas = c(20,20,10,30,40,30,10,40),
                    presup = c(25,18,11,32,34,30,12,30)

# a first test to plot with a numeric variable on the x-axis
plot(dataf$, dataf$ventas,type="p", col="blue", ylim=c(0,50), main="Ventas por producto")


plot(dataf$, dataf$ventas,type="p", col="blue", pch=1, ylim=c(0,50), main="Ventas por producto",
     xaxt="n",xlab= "producto")
# xaxt="n"  this makes the marking for invisible 

# and use axis() for label marking  the x-axis
axis(side=1,at=dataf$ ,labels=c("P1","P2","P3","P4","P5","P6","P7","P8"))

# and you can add the other variable if you want to, but then you will need a legend command
points(dataf$, dataf$presup,type="p", col="red", pch=1, ylim=c(0,50), main="Ventas por producto",
       xaxt="n",xlab= "producto")

# ... but then you will ned a legend command
legend("topright", c("ventas", "presup"), col=c("blue", "red"), pch=1)

# I would prefer pch=19

(@juanvg1972. There is an "Edit" button to correct typing mistakes (most of us make mistakes or spelling errors) and a "code" button, click on the #-symbol. That will make your code more readable. )


Super Moderator
Nice suggestion GG.

Another solution would be to use a dotchart. For example: