connecting points

后端 未结 2 1675
抹茶落季
抹茶落季 2020-12-17 23:51

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 =         


        
相关标签:
2条回答
  • 2020-12-18 00:00

    This would plot the points with x as the integer values of the factor categories:

     ggplot(dtf, aes(x = as.numeric(Component), y = Variance)) +
          geom_point() + geom_line()
    

    You can put back in the category labels with:

    ggplot(dtf, aes(x = as.numeric(Component), y = Variance)) +
      geom_point() +geom_line() + scale_x_discrete(labels=dtf$Component)
    
    0 讨论(0)
  • 2020-12-18 00:07

    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()
    
    0 讨论(0)
提交回复
热议问题