0

I am having a problem with plotting a couple lines using the base plot function in R. When I set type = 'o', the plot shows all the values, but when I set it to 'l' nothing shows up. I want to have a clean line that shows each line without the dots.

I have tried ggplot, but it doesn't cover the top portion of the plots.

Can anyone give me an idea why this is happening?

Here is the source code, sample of the data and the graph with type='o' and type='l':

plot(range(c$Date), range(c$`Citibank Share Price`,na.rm=T), type="n", xlab="Year",
     ylab="Ratio to Jan. 1923 Price (Jan. 31 1923 Price =1)" )
color <- c('yellow','blue','green','black','grey','red')
# add lines
for (i in 2:dim(c)[2]) { 
  lines(c[,1], c[,i], type='l', lty=.1,lwd=.5, col=color[i-1]) 
} 

legend('topleft',legend=c('DJIA','S&P Ind.','S&P Banks','S&P Rail.','S&P 500','Citibank'),col=color,pch=1)

Sample data

        Date Dow Jones Industrials Average (Actual) S&P Industrials Index S&P Banks: Money Center (NYC) S&P Railroads
1 1923-01-31                               1.000000                     1                             1             1
2 1923-02-01                               1.002874                    NA                            NA            NA
3 1923-02-02                               1.013035                    NA                            NA            NA
4 1923-02-03                               1.019501                    NA                            NA            NA
5 1923-02-04                                     NA                    NA                            NA            NA
6 1923-02-05                               1.026686                    NA                            NA            NA
  S&P 500 Composite Price Index (w/GFD extension) Citibank Share Price
1                                               1             1.000000
2                                              NA                   NA
3                                              NA                   NA
4                                              NA             0.997006
5                                              NA                   NA
6                                              NA                   NA

Image with line='o'

Image with line='l'

ggplot code

colnames(c)[-1] <- c('DJIA','S&P_Ind','S&P_NYC_Bank','S&P_Rail','S&P500','Citibank')

c <- melt(c, id.vars='Date')
ggplot(data=c, aes(x=Date,y=value,color=variable, group=variable)) + geom_line() 

enter image description here

rokman54
  • 161
  • 1
  • 3
  • 15
  • can you post your full data somewhere? – AK88 Jul 17 '17 at 03:47
  • It looks like `lty=.1` might be your problem. Change `.1` to an integer from 1 to 5. (FYI, if needed, you can also specify a wider range of line types using other methods; see [here](https://stackoverflow.com/a/34713257/496488), for example. The example uses ggplot2, but you can specify line types the same way in base graphics.) – eipi10 Jul 17 '17 at 05:15

0 Answers0