Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:将水平线添加到ggplot2_R_Ggplot2_Data Visualization - Fatal编程技术网

R:将水平线添加到ggplot2

R:将水平线添加到ggplot2,r,ggplot2,data-visualization,R,Ggplot2,Data Visualization,我正在使用R中的ggplot2库 假设我有一个这样的图: library(ggplot2) ggplot(work) + geom_line(aes(x = var1, y = var2, group = 1)) + theme(axis.text.x = element_text(angle = 90)) + ggtitle("sample graph") 有没有办法直接在这张图上加第二行 e、 g 谢谢确实有

我正在使用R中的ggplot2库

假设我有一个这样的图:

library(ggplot2)

ggplot(work) + geom_line(aes(x = var1, y = var2, group = 1)) +
               theme(axis.text.x = element_text(angle = 90)) +
               ggtitle("sample graph")
有没有办法直接在这张图上加第二行

e、 g

谢谢

确实有可能:

library(ggplot2)
ggplot(mtcars, aes(x = mpg)) +
        geom_line(aes(y = hp), col = "red") +
        geom_line(aes(y = mean(hp)), col = "blue")

但是,对于特定的水平线,我将使用
geom_hline
intstead:

ggplot(mtcars, aes(x = mpg, y = hp)) +
        geom_line(col = "blue") +
        geom_hline(yintercept = mean(mtcars$hp), col = "red")

确实有可能:

library(ggplot2)
ggplot(mtcars, aes(x = mpg)) +
        geom_line(aes(y = hp), col = "red") +
        geom_line(aes(y = mean(hp)), col = "blue")

但是,对于特定的水平线,我将使用
geom_hline
intstead:

ggplot(mtcars, aes(x = mpg, y = hp)) +
        geom_line(col = "blue") +
        geom_hline(yintercept = mean(mtcars$hp), col = "red")

如果您创建一个小的可复制示例以及预期输出,则会更容易提供帮助。阅读。你的代码中有一些拼写错误:遗忘的
-它似乎与上面的编辑一起工作。正如@Ronak Shah所建议的,如果MRE仍然不能像您所希望的那样工作,请提供它。
geom_hline(yintercept=mean(var2))
?如果您创建一个小的可复制示例以及预期的输出,将更容易提供帮助。阅读。你的代码中有一些拼写错误:遗忘的
-它似乎与上面的编辑一起工作。按照@Ronak Shah的建议,如果MRE仍然不能按照您的意愿工作,请提供它。
geom_hline(yintercept=mean(var2))