Spatial plot error after changes to dataframe - r

I was using the following code to plot a spatial plot successfully:
colours<-(brewer.pal(7, "Blues"))
brks<-classIntervals(EDdata2$SIRt, n=7, style="fixed",fixedBreaks=c(0,1,2,5.0,10.0,20,50,120))
plot(brks, pal=colours)
brks<-brks$brks
plot(EDdata2, col=colours[findInterval(EDdata2$SIRt, brks,
all.inside=TRUE)], axes=F, border=FALSE)
However I made some changes to the spatialpolygonsdataframe EDdata2 by adding some extra columns and changing how SIRt is calculated (however it remains a column of numbers - they are just calculate differently)
Now when I try to run the plot code I get an error despite having made no changes to the plotting code:
Error in plot.default(...) : formal argument "axes" matched by multiple actual arguments
Whats going on here ?

That means that the package author of the unnamed package you used to create EDdata2 defined a plot method for whatever class EDdata2 might be that both used the axes argument and also used the triple dot mechanism to pass arguments to plot.default without filtering out that argument. (This does suggest that the package author didn't really want you to make your own axes, so you should investigate the help page for plot.whatever to see if it offers a mechanism for passing the values you want to use for 'at' and 'labels'.) You will need to do the spadework yourself (or edit your answer to make it more complete and reproducible) to investigate.
If this code is using the plot method for the SpatialPolygons-class in package:sp then the default value for axes is already FALSE.
help("SpatialPolygons-class", package="sp")
It is of course possible to mess this up by defining "F" to be something other than "FALSE" and then using axes=F. In the current instance it might be simpler to just remove that argument from the call.

Related

Pyramind Plot using ggplot2 in R

I need help in removing/converting the negative values in the x-axis of the pyramid plot.
I was able to build the pyramid through https://walker-data.com/census-r/exploring-us-census-data-with-visualization.html subheading 4.5.2. I keep getting an error saying function not found: number_format.
I think the scale_x_continuous is where I'd be able to change the - sign but returns an error everytime
So from the context of the plot, I am assuming that you don't want to convert the values themselves, but rather convert the x-axis so that the male and female sides of the pyramid are mirrored?
In which case, you can use abs. The function abs(x) computes the absolute value of x, which would remove the negative sign from your data's values.
Without seeing a reproducible example of your code (which should be included when you ask questions like this on Stack Overflow, see the package {reprex} for help with this), it's a little difficult to be sure exactly what you need to change to make the code work, but I think you should be on the right track with scale_x_continuous.
With regard to the error that you're receiving, it suggests that you haven't imported the library for that function, {scales}, as suggested by stefan in the comments (and as suggested, scales:::label_number has superseded scales:::number_format, so you should use the former).
If you're using the scale_x_continuous code from the second plot in Section 4.5.2 of the link you have shared:
utah_pyramid +
scale_x_continuous(
labels = ~ number_format(scale = .001, suffix = "k")(abs(.x)),
limits = 140000 * c(-1, 1)
)
The number_format function isn't the part of the code that is producing the absolute values, it is converting the scale of the values to thousands. It is the abs(.x) part that is removing the negative sign.

plotting polygons inside loop in R, why this ambiguous behavior?

So, I want to plot multiple polygons over one polygon using a loop in r. I have read that you need to use print in order to succesively display the polygons over the original polygon. However, that is not what R seems to do. Besides, in a hardly reproducible example, what i find is that either using print or not, the polygons will not plot within the loop. I am using a mac with OS monterrey, btw.
Here is a reproducible example, using print gives a bunch of NULL messages, but does print, not using print still plots. Yet a more complicated example will never plot from within the loop despite plotting everything when i request line by line from within the loop.
require(maptools)
data(wrld_simpl)
plot(wrld_simpl)
for (i in 1:12){
region=wrld_simpl[wrld_simpl$NAME==wrld_simpl$NAME[i],]
print(plot(region,border="red",add=T))
}
Maybe you're confusing print and plot? The plot function creates (or adds elements to) a plot. Whereas print function prints its arguments to the console. Since plot doesn't return a value, print has nothing to print and hence prints NULL.
Btw. you don't even need a loop. Since the loaded data wrld_simpl is of class SpatialPolygonsDataFrame, the object is subsettable like a dataframe you can just plot the needed indices:
plot(wrld_simpl)
plot(wrld_simpl[1:12,], border="red", add=T)
If you need help on a more complicated example, I suggest you share it?

Changing fonts in main title of plot in R

I am using Sweave and knitr together with <<dev="tikz">>= for figures. For simplicity, I first will try to explain my problem without providing a minimal working example:
I am using the command acf for plotting an autocorrelation function and want to change the font of the main title to e.g. font.main=1. I looked up the documentation which tells me that additional arguments of acf are the same as for plot.acf, which in turn uses the same as plot. Therefore I think font.main should work for acf as good as it does for plot. Unfortunately, adding an additional parameter for font.main in acf has no effect on the font of the main title. However, in plot this works fine. What is wrong here?
Something seems odd because the documentation of acf states that ... are "further arguments to be passed to plot.acf". And, the documentation of plot.acf further states that ... are "graphics parameters to be passed to the plotting routines".
This seems partially correct as passing font.lab and font.axis appear to produce the intended effect. However, font.main is ignored for reasons yet to be uncovered.
Until this gets fixed, the solution is to change the graphical parameters first, then run the command.
op <- par(font.main=1, ...)
acf(...)
par(op) # change back

plot function type=ā€œnā€ is ignored for plot(y~x)?

I am trying to plot a graph of certain values against time using the plot function.
I am simply trying to change the representation of the dots, using the pch= function. However R is simply ignoring me! I have also tried removing the dots so that I can place labels instead, but when I type in type="n" it ignores that too!
I am using the exact same format of code that I have used for other plots but this time it just isn't cooperating.
If I specify other features such as the title or the x/y axis labels, it will add those in but it simply ignores the pch or type commands.
This is my basic code:
plot(Differences ~ Time, data=subsetH)
But if I run
plot(Differences ~ Time, type="n", data=subsetH)
or
plot(Differences ~ Time, pch=2, data=subsetH)
it keeps plotting the same thing.
Is there something obvious I have missed?
I just came across your question because I encountered the same thing - creating an empty plot did not work, as type='n' was always ignored (as well as other type specifications).
With the help of this entry: Plotting time-series with Date labels on x-axis
I realized that my date format needed to be assigned as "date" class (as.Date()).
I know your entry dates back a little bit already, but maybe it's still useful.

"Regions defined for each Polygons" when mapping shp with R

I'm simply trying to visualize some land polygons from natural earth
landData2<-readOGR("/home/pavel/Documents/Studium/Hiwi/Maps/ne_10m_land", "ne_10m_land")
landGG2<- fortify(landData2)
after the second line I get the message
"Regions defined for each Polygons"
I already fortified it so this post was of no use
Why does ggplot give a blank page with the message "Regions defined for each Polygons"?
I got the data from
http://www.naturalearthdata.com/downloads/10m-physical-vectors/
I used the simple (not scale ranked) polygons
Does anyone know what it means?
Ok, this went fast.
Following this Post
Filling polygons of a map using GGplot in R
the problem seems to be that fortify does not transfer the id to a numeric variable
simply adding
landGG$id <-as.numeric(landGG$id)
helps
Still not sure why exactly this message comes out and why it is only true for the parts of the data frame after the "hole" variable (id, piece and group variable) but probably some problem with fortify
But it works.
Thought I'd leave it here instead of deleting

Resources