[英]Positioning of label text in ggplot2 in R
我正在使用geom_smooth
制作折線圖並使用geom_smooth
對其進行geom_dl
。 這些線還具有geom_points
作為附加顯示。 文本位於點上,我希望文本位於平滑線的尾部。 我的代碼如下。
library(ggplot2)
library(directlabels)
ggplot(gedu5, aes(x = Year, y = gedu, colour = Country, group = Country)) +
geom_smooth(method = "lm", formula = y ~ poly(x, 3), se = FALSE)+
geom_point(stat = "identity") +
scale_colour_discrete(guide = 'none') +
geom_dl(aes(label = Country), method = list(dl.combine("first.points", "last.points"), cex = 1)) +
labs(title = "Government expenditure on education, total (% of GDP)", cex.main = 2.5) +
theme(axis.title = element_blank()) +
facet_wrap(.~Region, 2, scales="free")
它呈現的圖像如下所示。 正如你在圖片中看到的 標簽實際上是固定在點上而不是線上。 我想將標簽貼在線條上而不是點上。
上圖的數據在這里:
structure(list(Year = c(1998L, 1999L, 2000L, 2001L, 2002L, 2003L,
2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L,
2013L, 2014L, 2015L, 2016L, 2017L, 1998L, 1999L, 2000L, 2001L,
2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L,
2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 1998L, 1999L,
2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L,
2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 2017L,
1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L,
2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L,
2016L, 2017L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L,
2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L,
2014L, 2015L, 2016L, 2017L, 1998L, 1999L, 2000L, 2001L, 2002L,
2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L,
2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 1998L, 1999L, 2000L,
2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L,
2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 1998L,
1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L,
2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 2016L,
2017L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L,
2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L,
2015L, 2016L, 2017L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L,
2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L,
2013L, 2014L, 2015L, 2016L, 2017L, 1998L, 1999L, 2000L, 2001L,
2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L,
2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 1998L, 1999L,
2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L,
2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 2017L,
1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L,
2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L,
2016L, 2017L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L,
2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L,
2014L, 2015L, 2016L, 2017L, 1998L, 1999L, 2000L, 2001L, 2002L,
2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L,
2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 1998L, 1999L, 2000L,
2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L,
2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 1998L,
1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L,
2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 2016L,
2017L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L,
2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L,
2015L, 2016L, 2017L), Country = c("Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Bangladesh", "Bangladesh",
"Bangladesh", "Bangladesh", "Bangladesh", "Bangladesh", "Bangladesh",
"Bangladesh", "Bangladesh", "Bangladesh", "Bangladesh", "Bangladesh",
"Bangladesh", "Bangladesh", "Bangladesh", "Bangladesh", "Bangladesh",
"Bangladesh", "Bangladesh", "Bangladesh", "Bhutan", "Bhutan",
"Bhutan", "Bhutan", "Bhutan", "Bhutan", "Bhutan", "Bhutan", "Bhutan",
"Bhutan", "Bhutan", "Bhutan", "Bhutan", "Bhutan", "Bhutan", "Bhutan",
"Bhutan", "Bhutan", "Bhutan", "Bhutan", "India", "India", "India",
"India", "India", "India", "India", "India", "India", "India",
"India", "India", "India", "India", "India", "India", "India",
"India", "India", "India", "Maldives", "Maldives", "Maldives",
"Maldives", "Maldives", "Maldives", "Maldives", "Maldives", "Maldives",
"Maldives", "Maldives", "Maldives", "Maldives", "Maldives", "Maldives",
"Maldives", "Maldives", "Maldives", "Maldives", "Maldives", "Nepal",
"Nepal", "Nepal", "Nepal", "Nepal", "Nepal", "Nepal", "Nepal",
"Nepal", "Nepal", "Nepal", "Nepal", "Nepal", "Nepal", "Nepal",
"Nepal", "Nepal", "Nepal", "Nepal", "Nepal", "Pakistan", "Pakistan",
"Pakistan", "Pakistan", "Pakistan", "Pakistan", "Pakistan", "Pakistan",
"Pakistan", "Pakistan", "Pakistan", "Pakistan", "Pakistan", "Pakistan",
"Pakistan", "Pakistan", "Pakistan", "Pakistan", "Pakistan", "Pakistan",
"Sri_Lanka", "Sri_Lanka", "Sri_Lanka", "Sri_Lanka", "Sri_Lanka",
"Sri_Lanka", "Sri_Lanka", "Sri_Lanka", "Sri_Lanka", "Sri_Lanka",
"Sri_Lanka", "Sri_Lanka", "Sri_Lanka", "Sri_Lanka", "Sri_Lanka",
"Sri_Lanka", "Sri_Lanka", "Sri_Lanka", "Sri_Lanka", "Sri_Lanka",
"World", "World", "World", "World", "World", "World", "World",
"World", "World", "World", "World", "World", "World", "World",
"World", "World", "World", "World", "World", "World", "South_Asia",
"South_Asia", "South_Asia", "South_Asia", "South_Asia", "South_Asia",
"South_Asia", "South_Asia", "South_Asia", "South_Asia", "South_Asia",
"South_Asia", "South_Asia", "South_Asia", "South_Asia", "South_Asia",
"South_Asia", "South_Asia", "South_Asia", "South_Asia", "Sub.Saharan_Africa",
"Sub.Saharan_Africa", "Sub.Saharan_Africa", "Sub.Saharan_Africa",
"Sub.Saharan_Africa", "Sub.Saharan_Africa", "Sub.Saharan_Africa",
"Sub.Saharan_Africa", "Sub.Saharan_Africa", "Sub.Saharan_Africa",
"Sub.Saharan_Africa", "Sub.Saharan_Africa", "Sub.Saharan_Africa",
"Sub.Saharan_Africa", "Sub.Saharan_Africa", "Sub.Saharan_Africa",
"Sub.Saharan_Africa", "Sub.Saharan_Africa", "Sub.Saharan_Africa",
"Sub.Saharan_Africa", "Middle_income", "Middle_income", "Middle_income",
"Middle_income", "Middle_income", "Middle_income", "Middle_income",
"Middle_income", "Middle_income", "Middle_income", "Middle_income",
"Middle_income", "Middle_income", "Middle_income", "Middle_income",
"Middle_income", "Middle_income", "Middle_income", "Middle_income",
"Middle_income", "Central_Europe_and_the_Baltics", "Central_Europe_and_the_Baltics",
"Central_Europe_and_the_Baltics", "Central_Europe_and_the_Baltics",
"Central_Europe_and_the_Baltics", "Central_Europe_and_the_Baltics",
"Central_Europe_and_the_Baltics", "Central_Europe_and_the_Baltics",
"Central_Europe_and_the_Baltics", "Central_Europe_and_the_Baltics",
"Central_Europe_and_the_Baltics", "Central_Europe_and_the_Baltics",
"Central_Europe_and_the_Baltics", "Central_Europe_and_the_Baltics",
"Central_Europe_and_the_Baltics", "Central_Europe_and_the_Baltics",
"Central_Europe_and_the_Baltics", "Central_Europe_and_the_Baltics",
"Central_Europe_and_the_Baltics", "Central_Europe_and_the_Baltics",
"Euro_area", "Euro_area", "Euro_area", "Euro_area", "Euro_area",
"Euro_area", "Euro_area", "Euro_area", "Euro_area", "Euro_area",
"Euro_area", "Euro_area", "Euro_area", "Euro_area", "Euro_area",
"Euro_area", "Euro_area", "Euro_area", "Euro_area", "Euro_area",
"Middle_East_North_Africa", "Middle_East_North_Africa", "Middle_East_North_Africa",
"Middle_East_North_Africa", "Middle_East_North_Africa", "Middle_East_North_Africa",
"Middle_East_North_Africa", "Middle_East_North_Africa", "Middle_East_North_Africa",
"Middle_East_North_Africa", "Middle_East_North_Africa", "Middle_East_North_Africa",
"Middle_East_North_Africa", "Middle_East_North_Africa", "Middle_East_North_Africa",
"Middle_East_North_Africa", "Middle_East_North_Africa", "Middle_East_North_Africa",
"Middle_East_North_Africa", "Middle_East_North_Africa", "Latin_America_Caribbean",
"Latin_America_Caribbean", "Latin_America_Caribbean", "Latin_America_Caribbean",
"Latin_America_Caribbean", "Latin_America_Caribbean", "Latin_America_Caribbean",
"Latin_America_Caribbean", "Latin_America_Caribbean", "Latin_America_Caribbean",
"Latin_America_Caribbean", "Latin_America_Caribbean", "Latin_America_Caribbean",
"Latin_America_Caribbean", "Latin_America_Caribbean", "Latin_America_Caribbean",
"Latin_America_Caribbean", "Latin_America_Caribbean", "Latin_America_Caribbean",
"Latin_America_Caribbean", "North_America", "North_America",
"North_America", "North_America", "North_America", "North_America",
"North_America", "North_America", "North_America", "North_America",
"North_America", "North_America", "North_America", "North_America",
"North_America", "North_America", "North_America", "North_America",
"North_America", "North_America", "Low_income", "Low_income",
"Low_income", "Low_income", "Low_income", "Low_income", "Low_income",
"Low_income", "Low_income", "Low_income", "Low_income", "Low_income",
"Low_income", "Low_income", "Low_income", "Low_income", "Low_income",
"Low_income", "Low_income", "Low_income"), gedu = c(3.5, 3.8,
3.7, 3.3, 2.9, 2.6, 2.7, 3, 3.4, 3.7, 3.8, 3.7, 3.5, 3.5, 2.6,
3.5, 3.7, 3.3, 4.2, 4.1, 2.1, 2.1, 2.1, 2.2, 2, 2.1, 1.9, 2.1,
2.1, 2.2, 2, 1.9, 2, 2.1, 2.2, 2, 1.5, 1.3, 1.5, 2.5, 5.5, 5.7,
5.5, 5.7, 6.2, 6.8, 6.4, 7.1, 5.4, 4.8, 4.8, 4.6, 4, 4.7, 5.5,
5.6, 5.9, 7.3, 6.8, 7, 3.6, 4.4, 4.3, 4.2, 4, 3.6, 3.4, 3.2,
3.1, 3.2, 3.1, 3.3, 3.4, 3.8, 3.9, 3.8, 3.7, 3.9, 3.9, 3.8, 5.8,
5.5, 4.9, 4.7, 5.8, 5.3, 4.7, 5, 4.7, 4.4, 4.5, 5.8, 4.1, 3.5,
3.8, 3.8, 3.5, 4.4, 4.1, 4.1, 2.9, 2.9, 3, 3.7, 3.1, 3.1, 3.2,
3.4, 3.6, 3.5, 3.8, 4.7, 3.6, 3.8, 3.8, 3.5, 4, 3.7, 4.4, 5.5,
2.6, 2.6, 1.8, 1.5, 1.6, 1.9, 2, 2.2, 2.6, 2.6, 2.8, 2.6, 2.3,
2.2, 2.1, 2.5, 2.5, 2.6, 3, 2.9, 3, 5.2, 6.6, 7.3, 7.5, 7.2,
6.6, 5.7, 4.7, 3.7, 2.8, 2.1, 1.7, 1.8, 1.5, 1.6, 1.9, 2.2, 3.5,
2.8, 4.2, 4.2, 3.9, 4, 4, 4.1, 4, 4.1, 4.2, 4.2, 4.3, 4.7, 4.5,
4.3, 4.3, 4.6, 4.6, 4.7, 4.5, 4.5, 3, 2.8, 2.5, 2.8, 3.2, 3.1,
2.6, 3.2, 2.9, 2.6, 2.8, 2.6, 3.4, 2.8, 2.4, 3, 3.1, 3, 3.5,
2.9, 3.3, 3.3, 3.1, 3.4, 3.1, 3.2, 3.3, 3.6, 3.6, 3.5, 3.6, 3.9,
3.8, 3.9, 4.1, 4, 4.3, 4.5, 3.9, 4.5, 4.1, 4.1, 3.6, 3.7, 3.4,
3.6, 3.5, 3.9, 3.9, 3.6, 4, 4.5, 4.5, 4, 4.2, 4.3, 4.4, 4.3,
4.2, 4.4, 5, 4.6, 4.9, 5.1, 5.2, 5.1, 4.9, 4.8, 4.7, 4.6, 4.8,
5, 4.8, 4.6, 4.5, 4.6, 4.6, 4.8, 4.7, 4.7, 4.5, 4.8, 4.6, 5.2,
5.3, 5.1, 5, 5, 4.8, 4.7, 5.1, 5.6, 5.5, 5.1, 5, 5.3, 5.3, 4.9,
4.8, 4.8, 6.2, 6.3, 6.3, 6.2, 6.1, 6, 5.4, 5.6, 4.3, 5.2, 4.5,
4.7, 4.5, 4.4, 4.2, 4.2, 4.1, 4.2, 4.3, 4.5, 3.3, 3.8, 3.8, 3.8,
3.9, 3.5, 3.8, 3.9, 3.8, 3.9, 4.6, 5.3, 4.7, 4.4, 4.4, 5, 5.3,
4.9, 4.5, 4.5, 5.5, 5.5, 5.4, 5, 5, 5, 5, 4.8, 4.8, 4.8, 4.6,
4.8, 5.4, 5.3, 5.3, 4.9, 5, 5, 4.9, 5, 3.2, 3.2, 2.9, 3.3, 3.4,
3.2, 3.1, 3.1, 3.3, 3.4, 3.4, 3.5, 3.5, 3.8, 3.8, 3.1, 3.3, 3.8,
3.1, 3.5), Region = c("South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "South Asia", "South Asia", "South Asia",
"South Asia", "South Asia", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region",
"Region", "Region", "Region", "Region", "Region", "Region", "Region"
)), row.names = c(NA, -360L), class = "data.frame")
問題是, geom_dl
使用的值gedu
在你的數據集,並通過計算不擬合值geom_smooth
。
我試過
將stat="smooth"
傳遞給geom_dl
導致圖上沒有出現標簽
使用帶有geom="text"
stat_smooth
在端點手動添加標簽,但不起作用
因此,我能找到的唯一解決方案是手動計算擬合值,然后可以將其映射到geom_dl
y
上。 用於計算擬合值我
tidyr::nest
按Country
和Region
tidyr::nest
dfpurrr::map
和broom::augment
進行估計並獲得擬合值tidyr::unnest
並只保留所需的列library(ggplot2)
library(directlabels)
library(purrr)
library(broom)
library(tidyr)
library(dplyr)
gedu5 %>%
# Manually compute the fitted values
nest(data = -c(Region, Country)) %>%
mutate(mod = map(data, ~ lm(gedu ~ poly(Year, 3), data = .x)),
mod = map2(mod, data, augment)) %>%
unnest(mod) %>%
select(Country, Region, Year, gedu, .fitted) %>%
ggplot(aes(x = Year, y = gedu, colour = Country, group = Country)) +
geom_smooth(method = "lm", formula = y ~ poly(x, 3), se = FALSE) +
geom_dl(aes(y = .fitted, label = Country), method = list(dl.combine("first.points", "last.points"), cex = 1)) +
geom_point(stat = "identity") +
scale_colour_discrete(guide = "none") +
labs(title = "Government expenditure on education, total (% of GDP)", cex.main = 2.5) +
theme(axis.title = element_blank()) +
facet_wrap(.~Region, 2, scales="free")
這是一種通過為directlabels
創建自定義method
來directlabels
method
。
自定義方法是帶有參數d
和...
函數,它們返回一個數據框。 d
是一個包含x
、 y
、 groups
和一些其他列的數據框。
創建一個為每個組創建lm
的自定義方法。 保留第一個和最后一個值並適當設置hjust
。
library(tidyverse)
library(directlabels)
first_last_poly3 <- function(d, ...) {
d %>%
group_by(groups) %>%
group_modify(function(g, ...) {
mutate(g, y = predict(lm(y ~ poly(x, 3), data = g)))
}) %>%
filter(row_number() %in% c(1, n())) %>%
mutate(hjust = ifelse(row_number() == 1, 1, 0))
}
創建繪圖並使用first_last_poly3
作為geom_dl
method
。
ggplot(gedu5, aes(x = Year, y = gedu, colour = Country, group = Country)) +
geom_smooth(method = "lm", formula = y ~ poly(x, 3), se = FALSE)+
geom_point(stat = "identity") +
scale_colour_discrete(guide = 'none') +
geom_dl(aes(label = Country), method = first_last_poly3) +
labs(title = "Government expenditure on education, total (% of GDP)", cex.main = 2.5) +
theme(axis.title = element_blank()) +
facet_wrap(.~Region, 2, scales="free")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.