2

So I made a plot with multiple legends in it, but due to circumstances, I would like to make them in a different location and have the line's legend not have such a thick border.

Here's the code:

asset <- "JP-Morgan"

ticker <- "JPM"

start.date <- as.Date('2007-09-01')

# Import the Data

getSymbols(ticker, src='yahoo', from=start.date)

# string), and index the sixth column, which corresponds Adjusted Close values

Adj.Close <- get(ticker)[,6]

daily.ret2 <- ROC(Adj.Close, n=1, type='discrete')

mo.12 <- as.Date((as.numeric(Sys.Date())-365))

# Subset by 2011 on
daily.ret2 <- daily.ret2['2011/']

# Rename column so that it is not asset-specific
names(daily.ret2) <- 'Daily'

# Convert to data frame and add a column for dates
daily.ret2.df<- data.frame(year=format(time(daily.ret2),'%Y'), 
                           daily.ret2,
                           times = format(time(daily.ret2),'%Y-%m-%d'))

daily.ret2.mo <- daily.ret2[paste0(mo.12,'/'),1]
daily.ret2.12 <- data.frame(year=' Last 12 Months',daily.ret2.mo)

den.y.fr <- round(sd(daily.ret2.12$Daily)*-2,digits = 3)
den.y.to <- round(sd(daily.ret2.12$Daily)*2,digits = 3)
den.y.by <- round(sd(daily.ret2.12$Daily),digits = 3)

# All densities on one plot (kerel-smoothed) ####
kernal.dens <- ggplot(daily.ret2.df, aes(x=Daily,color = year))+
  geom_density(data = filter(daily.ret2.df, year != 2019),adjust=2, size=1, show.legend = T)+
  geom_density(data = daily.ret2.12,adjust=2, size=1.75, show.legend = T)+
  geom_segment(aes(x = sd(daily.ret2.12$Daily),xend = sd(daily.ret2.12$Daily), 
                   yend = 5,y = 0,lty='s of YoY'),color = "blue",size = .5)+
  geom_segment(aes(x = sd(daily.ret2.12$Daily)*2,xend = sd(daily.ret2.12$Daily)*2,
                         yend = 5,y = 0,lty='s of YoY'),color = "blue",size = .5)+
  geom_segment(aes(x = sd(daily.ret2.12$Daily)*-1,xend = sd(daily.ret2.12$Daily)*-1,
                         yend = 5,y = 0,lty='s of YoY'),color = "blue",size = .5)+
  geom_segment(aes(x = sd(daily.ret2.12$Daily)*-2,xend = sd(daily.ret2.12$Daily)*-2,
                         yend = 5,y = 0,lty='s of YoY'),color = "blue",size = .5)+
  scale_color_manual(values=c( "black", '#3A8026', "#7F8931", "#C4933D","#C47422", "#C45608", 
                              "#B12F08", "#9E0808", "#80262D"),
                     labels = c(' Last 12 Months ',' 2011   ',' 2012   ',' 2013   ',' 2014   ',' 2015   ',' 2016   ',
                                ' 2017   ',' 2018   '))+
  scale_x_continuous(breaks = seq(-.1,.1,.05), 
                     labels = paste0(seq(-.1,.1,.05)*100,'%'))+
  scale_y_continuous(breaks = seq(0,50,5))+
  scale_linetype_manual('segment legend',values = c('s of YoY'=4))+
  labs(x='\nReturn\n',y='Density\n',title=paste0(asset, ' Daily Return Densities\n'))+
  theme(plot.title=element_text(hjust=.5,size=44-max(0,nchar(asset)-6),face='bold'),
        axis.text=element_text(size=20,color='black',face='bold'),
        axis.title=element_text(size=28,color='black',face='bold'),
        legend.text=element_text(size=18,color='black',face='bold'),
        legend.title=element_blank(),
        legend.position = 'bottom',
        plot.margin=unit(c(1,1,1,1),"cm"))

So far I have tried adding two theme functions but that won't work, and adding directions directly into the scale_line_manual any other ideas of what could work?

Thank you for your time regardless of your ability to help.

L.Zingg
  • 85
  • 1
  • 10
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. It's hard to test plotting code without data. – MrFlick Sep 27 '19 at 19:15
  • Is this what you were looking for? Thank you for pointing that out, from now on I will be sure to use simpler examples instead of my actual code. – L.Zingg Sep 27 '19 at 21:21
  • The code in the edited question is different from what you want. Use the original code and provide some data. In your case this is easily done with ```getSymbols()``` from ```quantmod``` like this ```getSymbols("JPM", from = "2011-01-01", to = "2019-12-31", auto.assign = TRUE)```. Then show how you create ```daily.ret2.df``` and ```daily.ret2.12``` and what's in them. This makes it easier to reproduce your graph and test the code. – Mr.Rlover Sep 27 '19 at 22:35
  • There you go, for a general rule, is it better to give all this information or, just the minimal, maybe not, but I feel like I am giving you too much, and it takes to much of your time up. – L.Zingg Sep 27 '19 at 22:47

0 Answers0