Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

r adding line (new coordinates) to ggplot2 scaterplot

How can I add a line with new x coordinates to my scatterplot? I get an error saying the line x-values must match those from my scatterplot.. I tried using geom_line() as you can see

library(ggplot2)

x <- c(1,2,3)
y <- c(4,5,6)

a <- seq(0.5,5, by = 0.5)
b <- seq(1,10)


ggplot(as.data.frame(cbind(x,y)), aes(x, y)) +
  geom_point(shape = 1) +
  geom_point(aes(2.5,2.5, colour = "My Portfolio"), 
             shape = 18, 
             size = 3) +
  geom_line(aes(a, b)) +
  ggtitle("Efficient Frontier") +
  xlab("Volatility (Weekly)") +
  ylab("Expected Returns (Weekly)") +
  theme(plot.title = element_text(size=14, face="bold.italic", hjust = 0.5, margin=margin(0,0,15,0)),
        axis.title.x = element_text(size = 10, margin=margin(15,0,0,0)),
        axis.title.y = element_text(size = 10, margin=margin(0,15,0,0)),
        panel.border = element_rect(colour = "black", fill=NA, size=1),
        legend.position = c(0.93,0.06),
        legend.title = element_blank(),
        legend.text = element_text(size=8),
        legend.background = element_rect(color = "black"),
        legend.key=element_blank())

thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have to provide a new data.frame to geom_line() and set the aestetics accordingly.

library(ggplot2)

x <- c(1,2,3); y <- c(4,5,6)
a <- seq(0.5,5, by = 0.5); b <- seq(1,10)

ggplot(as.data.frame(cbind(x,y)), aes(x, y)) +
  geom_point(shape = 1) +
  geom_point(aes(2.5,2.5, colour = "My Portfolio"), 
             shape = 18, 
             size = 3) +
  geom_line(data=data.frame(a,b), mapping=aes(x=a, y=b)) +
  ggtitle("Efficient Frontier") +
  xlab("Volatility (Weekly)") +
  ylab("Expected Returns (Weekly)") +
  theme(plot.title = element_text(size=14, face="bold.italic", hjust = 0.5, margin=margin(0,0,15,0)),
        axis.title.x = element_text(size = 10, margin=margin(15,0,0,0)),
        axis.title.y = element_text(size = 10, margin=margin(0,15,0,0)),
        panel.border = element_rect(colour = "black", fill=NA, size=1),
        legend.position = c(0.93,0.06),
        legend.title = element_blank(),
        legend.text = element_text(size=8),
        legend.background = element_rect(color = "black"),
        legend.key=element_blank())

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading