具有实际值而不是日志的 Ggplot2 图例
Ggplot2 legend with actual values instead of log
我使用 ggplot2
创建了一张地图,其中我使用以下代码显示了欧洲国家相对于世界其他地区的贸易平衡:
library(ggplot2)
library(sf)
library(giscoR)
data <- read.csv("~/Downloads/full202052 (1)/full202052.dat")
data2 = data[data$TRADE_TYPE=="E",]
x = aggregate(VALUE_IN_EUROS ~ FLOW + PARTNER_ISO, data2, sum)
x = sapply(split(x, x$PARTNER_ISO), function(x) diff(x$VALUE_IN_EUROS))
x = data.frame(Code = names(unlist(x)), Value = unlist(x))
eu = levels(as.factor(data$DECLARANT_ISO))
eu = gisco_get_countries(epsg = "4326", year = "2020", resolution = "3", country = c(eu[!eu %in% c("GB","GR")], "UK", "EL"))
borders <- gisco_get_countries(epsg = "4326", year = "2020", resolution = "3", country = x$Code)
merged <- merge(borders, x, by.x = "CNTR_ID", by.y = "Code", all.x = TRUE)
Africa <- gisco_get_countries(epsg = "4326", year = "2020", resolution = "3", region = "Africa")
ggplot(merged) +
geom_sf(aes(fill = sign(Value/1000000000)*log(abs(Value/1000000000))), color = NA, alpha = 0.9) +
#geom_sf(aes(fill = Value/1000000000), color = NA, alpha = 0.9) +
geom_sf(data = eu, fill = "deepskyblue4", color = NA, size = 0.1) +
geom_sf(data = Africa, fill = NA, size = 0.1, col = "grey30") +
geom_sf(data = borders, fill = NA, size = 0.1, col = "grey30") +
scale_fill_gradient2(
name = "Euros (Billions)",
guide = guide_legend(
direction = "horizontal",
keyheight = 0.5,
keywidth = 2,
title.position = "top",
title.hjust = 0,
label.hjust = .5,
nrow = 1,
byrow = TRUE,
reverse = FALSE,
label.position = "bottom"
)
) + theme_void()+
labs(
title = "Trade Balance of Europe Vis-à-vis the World (2020)",
subtitle = "(In Billions of Euros)",
caption = paste0("Source: Eurostat")) +
# Theme
theme(
#plot.background = element_rect(fill = "black"),
plot.title = element_text(
color = "black",
hjust = 0.5,
vjust = -1,
),
plot.subtitle = element_text(
color = "black",
hjust = 0.5,
vjust = -2,
face = "bold"
),
plot.caption = element_text(
color = "black",
size = 6,
hjust = 0.5,
margin = margin(b = 2, t = 13)
),
legend.text = element_text(
size = 7,
color = "black"
),
legend.title = element_text(
size = 7,
color = "black"
),
legend.position = c(0.5, 0.02),
)
结果如下图:
地图的图例显示 -5, 0, 5
,但我希望它显示 -200, 0, 200
。谁能给我一个关于如何将图例标签更改为我想要的数字的线索?谢谢。
此问题与 sf
无关。您可以使用颜色或填充比例的 trans
参数,而不是在 aes
语句中转换数据。更多不同的转换可以在 scales
包中找到。如果需要,您还可以在那里找到创建新的说明。这是使用 trans
参数的示例:
library(ggplot2)
df<-data.frame(x=-4:4, y=-4:4, col=exp(-4:4))
ggplot(df) +
geom_point(aes(x=x,y=y,color= sign(col)*log(abs(col))))
ggplot(df) +
geom_point(aes(x=x,y=y,color= col))+
scale_color_continuous(trans='log')
我使用 ggallin
包和 pseudolog10_trans
函数解决了这个问题。更新后的代码如下:
ggplot(merged) +
geom_sf(aes(fill = Value/1000000000), color = NA, alpha = 0.9, size = 0.1) +
geom_sf(data = eu, fill = "deepskyblue4", color = NA, size = 0.1) +
#geom_sf(data = Africa, fill = NA, size = 0.1, col = "grey30") +
#geom_sf(data = borders, fill = NA, size = 0.1, col = "grey30") +
scale_fill_gradient2(
trans = pseudolog10_trans,
name = "Euros (Billions)",
guide = guide_legend(
direction = "horizontal",
keyheight = 0.5,
keywidth = 2,
title.position = "top",
title.hjust = 0,
label.hjust = .5,
nrow = 1,
byrow = TRUE,
reverse = FALSE,
label.position = "bottom"
)
) + theme_void()+
labs(
title = "Trade Balance of Europe Vis-à-vis the World (2020)",
subtitle = "(In Billions of Euros)",
caption = paste0("Source: Eurostat")) +
# Theme
theme(
#plot.background = element_rect(fill = "black"),
plot.title = element_text(
color = "black",
hjust = 0.5,
vjust = -1,
),
plot.subtitle = element_text(
color = "black",
hjust = 0.5,
vjust = -2,
face = "bold"
),
plot.caption = element_text(
color = "black",
size = 6,
hjust = 0.5,
margin = margin(b = 2, t = 13)
),
legend.text = element_text(
size = 7,
color = "black"
),
legend.title = element_text(
size = 7,
color = "black"
),
legend.position = c(0.5, 0.02),
)
它产生了以下地图:
我使用 ggplot2
创建了一张地图,其中我使用以下代码显示了欧洲国家相对于世界其他地区的贸易平衡:
library(ggplot2)
library(sf)
library(giscoR)
data <- read.csv("~/Downloads/full202052 (1)/full202052.dat")
data2 = data[data$TRADE_TYPE=="E",]
x = aggregate(VALUE_IN_EUROS ~ FLOW + PARTNER_ISO, data2, sum)
x = sapply(split(x, x$PARTNER_ISO), function(x) diff(x$VALUE_IN_EUROS))
x = data.frame(Code = names(unlist(x)), Value = unlist(x))
eu = levels(as.factor(data$DECLARANT_ISO))
eu = gisco_get_countries(epsg = "4326", year = "2020", resolution = "3", country = c(eu[!eu %in% c("GB","GR")], "UK", "EL"))
borders <- gisco_get_countries(epsg = "4326", year = "2020", resolution = "3", country = x$Code)
merged <- merge(borders, x, by.x = "CNTR_ID", by.y = "Code", all.x = TRUE)
Africa <- gisco_get_countries(epsg = "4326", year = "2020", resolution = "3", region = "Africa")
ggplot(merged) +
geom_sf(aes(fill = sign(Value/1000000000)*log(abs(Value/1000000000))), color = NA, alpha = 0.9) +
#geom_sf(aes(fill = Value/1000000000), color = NA, alpha = 0.9) +
geom_sf(data = eu, fill = "deepskyblue4", color = NA, size = 0.1) +
geom_sf(data = Africa, fill = NA, size = 0.1, col = "grey30") +
geom_sf(data = borders, fill = NA, size = 0.1, col = "grey30") +
scale_fill_gradient2(
name = "Euros (Billions)",
guide = guide_legend(
direction = "horizontal",
keyheight = 0.5,
keywidth = 2,
title.position = "top",
title.hjust = 0,
label.hjust = .5,
nrow = 1,
byrow = TRUE,
reverse = FALSE,
label.position = "bottom"
)
) + theme_void()+
labs(
title = "Trade Balance of Europe Vis-à-vis the World (2020)",
subtitle = "(In Billions of Euros)",
caption = paste0("Source: Eurostat")) +
# Theme
theme(
#plot.background = element_rect(fill = "black"),
plot.title = element_text(
color = "black",
hjust = 0.5,
vjust = -1,
),
plot.subtitle = element_text(
color = "black",
hjust = 0.5,
vjust = -2,
face = "bold"
),
plot.caption = element_text(
color = "black",
size = 6,
hjust = 0.5,
margin = margin(b = 2, t = 13)
),
legend.text = element_text(
size = 7,
color = "black"
),
legend.title = element_text(
size = 7,
color = "black"
),
legend.position = c(0.5, 0.02),
)
结果如下图:
地图的图例显示 -5, 0, 5
,但我希望它显示 -200, 0, 200
。谁能给我一个关于如何将图例标签更改为我想要的数字的线索?谢谢。
此问题与 sf
无关。您可以使用颜色或填充比例的 trans
参数,而不是在 aes
语句中转换数据。更多不同的转换可以在 scales
包中找到。如果需要,您还可以在那里找到创建新的说明。这是使用 trans
参数的示例:
library(ggplot2)
df<-data.frame(x=-4:4, y=-4:4, col=exp(-4:4))
ggplot(df) +
geom_point(aes(x=x,y=y,color= sign(col)*log(abs(col))))
ggplot(df) +
geom_point(aes(x=x,y=y,color= col))+
scale_color_continuous(trans='log')
我使用 ggallin
包和 pseudolog10_trans
函数解决了这个问题。更新后的代码如下:
ggplot(merged) +
geom_sf(aes(fill = Value/1000000000), color = NA, alpha = 0.9, size = 0.1) +
geom_sf(data = eu, fill = "deepskyblue4", color = NA, size = 0.1) +
#geom_sf(data = Africa, fill = NA, size = 0.1, col = "grey30") +
#geom_sf(data = borders, fill = NA, size = 0.1, col = "grey30") +
scale_fill_gradient2(
trans = pseudolog10_trans,
name = "Euros (Billions)",
guide = guide_legend(
direction = "horizontal",
keyheight = 0.5,
keywidth = 2,
title.position = "top",
title.hjust = 0,
label.hjust = .5,
nrow = 1,
byrow = TRUE,
reverse = FALSE,
label.position = "bottom"
)
) + theme_void()+
labs(
title = "Trade Balance of Europe Vis-à-vis the World (2020)",
subtitle = "(In Billions of Euros)",
caption = paste0("Source: Eurostat")) +
# Theme
theme(
#plot.background = element_rect(fill = "black"),
plot.title = element_text(
color = "black",
hjust = 0.5,
vjust = -1,
),
plot.subtitle = element_text(
color = "black",
hjust = 0.5,
vjust = -2,
face = "bold"
),
plot.caption = element_text(
color = "black",
size = 6,
hjust = 0.5,
margin = margin(b = 2, t = 13)
),
legend.text = element_text(
size = 7,
color = "black"
),
legend.title = element_text(
size = 7,
color = "black"
),
legend.position = c(0.5, 0.02),
)
它产生了以下地图: