Problems plotting a data.frame

#1
Hi,

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
 

bugman

Super Moderator
#2
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

Code:
plot(dataf$presup,dataf$ventas,type="p", col="blue", ylim=c(0,50), main="Ventas por product")
I
 
Last edited:
#3
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 ("prod.no") and plot the labels later with the axis command.


Code:
# but how do you plot that?
# I add the numeric variable: prod.no
dataf <- data.frame(producto=c("P1","P2","P3","P4","P5","P6","P7","P8"),
                    prod.no =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)
)
dataf

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


###

plot(dataf$prod.no, 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 prod.no invisible 

# and use axis() for label marking  the x-axis
axis(side=1,at=dataf$prod.no ,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$prod.no, 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. )
 

bugman

Super Moderator
#4
Nice suggestion GG.

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

Code:
library(Hmisc)
dotchart2(dataf$presup,dataf$producto,cex.labels=0.7,
          dotsize=1,col=c(1:8),pch=19,lty=2,cex=1.2)