簡體   English   中英

ggplot2:將stat_smooth回歸線擴展到繪圖區域的整個x范圍

[英]ggplot2: Extend stat_smooth regression line to entire x-range of plot area

我的ggplot中有兩條單獨的回歸線,每條回歸線對應一個單獨的變量。 但是,與local相對應的第二條線沒有延伸到整個圖。 是否有解決方法,或者是否使兩條斜線均等地延伸到整個圖形區域?

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")

stat_smooth添加fullrange = T將使擬合跨度覆蓋圖的整個范圍:

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())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM