Call.graphics error in ggplot

Am working with a shapefile while doing my spatial data analysis when i try to use ggplot to plot the sapefile this is the error it throws

library(ggplot2)
lf<- fortify(ke_counties,region = "CONST_CODE")#Convert spatial data
head(lf)
p<- ggplot() +
  geom_polygon(data=lf,
               aes(x=long,y=lat,group=group,fill=order),#the order is the column in the data
               color="black",size=0.25) +
  coord_map()#this adds coordinates in the map
p
lf

The Error that coem up yet my data frame is okey

Error in .Call.graphics(C_palette2, .Call(C_palette2, NULL)) : 
  invalid graphics state

Do you have a MWE of the ke_counties object?

Also, FYI, the ggplot2 manual recommends:

Rather than using this function, I now recommend using the broom package, which implements a much wider range of methods. fortify may be deprecated in the future.

whats mwe cant figure that quite well

The main challenge that am dealing with here its that this is spatial data

can it convert polygon or a shapefile into a data frame coz thats what i want to achieve

like which function allows us to convert a spatial polygon into a data frame in ggplot2

An MWE is a minimal working example, so that we know how ke_counties looks like.

It is possible to plot spatial data, and there are different ways.
I usually use the sf package.
You can read in a shape file with x <- st_read("file.shp")
and plot the file with ggplot() + geom_sf(data=x)

As a side note, here we call an MWE a REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.