[R] Circular plot - polar plot code questions
Bruce Miller
b@t@nc@t@ @end|ng |rom gm@||@com
Sun Apr 23 19:19:01 CEST 2023
Hi all...
I assume there are a host of GGplot2 users out there.
I have circular plot - polar plot code questions
I needed to create circular – polar plots of reproductive status for
bats. I found a great sample of how to do this here:
https://stackoverflow.com/questions/41081376/how-to-create-a-circular-plot-showing-monthly-presence-or-absence-using-radial-p
I modified the sample code to meet the 3 data elements I am using the
"raw data" table below with T=testes enlarge, P= Pregnant and L= Lactating.
library(data.table)
m <- fread("id T month P L
1 0 1 0 0
2 0 2 0 0
3 0 3 0 0
4 1 4 0 0
5 1 5 1 0
6 1 6 1 1
7 0 7 1 1
8 0 8 1 1
9 0 9 0 1
10 0 10 0 0
11 0 11 0 0
12 0 12 0 0")
# reshape from wide to long (as preferred by ggplot)
ml <- melt(m, measure.vars = c("T", "P", "L"))
# create factors to ensure desired order
ml[, variable := factor(variable, levels = c("T", "P","L"))]
ml[, fct_month := factor(month, levels = 1:12, labels = month.abb)]
library(ggplot2)
ggplot(ml[value != 0], aes(x = fct_month, y = variable,
group = variable, colour = variable)) +
geom_line(size = 3) +
scale_y_discrete(expand = c(0, 1), breaks = NULL) +
xlim(month.abb) +
coord_polar() +
theme_bw() + xlab(NULL) + ylab(NULL)
This is creating a plot more or less as needed. 4 questions:
1 Do I even need the ID field? Seems not useful.
2 I assume the melt step can be avoided if my data is "Tidy" and long
format at the start, correct?
3 How can I increase the weight of the month lines and labels
4 Where can I add color choices for the 3 variables in the plot? Simply
adding an HTML color number to the “colour=variable” then plots the data
and labeling one value as that color.
Tnx all. The list is always educational on a daily basis.
Cheers,
Bat Dude
[[alternative HTML version deleted]]
More information about the R-help
mailing list