When I use "stat_contour_filled" to draw a figure the legends are discrete colors like this:
I would like to add a continuous colorbar like this:
How can I achieve it, here's my code:
ggpolar(pole = "N", max.lat = 90, min.lat = 30,
land.fill.colour = "#D9D9D9", land.outline.colour = "#343434",
plt.lat.labels = FALSE) +
ggtitle("A") +
theme(plot.title = element_text(hjust = 0, family = "Arial", size = 22,face = "bold")) +
stat_contour_filled(data = change_ratio_df,
aes(x = lon, y = lat, z = change_ratio),
breaks = c(-2, -1.8, -1.6, -1.4, -1.2, -1, -0.8, -0.6, -0.4, -0.2, 0,
0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4, 1.6, 1.8, 2)) +
scale_fill_manual(values = c("#053061", "#124580", "#1D5FA1", "#2D74B3", "#3A86BD", "#529AC7",
"#74B1D4", "#95C5DE", "#B1D4E6", "#CAE1ED", "#DFEBF2", "#EDF2F5",
"#FAF3ED", "#FCE8D9", "#FCD8C0", "#FAC0A2", "#F5A684", "#EB896C",
"#DB6C56", "#CC4F43", "#BD2F32", "#A81428", "#850C24", "#66001F" ))+
geom_point(data = selected_points, aes(x = lon, y = lat), color = "#343434", size = 0.5) +
geom_path(data = line_df,aes(X, Y, group = L1),colour = "#343434") +
theme(
plot.background = element_rect(fill = NA, color = NA), # 设置整个图的背景为无颜色
panel.background = element_rect(fill = NA, color = NA), # 设置绘图区域背景为无颜色
legend.position = "right",
legend.key.width = unit(2, "line"),
legend.key.height = unit(0.5, "line"),
legend.title = element_text(family = "Arial", size = 12),
legend.text = element_text(size = 12),
axis.title = element_blank(),
axis.text = element_text(family = "Arial", size = 12),
#panel.grid.major = element_line(size=0.25,linetype='dashed',colour="black"),
axis.ticks = element_blank()
)
When I use "stat_contour_filled" to draw a figure the legends are discrete colors like this:
I would like to add a continuous colorbar like this:
How can I achieve it, here's my code:
ggpolar(pole = "N", max.lat = 90, min.lat = 30,
land.fill.colour = "#D9D9D9", land.outline.colour = "#343434",
plt.lat.labels = FALSE) +
ggtitle("A") +
theme(plot.title = element_text(hjust = 0, family = "Arial", size = 22,face = "bold")) +
stat_contour_filled(data = change_ratio_df,
aes(x = lon, y = lat, z = change_ratio),
breaks = c(-2, -1.8, -1.6, -1.4, -1.2, -1, -0.8, -0.6, -0.4, -0.2, 0,
0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4, 1.6, 1.8, 2)) +
scale_fill_manual(values = c("#053061", "#124580", "#1D5FA1", "#2D74B3", "#3A86BD", "#529AC7",
"#74B1D4", "#95C5DE", "#B1D4E6", "#CAE1ED", "#DFEBF2", "#EDF2F5",
"#FAF3ED", "#FCE8D9", "#FCD8C0", "#FAC0A2", "#F5A684", "#EB896C",
"#DB6C56", "#CC4F43", "#BD2F32", "#A81428", "#850C24", "#66001F" ))+
geom_point(data = selected_points, aes(x = lon, y = lat), color = "#343434", size = 0.5) +
geom_path(data = line_df,aes(X, Y, group = L1),colour = "#343434") +
theme(
plot.background = element_rect(fill = NA, color = NA), # 设置整个图的背景为无颜色
panel.background = element_rect(fill = NA, color = NA), # 设置绘图区域背景为无颜色
legend.position = "right",
legend.key.width = unit(2, "line"),
legend.key.height = unit(0.5, "line"),
legend.title = element_text(family = "Arial", size = 12),
legend.text = element_text(size = 12),
axis.title = element_blank(),
axis.text = element_text(family = "Arial", size = 12),
#panel.grid.major = element_line(size=0.25,linetype='dashed',colour="black"),
axis.ticks = element_blank()
)
Share
Improve this question
asked Feb 17 at 15:02
Ruijie WangRuijie Wang
633 bronze badges
1
|
1 Answer
Reset to default 0You can achieve your desired result (which according to the image is not a colorbar but a binned legend) by switching to guide_bins
and by tweaking the legend using theme=
and override.aes
. Additionally I used the labels=
argument of scale_fill_manual
to label only each second break.
Note: The number of colors you specify differ from the number of breaks. Hence, not all colors will be used.
Using a minimal reprex based on the faithfuld
dataset.
library(ggplot2)
set.seed(123)
faithfuld2 <- faithfuld
faithfuld2$density <- runif(nrow(faithfuld2), -2.2, 2.2)
v <- ggplot(faithfuld2, aes(waiting, eruptions, z = density))
v +
stat_contour_filled(
breaks = seq(-2.2, 2.2, .2)
) +
scale_fill_manual(
values = c(
"#053061", "#124580", "#1D5FA1", "#2D74B3", "#3A86BD", "#529AC7",
"#74B1D4", "#95C5DE", "#B1D4E6", "#CAE1ED", "#DFEBF2", "#EDF2F5",
"#FAF3ED", "#FCE8D9", "#FCD8C0", "#FAC0A2", "#F5A684", "#EB896C",
"#DB6C56", "#CC4F43", "#BD2F32", "#A81428", "#850C24", "#66001F"
),
labels = \(x) ifelse(seq_along(x) %% 2 == 1, x, "")
) +
guides(
fill = guide_bins(
theme = theme(
legend.axis.line = element_blank()
),
override.aes = list(linewidth = 0)
)
)
dput
? – Quinten Commented Feb 17 at 15:36