简体   繁体   中英

Remove axis labels from a plot in R after the plot has already been created

I am using the plot function of a particular package, namely the SPEI library . This function does not appear to accept any parameters to change the way the plot looks when it is generated.

I would like to know how to remove axis values, add new ones, and (ideally) rename the x-axis after the plot has already been created.

Please note that I have seen the other similar topics (eg: Remove plot axis values ) and they are not applicable to my situation. I know that when calling the base plot functions in R, you can set xaxt = "n" , axes= FALSE , etc.

Here is a quick version of what I mean:

library(SPEI)
data(wichita)
x <- spei(wichita[,'PRCP'], 1) 
plot.spei(x, main = "Here's a plot")
plot.spei(x, main = "Also a plot", xaxt = "n") #Note that xaxt does not affect output

That function uses base graphics and does not allow for any parameter to be passed through the function. There is no way to remove the x-axis labels without editing the function. Here's a way to make a copy and change just the one line that needs to be edited. (Note, since this method uses line numbers it's pretty fragile, this was tested with SPEI_1.7 )

my_plot_spei <- plot.spei
my_plot_spei_body <- as.list(body(my_plot_spei))
my_plot_spei_body[[c(14,4,5)]] <- quote(plot(datt, type = "n", xlab = "", ylab = label, main = main[i], ...))
body(my_plot_spei) <- as.call(my_plot_spei_body)

then this will work

x <- spei(wichita[,'PRCP'], 1) 
my_plot_spei(x, main = "Here's a plot", xaxt="n")

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM