Home > Net >  ggplot x axis label cant be edited
ggplot x axis label cant be edited

Time:05-08

library(sf)
library(maptools)
library(scales)
library(rnaturalearth)
library(rnaturalearthdata)

I have a grid data frame that i want to add to ggplot as an raster object

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"))

I have very nice layout using geom_sf() and world data from naturalearth package. Specifically axes labels are formatted nicely. However adding raster object, somehow ruins that x-axes label

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 axes coordinates are only showing 180s, and I cant find a way to resolve it enter image description here

CodePudding user response:

Based on enter image description here

  • Related