当我想用变量“拆分”数据时,我很难用color()代替facet_grid()。我希望不使用回归线生成单个图,而是使用所有回归线生成一个图。
这是我的代码:
ggplot(data, aes(x = Rooms, y = Price)) +
geom_point(size = 1, alpha = 1/100) +
geom_smooth(method = "lm", color = Type) # Single plot with all regression lines
ggplot(data, aes(x = Rooms, y = Price)) +
geom_point(size = 1, alpha = 1/100) +
geom_smooth(method = "lm") + facet_grid(. ~ Type) # Individual plots with regression lines
(第一个图不起作用)这是输出: “ grDevices :: col2rgb(colour,TRUE)中的错误:无效的颜色名称'Type' 另外:警告消息: 1:删除了包含非限定值(stat_smooth)的12750行。 2:删除了包含缺失值(geom_point)的12750行。”
以下是数据链接: Dataset
答案 0 :(得分:1)
您需要提供到geom_smooth
的美观映射,而不仅是参数,这意味着您需要将colour
放在aes()
内。每当您要使图形元素与数据中的某物而不是固定参数相对应时,便需要执行此操作。
这是内置iris
数据集的示例。实际上,如果将colour
移至ggplot
调用,使其也被geom_point
继承,则可以为点和线着色。
library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point() +
geom_smooth(aes(colour = Species), method = "lm")
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species)) +
geom_point() +
geom_smooth(method = "lm")
由reprex package(v0.2.0)于2018-07-20创建。