Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

r - Draw monthly data in geofacet US maps

I have a data df with the format

State Date Ratio
AL 2019-01 10.1
AL 2019-02 12.1
... ... ...
NY 2019-01 15.1
... ... ...
question from:https://stackoverflow.com/questions/65645671/draw-monthly-data-in-geofacet-us-maps

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your date is structured 'yyyy-mm', so I'm guessing it's a character vector rather than a date object. You should convert it to class Date with as.Date() and then it should work as expected. (You'll need to paste on the day of the month.)

You get a grouping error because when your x-axis is a character vector, geom_line will group by values of the character vector x-axis. Lines are drawn instead between the various y values at each x value. Here's an example using the geofacet package's own state_ranks dataset.

library(ggplot2)
library(dplyr)
library(geofacet)

data(state_ranks)


# The lines are not connected across a character x-axis.
ggplot(state_ranks) + 
  geom_line(aes(x = variable, y = rank))

# Throws error: geom_path: Each group consists of only one observation. Do 
#               you need to adjust the group aesthetic?
ggplot(state_ranks) + 
  geom_line(aes(x = variable, y = rank)) + 
  facet_geo(~ state)

If you group by state, you get the expected result (with an alphabetically ordered x-axis).

# Works, x-axis is alphabetized and lines are connected
ggplot(state_ranks) + 
  geom_line(aes(x = variable, y = rank, group = state)) + 
  facet_geo(~ state)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...