R 连接点

R 连接点,r,ggplot2,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")),

我有下面的情节

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

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

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

这将以x作为因子类别的整数值绘制点:

 ggplot(dtf, aes(x = as.numeric(Component), y = Variance)) +
      geom_point() + geom_line()
您可以使用以下各项将标签放回类别:

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

(+1)也许你应该解释一下为什么会发生这种情况。我要花很长时间才能解决这个问题!如果您使用一种额外的美学方法来比较来自同一轴上不同条件的数据,例如
x=Throttle,y=Acceleration,color=Widget
,您可以添加
group=Widget
来绘制连接相同Widget点的线。谢谢。这样做的主要问题是,它还需要更改x轴标签,以避免显示“as.numeric(Component)”同意。这当然不是最优的。需要2个修复,到目前为止我只应用了一个。你应该接受另一个答案。