![](/img/trans.png)
[英]How to color only for points above and below certain amounts in ggplot2?
[英]How to label only certain points in ggplot2
我有一個 dataframe global
三列Year
, MTCO2
和Scenario
。 我已經創建了一個散點圖,看起來我想要它,但我正在努力解決如何在 plot 上僅幾個點來最好地優化 label。
到目前為止,這是我的代碼:
plot1 <- global %>%
ggplot(aes(Year, MTCO2, group = Scenario, colour = Scenario)) +
geom_line(size = 1) +
geom_point(size = 2) +
scale_x_continuous(name = "Year", breaks = c(1990, 2000, 2006, 2020, 2030, 2040, 2050)) +
theme_bw() +
labs(title = "Global CO2 Emissions Projections with and without Constraints")
plot1a <- plot1 + geom_text(data = subset(global, Year == 2006 | Year == 2030 | Year == 2050 ),
aes(Year, MTCO2, label = MTCO2))
我的目標是要使文本是黑色的,從一條線連接到該點的角度,有一個label在2006年,一個ZD304BA20E96DBA20E96D87411588EBAAC8YEABAC8550E11 for 2030 for 2030 for 2030 and888888888888888888888888888888888888888888808.僅限 Global_Constraint 行)。 我還希望能夠在點值之后寫更多(即 8261,基准年值)。 圖例還在點上顯示了字母“a”,我也想去掉它。
這是數據:
dput(global)
structure(list(Year = c(1990, 2005, 2006, 2010, 2015, 2020, 2025,
2030, 2035, 2040, 2045, 2050, 1990, 2005, 2006, 2010, 2015, 2020,
2025, 2030, 2035, 2040, 2045, 2050), MTCO2 = c(6045, 8039, 8261,
9149, 9646, 10453, 7889, 6937, 5467, 4400, 2589, 2432, 6045,
8039, 8261, 9149, 9646, 10528, 11488, 12368, 13092, 13708, 14305,
14754), Scenario = c("Global_Constraint", "Global_Constraint",
"Global_Constraint", "Global_Constraint", "Global_Constraint",
"Global_Constraint", "Global_Constraint", "Global_Constraint",
"Global_Constraint", "Global_Constraint", "Global_Constraint",
"Global_Constraint", "Global_Reference", "Global_Reference",
"Global_Reference", "Global_Reference", "Global_Reference", "Global_Reference",
"Global_Reference", "Global_Reference", "Global_Reference", "Global_Reference",
"Global_Reference", "Global_Reference")), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -24L), spec = structure(list(
cols = list(Year = structure(list(), class = c("collector_double",
"collector")), MTCO2 = structure(list(), class = c("collector_double",
"collector")), Scenario = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
我想有幾種方法可以解決它。 只需對您的代碼進行最少的更改,我就嘗試過:
library(ggplot2)
plot1 <- global %>%
ggplot(aes(Year, MTCO2, group = Scenario, colour = Scenario)) +
geom_line(size = 1) +
geom_point(size = 2) +
scale_x_continuous(name = "Year", breaks = c(1990, 2000, 2006, 2020, 2030, 2040, 2050)) +
theme_bw() +
labs(title = "Global CO2 Emissions Projections with and without Constraints")
plot1a <- plot1 + geom_text(data = subset(global, Year == 2006 | Year == 2030 | Year == 2050 ),
aes(Year, MTCO2, label = MTCO2),
color = "black", nudge_x = -6)
我認為最簡單的方法是創建 label,然后設計 plot:
#Format data
global %>% mutate(label=ifelse(Year %in% c(2006,2030,2050),MTCO2,NA)) -> global
#Plot
ggplot(global,aes(x=Year, y=MTCO2,label=label)) +
geom_line(size = 1,aes(color = Scenario)) +
geom_point(size = 2,aes(color = Scenario)) +
geom_text(vjust=-1)+
scale_x_continuous(name = "Year", breaks = c(1990, 2000, 2006, 2020, 2030, 2040, 2050)) +
theme_bw() +
labs(title = "Global CO2 Emissions Projections with and without Constraints")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.