ggplot2:将 stat_smooth 回归线扩展到绘图区域的整个 x 范围
ggplot2: Extend stat_smooth regression line to entire x-range of plot area
我的 ggplot 中有两条独立的回归线,每条回归线对应一个单独的变量。但是,对应于 local
的第二行并未延伸到整个图形。是否有解决此问题的方法或使两个 ablines 在图形区域中均等延伸的方法?
ggplot(metrics, aes(x=popDensity, y= TPB, color = factor(type))) + geom_point() +theme_minimal() + stat_smooth(method = "lm", se = FALSE) +
geom_label_repel(aes(label= rownames(metrics)), size=3, show.legend = FALSE) +
theme(axis.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=12)) +
labs(x = expression(paste( "Populatin Density ", km^{2})), y = expression(paste("Rating")))+
theme(legend.position="top", legend.direction="horizontal") + theme(legend.title=element_blank())
这是数据示例:
> dput(metrics)
structure(list(popDensity = c(4308, 27812, 4447, 5334, 4662,
2890, 1689, 481, 4100), TPB = c(2.65, 4.49, 2.37, 2.87, 3.87,
2.95, 1.18, 1.62, 1.87), type = c("Global", "Global", "Global",
"Global", "Global", "Global", "Local", "Local", "Local")), .Names = c("popDensity",
"TPB", "type"), row.names = c("City1", "City2", "City3", "City4",
"City5", "City6", "City7", "City8", "City9"), class = "data.frame")
将 fullrange = T
添加到 stat_smooth
将使拟合跨越绘图的整个范围:
ggplot(metrics, aes(x = popDensity, y = TPB, color = factor(type))) +
geom_point() +
theme_minimal() +
stat_smooth(method = "lm", se = FALSE, fullrange = T) +
geom_label_repel(aes(label = rownames(metrics)),
size = 3,
show.legend = FALSE) +
theme(axis.title = element_text(
family = "Trebuchet MS",
color = "#666666",
face = "bold",
size = 12
)) +
labs(x = expression(paste("Populatin Density ", km ^ {2})),
y = expression(paste("Rating"))) +
theme(legend.position = "top", legend.direction = "horizontal") +
theme(legend.title = element_blank())
我的 ggplot 中有两条独立的回归线,每条回归线对应一个单独的变量。但是,对应于 local
的第二行并未延伸到整个图形。是否有解决此问题的方法或使两个 ablines 在图形区域中均等延伸的方法?
ggplot(metrics, aes(x=popDensity, y= TPB, color = factor(type))) + geom_point() +theme_minimal() + stat_smooth(method = "lm", se = FALSE) +
geom_label_repel(aes(label= rownames(metrics)), size=3, show.legend = FALSE) +
theme(axis.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=12)) +
labs(x = expression(paste( "Populatin Density ", km^{2})), y = expression(paste("Rating")))+
theme(legend.position="top", legend.direction="horizontal") + theme(legend.title=element_blank())
这是数据示例:
> dput(metrics)
structure(list(popDensity = c(4308, 27812, 4447, 5334, 4662,
2890, 1689, 481, 4100), TPB = c(2.65, 4.49, 2.37, 2.87, 3.87,
2.95, 1.18, 1.62, 1.87), type = c("Global", "Global", "Global",
"Global", "Global", "Global", "Local", "Local", "Local")), .Names = c("popDensity",
"TPB", "type"), row.names = c("City1", "City2", "City3", "City4",
"City5", "City6", "City7", "City8", "City9"), class = "data.frame")
将 fullrange = T
添加到 stat_smooth
将使拟合跨越绘图的整个范围:
ggplot(metrics, aes(x = popDensity, y = TPB, color = factor(type))) +
geom_point() +
theme_minimal() +
stat_smooth(method = "lm", se = FALSE, fullrange = T) +
geom_label_repel(aes(label = rownames(metrics)),
size = 3,
show.legend = FALSE) +
theme(axis.title = element_text(
family = "Trebuchet MS",
color = "#666666",
face = "bold",
size = 12
)) +
labs(x = expression(paste("Populatin Density ", km ^ {2})),
y = expression(paste("Rating"))) +
theme(legend.position = "top", legend.direction = "horizontal") +
theme(legend.title = element_blank())