连接点

P S*_*laz 7 r ggplot2

我有以下情节

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我只想用直线连接点.我试过+geom_line()但是产生了一个错误

Did*_*rts 21

您的x值是离散的(因子),geom_line()每个唯一x值都被视为单独的组,并尝试仅在此组内连接点.设置group=1在aes()确保所有值作为一个组处理.

ggplot(dtf, aes(x = Component, y = Variance,group=1)) +
  geom_point()+geom_line()
Run Code Online (Sandbox Code Playgroud)

  • 如果你正在使用额外的美学来比较同一轴上不同条件下的数据,例如`x = Throttle,y = Acceleration,color = Widget`,你可以添加`group = Widget`来绘制连接相同点的线条小部件. (2认同)