我有以下情节
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()
,但却产生了错误
答案 0 :(得分:21)
您的x
值是离散的(因子),geom_line()
每个唯一x
值都被视为单独的组,并尝试仅在此组内连接点。在group=1
中设置aes()
可确保将所有值视为一个组。
ggplot(dtf, aes(x = Component, y = Variance,group=1)) +
geom_point()+geom_line()
答案 1 :(得分:0)
这会将点绘制为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)