[R] Circular plot - polar plot code questions
Jim Lemon
drj|m|emon @end|ng |rom gm@||@com
Mon Apr 24 14:16:13 CEST 2023
Hi Bruce,
Reading your message, I get the idea that you want a radial.plot with
arcs to indicate "at least one bat is in this state'.This can be done
with an addition to the rp.type= argument. If you already have what
you want in this format, it may not be worth programming it. However,
if you want "how many bats are in this state":
m <- read.table(
text="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",
header=TRUE)
library(plotrix)
png("batstate.png",width=600)
kiteChart(t(m[,c("T","P","L")]),
main="Bat state by month",
xlab="Month",ylab="State",
varlabels=c("Testiular enlargement","Pregnant","Lactatins"),
timelabels=month.abb)
dev.off()
Jim
On Mon, Apr 24, 2023 at 7:22 PM Bruce Miller <batsncats using gmail.com> wrote:
>
> 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]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: batstate.png
Type: image/png
Size: 22537 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20230424/9d48c730/attachment.png>
More information about the R-help
mailing list