简体   繁体   中英

How to create a polar plot with date as month in ggplot2

I have a data frame df with the date of occurrence of the event. How to create a polar plot where it shows the months and a variable for a circle that goes from 0 to 1 and the point as color. In the plot the months would come through Date column, a single red point just below 1 would be from pnt , blue points would be the dates of occurrence,and ranno is a column of a random number between 0 to 1 to construct circles.

df<-structure(list(Date = structure(c(3471, 3822, 4165, 4521, 4936, 
                                      5273, 5653, 6040, 6422, 6733, 7092, 7457, 7825, 8197, 8637, 8950, 
                                      9316, 9696, 10040, 10373, 10759, 11118, 11485, 11846, 12235, 
                                      12603, 12946, 13298, 13671, 14103, 14433, 14751, 15151, 15499, 
                                      15884, 16192, 16581), class = "Date"), pnt = c(0.92, 0.92, 0.92, 
                                                                                     0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 
                                                                                     0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 
                                                                                     0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 
                                                                                     0.92), ranno = c(1.04, 0.71, 0.46, 0.83, 0.5, 0.87, 0.23, 0.78, 
                                                                                                      0.5, 0.59, 0.71, 0.92, 0.52, 0.07, 0.74, 0.57, 0.87, 0.05, 0.84, 
                                                                                                      0.25, 1.02, 0.21, 0.31, 0.05, 0.87, 0.98, 0.22, 0.79, 0.64, 0.22, 
                                                                                                      0.06, 0.89, 0.42, 0.19, 0.49, 0.09, 0.44), mon = c("July", "June", 
                                                                                                                                                         "May", "May", "July", "June", "June", "July", "August", "June", 
                                                                                                                                                         "June", "June", "June", "June", "August", "July", "July", "July", 
                                                                                                                                                         "June", "May", "June", "June", "June", "June", "July", "July", 
                                                                                                                                                         "June", "May", "June", "August", "July", "May", "June", "June", 
                                                                                                                                                         "June", "May", "May")), row.names = c(NA, -37L), class = c("tbl_df", 
                                                                                                                                                                                                                    "tbl", "data.frame"))



df
p  <- ggplot(df, aes(x=Date, y=ranno))+
  coord_polar(theta="x", start=pi/2,direction=1) 

p
pp<- p+
  geom_point(aes(pnt),color="Blue")

pp

I like to use the lubridate package when dealing with dates.

I'm not sure of the purpose of your ranno constant but I have added it as a red dot to the graph as per your question. I hope this is useful.

library(dplyr)
library(lubridate)
library(ggplot2)
Month <- c("January","February","March","April","May","June","July","August","September","October","November","December")
months <- data.frame(Month) %>% mutate(Month = as.character(Month))
df <- df %>% 
  mutate(Month = months(Date), DecimalDay = day(Date)/days_in_month(Date)) %>% 
  full_join(months, by="Month") %>%
  mutate(Month = factor(Month, levels=month.name))

ggplot(df, aes(x=Month, y=DecimalDay))+  
  coord_polar(theta="x", start=0,direction=-1) +  
  geom_point(color="Blue") +  
  geom_point(aes(y=pnt), colour="red") +
  theme_light()

I get the graph: 在此处输入图片说明

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