ggplot x轴标签无法编辑
ggplot x axis label cant be edited
library(sf)
library(maptools)
library(scales)
library(rnaturalearth)
library(rnaturalearthdata)
我有一个网格数据框,我想将其作为栅格对象添加到 ggplot
plot_df<- dput(plot_df[100:150,c(1,2,22)])
structure(list(longitude = c(-179.75, -179.75, -179.75, -179.75,
-179.75, -179.75, -179.75, -179.75, -179.75, -179.75, -179.75,
-179.75, -179.75, -179.75, -179.75, -179.75, -179.75, -179.75,
-179.75, -179.75, -179.75, -179.75, -179.75, -179.75, -179.75,
-179.75, -179.75, -179.75, -179.75, -179.75, -179.75, -179.75,
-179.75, -179.75, -179.75, -179.75, -179.75, -179.75, -179.75,
-179.75, -179.75, -179.75, -179.75, -179.75, -179.75, -179.75,
-179.75, -179.75, -179.75, -179.75, -179.75), latitude = c(-40.25,
-39.75, -39.25, -38.75, -38.25, -37.75, -37.25, -36.75, -36.25,
-35.75, -35.25, -34.75, -34.25, -33.75, -33.25, -32.75, -32.25,
-31.75, -31.25, -30.75, -30.25, -29.75, -29.25, -28.75, -28.25,
-27.75, -27.25, -26.75, -26.25, -25.75, -25.25, -24.75, -24.25,
-23.75, -23.25, -22.75, -22.25, -21.75, -21.25, -20.75, -20.25,
-19.75, -19.25, -18.75, -18.25, -17.75, -17.25, -16.75, -16.25,
-15.75, -15.25), chl = c(5.79614072001568, 5.88809180584808,
5.91470803081587, 6.02307441926183, 5.94829246190804, 5.90227592841317,
5.88679405479741, 5.86218190882011, 5.88041651864321, 6.07368487605027,
6.1523111308037, 6.08351738769765, 6.05085211903314, 6.1526656045802,
6.01818374516031, 6.06720061938859, 6.10903571379908, 6.08132230310023,
5.94930399887433, 5.88474219391825, 5.9661907387131, 5.94088644166972,
5.7699915104762, 5.75660811309503, 5.72855772442633, 5.75380875490745,
5.74173250244927, 5.6893502273505, 5.65375136847291, 5.61335816690552,
5.62649249254121, 5.56363852997544, 5.61024930490138, 5.5855319776649,
5.57533893757262, 5.62878350382851, 5.67701322880346, 5.65414261630743,
5.65748798686245, 5.65999372947895, 5.70239818840709, 5.71014558066082,
5.68826542087147, 5.65905910276594, 5.54868461636391, 5.53320312262907,
5.55743825882603, 5.74248561230272, 5.76249373157709, 5.25381860438204,
5.02448076170133)), row.names = c(NA, -51L), class = c("tbl_df",
"tbl", "data.frame"))
我使用 geom_sf()
和来自 naturalearth 包的世界数据进行了非常好的布局。特别是轴标签的格式很好。然而添加光栅对象,以某种方式破坏了 x 轴标签
world <- ne_countries(scale = "medium", returnclass = "sf") # add continents
ggplot(data = world) +
geom_sf(color = "black", fill = "grey40") +
geom_raster(data=plot_df, aes(y= latitude, x = longitude, fill = chl)) +
scale_fill_gradientn(colours = rev(rainbow(7)), na.value = NA) +
scale_x_continuous(breaks = seq(-180, 180, by = 40))+
theme_bw() +
coord_sf(expand = FALSE) +
labs(fill="log(chl)", x="Longitude", y="Latitude")
X轴坐标只显示180s,找不到解决办法
基于https://github.com/tidyverse/ggplot2/issues/4571#issuecomment-890344707它是球面几何 (s2) 引擎故障。
你只需要在情节之前添加sf_use_s2(FALSE)
。
因此:
sf_use_s2(FALSE)
ggplot(data = world) +
geom_sf(color = "black", fill = "grey40") +
geom_raster(data=plot_df, aes(y= latitude, x = longitude, fill = chl)) +
scale_fill_gradientn(colours = rev(rainbow(7)), na.value = NA) +
scale_x_continuous(breaks = seq(from = -180, to = 180, by = 60)) +
theme_bw() +
coord_sf(expand = FALSE) +
labs(fill="log(chl)", x="Longitude", y="Latitude")
输出为:
library(sf)
library(maptools)
library(scales)
library(rnaturalearth)
library(rnaturalearthdata)
我有一个网格数据框,我想将其作为栅格对象添加到 ggplot
plot_df<- dput(plot_df[100:150,c(1,2,22)])
structure(list(longitude = c(-179.75, -179.75, -179.75, -179.75,
-179.75, -179.75, -179.75, -179.75, -179.75, -179.75, -179.75,
-179.75, -179.75, -179.75, -179.75, -179.75, -179.75, -179.75,
-179.75, -179.75, -179.75, -179.75, -179.75, -179.75, -179.75,
-179.75, -179.75, -179.75, -179.75, -179.75, -179.75, -179.75,
-179.75, -179.75, -179.75, -179.75, -179.75, -179.75, -179.75,
-179.75, -179.75, -179.75, -179.75, -179.75, -179.75, -179.75,
-179.75, -179.75, -179.75, -179.75, -179.75), latitude = c(-40.25,
-39.75, -39.25, -38.75, -38.25, -37.75, -37.25, -36.75, -36.25,
-35.75, -35.25, -34.75, -34.25, -33.75, -33.25, -32.75, -32.25,
-31.75, -31.25, -30.75, -30.25, -29.75, -29.25, -28.75, -28.25,
-27.75, -27.25, -26.75, -26.25, -25.75, -25.25, -24.75, -24.25,
-23.75, -23.25, -22.75, -22.25, -21.75, -21.25, -20.75, -20.25,
-19.75, -19.25, -18.75, -18.25, -17.75, -17.25, -16.75, -16.25,
-15.75, -15.25), chl = c(5.79614072001568, 5.88809180584808,
5.91470803081587, 6.02307441926183, 5.94829246190804, 5.90227592841317,
5.88679405479741, 5.86218190882011, 5.88041651864321, 6.07368487605027,
6.1523111308037, 6.08351738769765, 6.05085211903314, 6.1526656045802,
6.01818374516031, 6.06720061938859, 6.10903571379908, 6.08132230310023,
5.94930399887433, 5.88474219391825, 5.9661907387131, 5.94088644166972,
5.7699915104762, 5.75660811309503, 5.72855772442633, 5.75380875490745,
5.74173250244927, 5.6893502273505, 5.65375136847291, 5.61335816690552,
5.62649249254121, 5.56363852997544, 5.61024930490138, 5.5855319776649,
5.57533893757262, 5.62878350382851, 5.67701322880346, 5.65414261630743,
5.65748798686245, 5.65999372947895, 5.70239818840709, 5.71014558066082,
5.68826542087147, 5.65905910276594, 5.54868461636391, 5.53320312262907,
5.55743825882603, 5.74248561230272, 5.76249373157709, 5.25381860438204,
5.02448076170133)), row.names = c(NA, -51L), class = c("tbl_df",
"tbl", "data.frame"))
我使用 geom_sf()
和来自 naturalearth 包的世界数据进行了非常好的布局。特别是轴标签的格式很好。然而添加光栅对象,以某种方式破坏了 x 轴标签
world <- ne_countries(scale = "medium", returnclass = "sf") # add continents
ggplot(data = world) +
geom_sf(color = "black", fill = "grey40") +
geom_raster(data=plot_df, aes(y= latitude, x = longitude, fill = chl)) +
scale_fill_gradientn(colours = rev(rainbow(7)), na.value = NA) +
scale_x_continuous(breaks = seq(-180, 180, by = 40))+
theme_bw() +
coord_sf(expand = FALSE) +
labs(fill="log(chl)", x="Longitude", y="Latitude")
X轴坐标只显示180s,找不到解决办法
基于https://github.com/tidyverse/ggplot2/issues/4571#issuecomment-890344707它是球面几何 (s2) 引擎故障。
你只需要在情节之前添加sf_use_s2(FALSE)
。
因此:
sf_use_s2(FALSE)
ggplot(data = world) +
geom_sf(color = "black", fill = "grey40") +
geom_raster(data=plot_df, aes(y= latitude, x = longitude, fill = chl)) +
scale_fill_gradientn(colours = rev(rainbow(7)), na.value = NA) +
scale_x_continuous(breaks = seq(from = -180, to = 180, by = 60)) +
theme_bw() +
coord_sf(expand = FALSE) +
labs(fill="log(chl)", x="Longitude", y="Latitude")
输出为: