eliocamp / metR

Tools for Easier Analysis of Meteorological Fields
https://eliocamp.github.io/metR/
140 stars 22 forks source link

decimal places in geom_contour2 plot #117

Closed IssamHu closed 4 years ago

IssamHu commented 4 years ago

Dear Sir, How can I reduce the decimal places in the geom_contour2 plot my codes:

library("ggplot2")
library("plotly")
library("reshape2")
library("gridExtra")
library("metR")
vol<-read.csv(file.choose()) ###ocean_raster_contour_2019_New
vol$Date<- as.Date(vol$Date, format = "%m/%d/%Y")
okl<- ggplot() +
geom_contour_fill(data=vol, aes(x=Date, y=Depth, z= Temperature)) +

scale_fill_distiller(palette="Spectral", name="°C") +
geom_contour2(data=vol,aes(x=Date,y=Depth,z=Salinity, color=..level..)) +

theme_bw() + geom_text_contour(data=vol,aes(x=Date,y=Depth,
z=Salinity)) +

scale_y_continuous(trans = "reverse") +

scale_x_date(date_breaks = "1 month") +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),

    axis.title.x=element_blank()) +
guides(color=guide_legend("PPT"))
okl

That what I get:

Rplot

I have rounded my data in the csv file but still can't solve the problem: ocean_raster_contour_2019_New.txt Thanks

eliocamp commented 4 years ago

You need to round the label aesthetic in the geom_text_contour call. Like this:

geom_text_contour(data = vol, aes(x = Date, y = Depth, z = Salinity, 
                                  label = round(..level.., 2)))

Alternatively, (and probably better), you could force the breaks of the geom to be round numbers. I'm without a computer with R install so I can't create a reprex, but I hope the previous example is enough.

PS: I reformatted your comment so that the code is in a codeblock.

IssamHu commented 4 years ago

Thank you very much, it works with me

mattbk commented 3 years ago

This doesn't need to be reopened, but I can add that format() seems to work appropriately as well, but I could not get scales::label_number() to work.

label = format(..level..,
    scientific = F,
    big.mark = ",")