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
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.
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: