gpt4 book ai didi

R - geom_line() 将点转换为垂直线

转载 作者:行者123 更新时间:2023-12-04 13:03:25 27 4
gpt4 key购买 nike

我正在融化一个数据框,以便在 ggplot2 中绘制它。然而,geom_line()没有给我水平线。相反,它正在将我的点转换为垂直线。我将附上图片并添加代码以进一步说明:

仅使用 geom_point() :

vecmmelt = melt(tail(tempdf,7), id.vars = "Date")

ggplot(vecmmelt, aes(x = Date, y = value, colour = variable, group=1)) +geom_point()+theme_bw()

Using geom point only

现在我希望在使用 geom_line() 时有以下内容:

Expectation from + geom_line

通过使用以下代码,我真正得到的是下图:
vecmmelt = melt(tail(tempdf,7), id.vars = "Date")

ggplot(vecmmelt, aes(x = Date, y = value, colour = variable, group=1)) +geom_point()+geom_line()

Reality that is wrong

我尝试使用 geom_path()这也是错误的

最佳答案

看起来问题在于您指定了 group=1 ,它告诉 ggplot 连接所有的点。如 Mario Barbé解释一个类似的问题:

For line graphs, the data points must be grouped so that it knows which points to connect. In this case, it is simple -- all points should be connected, so group=1. When more variables are used and multiple lines are drawn, the grouping for lines is usually done by variable.



引用: Cookbook for R, Chapter: Graphs Bar_and_line_graphs_(ggplot2), Line graphs.

因此,在您的情况下,您应该指定 group=variable解决问题。

关于R - geom_line() 将点转换为垂直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47774442/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com