Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connecting points

Tags:

r

ggplot2

I have the following plot

require(ggplot2)

dtf <- structure(list(Variance = c(5.213, 1.377, 0.858, 0.613, 0.412, 0.229, 0.139, 0.094, 0.064), Component = structure(1:9, .Label = c("PC1", "PC2", "PC3", "PC4", "PC5", "PC6", "PC7", "PC8", "PC9"), class = "factor")), .Names = c("Variance", "Component"), row.names = c(NA, -9L), class = "data.frame")

ggplot(dtf, aes(x = Component, y = Variance)) +
geom_point()

enter image description here

I would simply like to connect the dots with straight lines. I tried +geom_line() but that generated an error

like image 389
LeelaSella Avatar asked Feb 23 '13 18:02

LeelaSella


1 Answers

Your x values are discrete (factor) and geom_line() each unique x value perceive as separate group and tries to connect points only inside this group. Setting group=1 in aes() ensures that all values are treated as one group.

ggplot(dtf, aes(x = Component, y = Variance,group=1)) +
  geom_point()+geom_line()
like image 148
Didzis Elferts Avatar answered Oct 14 '22 09:10

Didzis Elferts