我有以下情节

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()

我只想将点与直线连接起来。我尝试了+geom_line(),但生成了一个错误

最佳答案

您的x值是离散的(因数),而geom_line()的每个唯一x值都被视为单独的组,并尝试仅在该组内连接点。在group=1中设置aes()可确保将所有值都视为一组。

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

关于r - 连接点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15043956/

10-12 17:25